From 38f29e38fcc668171dc05c53d40a36b895c86102 Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: 星期四, 10 十月 2024 13:34:28 +0800
Subject: [PATCH] init

---
 ard-work/src/main/java/com/ruoyi/utils/websocket/service/ChatServerEndpoint.java |   92 +++++++++++++++++++++++-----------------------
 1 files changed, 46 insertions(+), 46 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 5a0cebb..d796d94 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,30 +1,33 @@
 package com.ruoyi.utils.websocket.service;
 
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONArray;
+import com.alibaba.fastjson2.JSONException;
 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.common.core.domain.entity.SysUser;
-import com.ruoyi.system.service.ISysUserService;
+import com.ruoyi.app.position.service.impl.AppPositionPushService;
+import com.ruoyi.call.dto.CallMessage;
+import com.ruoyi.call.listener.WebsocketHandler;
+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.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Optional;
-import java.util.stream.Collectors;
+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
- * @Description:
+ * @Description: websocket鎿嶄綔绫�
  * @Author 鍒樿嫃涔�
  * @Date 2023/1/27 7:42
  * @Version 1.0
@@ -34,47 +37,29 @@
 @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);
-        //姣忎釜鐢ㄦ埛杩炴帴鏃讹紝鍚姩鍒濆鎵�鏈塧pp鐢ㄦ埛浣嶇疆鍙戦��
-
-        List<ArdAppPosition> positionList = AppPositionPush.initDataMap.get(userId);
-        AppPositionPush.initDataMap.put(userId,positionList);
-        AppPositionPush.initPushTask(userId,session,3000);
     }
 
     @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);
-        // 鏍规嵁鐢ㄦ埛鏂扮殑棰戠巼閲嶆柊璋冩暣瀹氭椂浠诲姟
-        Map<String,Object> map = JSONObject.parseObject(message, Map.class);
-        if(map.size()>0) {
-            Integer frequency = (Integer)map.get("frequency");
-            String uId = (String) map.get("userId");
-            Map<String, List<ArdAppPosition>> dataMap = new HashMap<>();
-            List<ArdAppPosition> positionList = AppPositionPush.initDataMap.get(userId);//鑾峰彇褰撳墠鐢ㄦ埛鐨勯粯璁ゅ彂閫佺殑鏁版嵁map
-            if(frequency>0)
-            {
-                //鎺ユ敹鍒伴鐜囧ぇ浜�0锛屽睘浜庣敵璇峰紩瀵硷紝鑾峰彇闄ゅ紩瀵煎鐨勫叾浠朼pp鐢ㄦ埛鐨勪綅缃甃ist
-                List<ArdAppPosition> appPositionList = positionList.stream()
-                        .filter(obj -> !obj.getUserId().equals(uId))
-                        .collect(Collectors.toList());
-                dataMap.put(userId,appPositionList);
+        log.debug("鏀跺埌娑堟伅锛�" + message);
+        try {
+            if(isValidJson(message)) {
+                // 澶勭悊瑙嗛閫氳瘽娑堟伅
+                WebsocketHandler.handleMessage(userId, message);
+                // 鏍规嵁鐢ㄦ埛鏂扮殑棰戠巼閲嶆柊璋冩暣瀹氭椂浠诲姟
+                AppPositionPushService.messageHandler(userId, message);
+            } else {
+                // 澶勭悊鍏朵粬娑堟伅
+                WebSocketUtils.sendMessage(userId,"Received an invalid JSON message锛�"+message);
             }
-           else {
-                //鎺ユ敹鍒伴鐜囩瓑浜�0锛屽睘浜庨噴鏀惧紩瀵硷紝鑾峰彇鍒板叏閮╝pp鐢ㄦ埛鐨勪綅缃甃ist
-                dataMap.put(userId,AppPositionPush.getAppPositionList());
-            }
-            AppPositionPush.initDataMap.putAll(dataMap);//鏇存柊鍒濆浼氳瘽榛樿鏁版嵁map
-            AppPositionPush.startLocationPushTask(userId,session,uId, frequency);//寮�鍚柊浠诲姟
+        } catch (Exception ex) {
+            WebSocketUtils.sendMessage(userId, "鏃犳硶瑙f瀽娑堟伅銆�" + message + "銆�:" + ex.getMessage());
         }
     }
 
@@ -82,13 +67,13 @@
     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);
+        String message = "鐢ㄦ埛[" + userId + "] 鏂紑杩炴帴锛�";
+        log.info("娑堟伅锛�" + message);
+        //浠庢埧闂翠腑绉婚櫎鐢ㄦ埛
+        WebSocketUtils.ROOM_USER_SET.values().stream().forEach(userSet -> userSet.remove(userId));
         try {
             session.close();
-            AppPositionPush.stopLocationPushTask(session);
+            AppPositionPushService.stopHandler(userId);
         } catch (IOException e) {
             log.error("onClose error", e);
         }
@@ -104,4 +89,19 @@
         log.info("Throwable msg " + throwable.getMessage());
     }
 
+    /**
+     * 妫�鏌ョ粰瀹氱殑瀛楃涓叉槸鍚︿负鏈夋晥鐨凧SON鏍煎紡銆�
+     *
+     * @param message 瑕佹鏌ョ殑瀛楃涓�
+     * @return 濡傛灉鏄湁鏁堢殑JSON瀛楃涓诧紝杩斿洖true锛涘惁鍒欒繑鍥瀎alse銆�
+     */
+    public static boolean isValidJson(String message) {
+        try {
+            Object json = JSON.parse(message);
+            // 纭繚瑙f瀽缁撴灉鏄疛SONObject鎴朖SONArray绫诲瀷
+            return (json instanceof JSONObject || json instanceof JSONArray);
+        } catch (JSONException e) {
+            return false;  // 瑙f瀽澶辫触锛岃鏄庝笉鏄悎娉曠殑JSON
+        }
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3