From a6205cbf1a93ea8f77a771de2dbd68c8f1e52ebe Mon Sep 17 00:00:00 2001
From: zhangnaisong <2434969829@qq.com>
Date: 星期五, 02 八月 2024 13:25:46 +0800
Subject: [PATCH] 电磁锁新增临时电子围栏查询修改提交

---
 ard-work/src/main/java/com/ruoyi/utils/websocket/service/ChatServerEndpoint.java |   79 +++++++++++++++++++++++++++------------
 1 files changed, 55 insertions(+), 24 deletions(-)

diff --git a/ard-work/src/main/java/com/ruoyi/utils/websocket/service/ChatServerEndpoint.java b/ard-work/src/main/java/com/ruoyi/utils/websocket/service/ChatServerEndpoint.java
index 82af738..ca6b465 100644
--- a/ard-work/src/main/java/com/ruoyi/utils/websocket/service/ChatServerEndpoint.java
+++ b/ard-work/src/main/java/com/ruoyi/utils/websocket/service/ChatServerEndpoint.java
@@ -1,26 +1,25 @@
 package com.ruoyi.utils.websocket.service;
 
 import com.alibaba.fastjson2.JSONObject;
-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.call.dto.CallMessage;
+import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.utils.websocket.util.WebSocketUtils;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
-import javax.annotation.Resource;
 import javax.websocket.*;
 import javax.websocket.server.PathParam;
 import javax.websocket.server.ServerEndpoint;
 import java.io.IOException;
-import java.util.*;
-import java.util.stream.Collectors;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static com.ruoyi.utils.websocket.util.WebSocketUtils.ONLINE_USER_SESSIONS;
 
 /**
  * @ClassName ChatServerEndpoint
@@ -40,31 +39,64 @@
         String message = "鐢ㄦ埛[" + userId + "] 鎴愬姛杩炴帴锛�";
         log.info("鐢ㄦ埛鐧诲綍锛�" + message);
         WebSocketUtils.sendMessage(session, message);
-        //region 鏃ф柟娉�
-//        //姣忎釜鐢ㄦ埛杩炴帴鏃讹紝鍚姩鍒濆鎵�鏈塧pp鐢ㄦ埛浣嶇疆鍙戦��
-//        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);
-//        sendMessageAll("鐢ㄦ埛[" + userid + "] : " + message);
+        log.debug("鏀跺埌娑堟伅锛�" + message);
+        Map<String, Object> messageMap = JSONObject.parseObject(message, Map.class);
+        if (messageMap.get("type")!=null &&messageMap.get("type").equals("callMessage")) {
+            String msg = (String) messageMap.get("message");
+            CallMessage callMessage = JSONObject.parseObject(msg, CallMessage.class);
+            //鍗曡亰
+            if (callMessage != null && callMessage.getType().equals(0)) {
+                WebSocketUtils.sendMessagePrefix(callMessage.getTargetId(), message);
+            }
+            //缇よ亰
+            if (callMessage != null && callMessage.getType().equals(1)) {
+                String step = callMessage.getStep();
+                switch (step) {
+                    case "joinRoom":
+                        Set<String> userSet = WebSocketUtils.ROOM_USER_SET.getOrDefault(callMessage.getRoomId(), new HashSet<>());
+                        userSet.add(callMessage.getUserId());
+                        WebSocketUtils.ROOM_USER_SET.put(callMessage.getRoomId(), userSet);
+                        WebSocketUtils.sendRoomMessage(callMessage.getRoomId(), message);
+                        break;
+                    case "inviteRoom":
+                        List<String> targetIds = callMessage.getTargetIds();
+                        targetIds.stream().forEach(targetId -> {
+                            WebSocketUtils.sendMessagePrefix(targetId, message);
+                        });
+                        break;
+                    case "leaveRoom":
+                        userSet = WebSocketUtils.ROOM_USER_SET.getOrDefault(callMessage.getRoomId(), new HashSet<>());
+                        if (userSet.size() > 0) {
+                            userSet.remove(callMessage.getUserId());
+                            WebSocketUtils.ROOM_USER_SET.put(callMessage.getRoomId(), userSet);
+                            WebSocketUtils.sendRoomMessage(callMessage.getRoomId(), message);
+                        }
+                        break;
+                    default:
+                        WebSocketUtils.sendMessagePrefix(callMessage.getTargetId(), message);
+                        break;
+                }
+
+
+            }
+        }
         // 鏍规嵁鐢ㄦ埛鏂扮殑棰戠巼閲嶆柊璋冩暣瀹氭椂浠诲姟
-        AppPositionPushService.messageHandler(userId,message);
+        AppPositionPushService.messageHandler(userId, message);
+
     }
 
     @OnClose
     public void onClose(@PathParam("userId") String userId, Session session) {
         //褰撳墠鐨凷ession 绉婚櫎
         WebSocketUtils.ONLINE_USER_SESSIONS.remove(userId);
-        //骞朵笖閫氱煡鍏朵粬浜哄綋鍓嶇敤鎴峰凡缁忕寮�鑱婂ぉ瀹や簡
         String message = "鐢ㄦ埛[" + userId + "] 鏂紑杩炴帴锛�";
-        //sendMessage(session,message);
         log.info("娑堟伅锛�" + message);
+        //浠庢埧闂翠腑绉婚櫎鐢ㄦ埛
+        WebSocketUtils.ROOM_USER_SET.values().stream().forEach(userSet -> userSet.remove(userId));
         try {
             session.close();
             AppPositionPushService.stopHandler(userId);
@@ -82,5 +114,4 @@
         }
         log.info("Throwable msg " + throwable.getMessage());
     }
-
 }
\ No newline at end of file

--
Gitblit v1.9.3