|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @ClassName ChatServerEndpoint | 
|---|
|  |  |  | * @Description: | 
|---|
|  |  |  | * @Description: websocket操作类 | 
|---|
|  |  |  | * @Author 刘苏义 | 
|---|
|  |  |  | * @Date 2023/1/27 7:42 | 
|---|
|  |  |  | * @Version 1.0 | 
|---|
|  |  |  | 
|---|
|  |  |  | WebSocketUtils.sendMessage(session, message); | 
|---|
|  |  |  | //        sendMessageAll("用户[" + userid + "] : " + message); | 
|---|
|  |  |  | // 根据用户新的频率重新调整定时任务 | 
|---|
|  |  |  | 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.initDataList); | 
|---|
|  |  |  | 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"); | 
|---|
|  |  |  | if (frequency > 0) { | 
|---|
|  |  |  | List<ArdAppPosition> List = AppPositionPushService.initDataList.stream() | 
|---|
|  |  |  | .filter(obj -> !obj.getUserId().equals(uId)) | 
|---|
|  |  |  | .collect(Collectors.toList());//去除引导后的list | 
|---|
|  |  |  | AppPositionPushService.dataMap.put(userId,List); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<ArdAppPosition> tempList = AppPositionPushService.initDataList.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.initDataList); | 
|---|
|  |  |  | AppPositionPushService.dataMap.put(userId, newList); | 
|---|
|  |  |  | Timer timer = AppPositionPushService.tempUserTimerMap.get(userId); | 
|---|
|  |  |  | AppPositionPushService.stopTimerTask(timer); | 
|---|
|  |  |  | AppPositionPushService.tempUserTimerMap.remove(userId); | 
|---|
|  |  |  | AppPositionPushService.tempDataMap.remove(userId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //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 | 
|---|
|  |  |  | } | 
|---|
|  |  |  | AppPositionPushService.messageHandler(userId,message); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @OnClose | 
|---|
|  |  |  | public void onClose(@PathParam("userId") String userId, Session session) { | 
|---|
|  |  |  | //当前的Session 移除 | 
|---|
|  |  |  | WebSocketUtils.ONLINE_USER_SESSIONS.remove(userId); | 
|---|
|  |  |  | //        //并且通知其他人当前用户已经离开聊天室了 | 
|---|
|  |  |  | //并且通知其他人当前用户已经离开聊天室了 | 
|---|
|  |  |  | String message = "用户[" + userId + "] 断开连接!"; | 
|---|
|  |  |  | //        sendMessage(session,message); | 
|---|
|  |  |  | //sendMessage(session,message); | 
|---|
|  |  |  | log.info("消息:" + message); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | session.close(); | 
|---|
|  |  |  | //停止全局推送 | 
|---|
|  |  |  | 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);//旧方法 | 
|---|
|  |  |  | AppPositionPushService.stopHandler(userId); | 
|---|
|  |  |  | } catch (IOException e) { | 
|---|
|  |  |  | log.error("onClose error", e); | 
|---|
|  |  |  | } | 
|---|