| | |
| | | import com.ruoyi.app.position.domain.ArdAppPosition; |
| | | import com.ruoyi.app.position.service.IArdAppPositionService; |
| | | import com.ruoyi.app.position.service.impl.AppPositionPush; |
| | | import com.ruoyi.app.position.service.impl.AppPositionPushService; |
| | | import com.ruoyi.app.position.service.impl.ArdAppPositionServiceImpl; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import com.ruoyi.system.service.ISysUserService; |
| | | import com.ruoyi.utils.websocket.util.WebSocketUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | import javax.websocket.server.PathParam; |
| | | import javax.websocket.server.ServerEndpoint; |
| | | import java.io.IOException; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Optional; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | @Slf4j(topic = "websocket") |
| | | @ServerEndpoint("/websocket/{userId}") |
| | | public class ChatServerEndpoint { |
| | | |
| | | @OnOpen |
| | | public void openSession(@PathParam("userId") String userId, Session session) { |
| | | WebSocketUtils.ONLINE_USER_SESSIONS.put(userId, session); |
| | | String message = "用户[" + userId + "] 成功连接!"; |
| | | log.info("用户登录:" + message); |
| | | WebSocketUtils.sendMessage(session, message); |
| | | //每个用户连接时,启动初始所有app用户位置发送 |
| | | |
| | | List<ArdAppPosition> positionList = AppPositionPush.defaultDataMap.get(userId); |
| | | AppPositionPush.defaultDataMap.put(userId,positionList); |
| | | AppPositionPush.initPushTask(userId,session,3000); |
| | | //region 旧方法 |
| | | // //每个用户连接时,启动初始所有app用户位置发送 |
| | | // AppPositionPush.initDataMap.put(userId,AppPositionPush.getAppPositionList()); |
| | | // AppPositionPush.initPushTask(userId,session,3000); |
| | | //endregion |
| | | } |
| | | |
| | | @OnMessage |
| | | public void onMessage(@PathParam("userId") String userId, String message) { |
| | | log.info("收到消息:" + message); |
| | | Session session = WebSocketUtils.ONLINE_USER_SESSIONS.get(userId); |
| | | // WebSocketUtils.sendMessage(session, message); |
| | | WebSocketUtils.sendMessage(session, message); |
| | | // sendMessageAll("用户[" + userid + "] : " + message); |
| | | // 根据用户新的频率重新调整定时任务 |
| | | Map<String,Object> map = JSONObject.parseObject(message, Map.class); |
| | | if(map.size()>0) { |
| | | Integer frequency = (Integer)map.get("frequency"); |
| | | Map<String, Object> map = JSONObject.parseObject(message, Map.class); |
| | | if (map.size() > 0) { |
| | | Boolean enabled = (Boolean) map.get("enabled"); |
| | | if (enabled) { |
| | | //启动全局推送 |
| | | List<ArdAppPosition> newList = new ArrayList<>(AppPositionPushService.getAppPositionList()); |
| | | AppPositionPushService.dataMap.put(userId, newList); |
| | | if (!AppPositionPushService.userTimerMap.containsKey(userId)) { |
| | | Timer timer = AppPositionPushService.startTimerTask(userId, 3000); |
| | | AppPositionPushService.userTimerMap.put(userId, timer);//将定时器关联用户 |
| | | } |
| | | } else { |
| | | //停止全局推送 |
| | | if (AppPositionPushService.userTimerMap.containsKey(userId)) { |
| | | Timer timer = AppPositionPushService.userTimerMap.get(userId); |
| | | AppPositionPushService.stopTimerTask(timer); |
| | | AppPositionPushService.userTimerMap.remove(userId); |
| | | } |
| | | if (AppPositionPushService.tempUserTimerMap.containsKey(userId)) { |
| | | Timer temptimer = AppPositionPushService.tempUserTimerMap.get(userId); |
| | | AppPositionPushService.stopTimerTask(temptimer); |
| | | AppPositionPushService.tempUserTimerMap.remove(userId); |
| | | } |
| | | AppPositionPushService.dataMap.remove(userId); |
| | | AppPositionPushService.tempDataMap.remove(userId); |
| | | } |
| | | |
| | | Integer frequency = (Integer) map.get("frequency"); |
| | | String uId = (String) map.get("userId"); |
| | | Map<String, List<ArdAppPosition>> dataMap = new HashMap<>(); |
| | | List<ArdAppPosition> positionList = AppPositionPush.defaultDataMap.get(userId);//获取当前用户的默认发送的数据map |
| | | if(frequency>0) |
| | | { |
| | | //接收到频率大于0,属于申请引导,获取除引导外的其他app用户的位置List |
| | | List<ArdAppPosition> appPositionList = positionList.stream() |
| | | if (frequency > 0) { |
| | | List<ArdAppPosition> List = AppPositionPushService.getAppPositionList().stream() |
| | | .filter(obj -> !obj.getUserId().equals(uId)) |
| | | .collect(Collectors.toList()); |
| | | dataMap.put(userId,appPositionList); |
| | | .collect(Collectors.toList());//去除引导后的list |
| | | AppPositionPushService.dataMap.put(userId,List); |
| | | |
| | | List<ArdAppPosition> tempList = AppPositionPushService.getAppPositionList().stream() |
| | | .filter(obj -> obj.getUserId().equals(uId)) |
| | | .collect(Collectors.toList());//获取引导的list |
| | | AppPositionPushService.tempDataMap.put(userId, tempList); |
| | | Timer timer = AppPositionPushService.startTempTimerTask(userId, frequency); |
| | | AppPositionPushService.tempUserTimerMap.put(userId, timer); |
| | | } else { |
| | | List<ArdAppPosition> newList = new ArrayList<>(AppPositionPushService.getAppPositionList()); |
| | | AppPositionPushService.dataMap.put(userId, newList); |
| | | Timer timer = AppPositionPushService.tempUserTimerMap.get(userId); |
| | | AppPositionPushService.stopTimerTask(timer); |
| | | AppPositionPushService.tempUserTimerMap.remove(userId); |
| | | AppPositionPushService.tempDataMap.remove(userId); |
| | | } |
| | | else { |
| | | //接收到频率等于0,属于释放引导,获取到全部app用户的位置List |
| | | dataMap.put(userId,AppPositionPush.getAppPositionList()); |
| | | } |
| | | AppPositionPush.defaultDataMap.putAll(dataMap);//更新初始会话默认数据map |
| | | AppPositionPush.startLocationPushTask(userId,session,uId, frequency);//开启新任务 |
| | | //region 旧方法 |
| | | // Map<String, List<ArdAppPosition>> dataMap = new HashMap<>(); |
| | | // List<ArdAppPosition> positionList = AppPositionPush.initDataMap.get(userId);//获取当前用户的默认发送的数据map |
| | | // if(frequency>0) |
| | | // { |
| | | // //接收到频率大于0,属于申请引导,获取除引导外的其他app用户的位置List |
| | | // List<ArdAppPosition> appPositionList = positionList.stream() |
| | | // .filter(obj -> !obj.getUserId().equals(uId)) |
| | | // .collect(Collectors.toList()); |
| | | // dataMap.put(userId,appPositionList); |
| | | // } |
| | | // else { |
| | | // //接收到频率等于0,属于释放引导,获取到全部app用户的位置List |
| | | // dataMap.put(userId,AppPositionPush.getAppPositionList()); |
| | | // } |
| | | // AppPositionPush.initDataMap.putAll(dataMap);//更新初始会话默认数据map |
| | | // AppPositionPush.startLocationPushTask(userId,session,uId, frequency);//开启新任务 |
| | | //endregion |
| | | } |
| | | } |
| | | |
| | |
| | | //当前的Session 移除 |
| | | WebSocketUtils.ONLINE_USER_SESSIONS.remove(userId); |
| | | // //并且通知其他人当前用户已经离开聊天室了 |
| | | String message="用户[" + userId + "] 断开连接!"; |
| | | String message = "用户[" + userId + "] 断开连接!"; |
| | | // sendMessage(session,message); |
| | | log.info("消息:"+message); |
| | | log.info("消息:" + message); |
| | | try { |
| | | session.close(); |
| | | AppPositionPush.stopLocationPushTask(session); |
| | | //停止全局推送 |
| | | if (AppPositionPushService.userTimerMap.containsKey(userId)) { |
| | | Timer timer = AppPositionPushService.userTimerMap.get(userId); |
| | | AppPositionPushService.stopTimerTask(timer); |
| | | AppPositionPushService.userTimerMap.remove(userId); |
| | | } |
| | | if (AppPositionPushService.tempUserTimerMap.containsKey(userId)) { |
| | | Timer temptimer = AppPositionPushService.tempUserTimerMap.get(userId); |
| | | AppPositionPushService.stopTimerTask(temptimer); |
| | | AppPositionPushService.tempUserTimerMap.remove(userId); |
| | | } |
| | | AppPositionPushService.dataMap.remove(userId); |
| | | AppPositionPushService.tempDataMap.remove(userId); |
| | | // AppPositionPush.stopLocationPushTask(session);//旧方法 |
| | | } catch (IOException e) { |
| | | log.error("onClose error", e); |
| | | } |