From ee84c191faa2c235703eaa8cdc765729bed71a4f Mon Sep 17 00:00:00 2001
From: clll <1320612696@qq.com>
Date: 星期三, 06 九月 2023 17:37:46 +0800
Subject: [PATCH] Merge branch 'master' of http://10.153.19.150:10101/r/CanadaMes

---
 springboot-vue3/src/main/java/com/example/springboot/component/WebSocketServer.java |  217 +++++++++++++++++++++++++++--------------------------
 1 files changed, 111 insertions(+), 106 deletions(-)

diff --git a/springboot-vue3/src/main/java/com/example/springboot/component/WebSocketServer.java b/springboot-vue3/src/main/java/com/example/springboot/component/WebSocketServer.java
index 3302c9b..8749b5e 100644
--- a/springboot-vue3/src/main/java/com/example/springboot/component/WebSocketServer.java
+++ b/springboot-vue3/src/main/java/com/example/springboot/component/WebSocketServer.java
@@ -4,8 +4,11 @@
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.catalina.core.ApplicationContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
 
 import javax.websocket.OnClose;
 import javax.websocket.OnError;
@@ -25,116 +28,118 @@
 @Component
 public class WebSocketServer {
 
-  static ConfigurableApplicationContext applicationContext;
-  // 瑙e喅鏃犳硶娉ㄥ叆mapper闂 聽//浣跨敤鏂规硶
-  // homeMapper=WebSocketServer.applicationContext.getBean(HomeMapper.class);
+    // @Autowired
+    // HomeMapper homeMapper;
 
-  public static void setApplicationContext(ConfigurableApplicationContext configurableApplicationContext) {
-    WebSocketServer.applicationContext = configurableApplicationContext;
-  }
-
-  private static final Logger log = LoggerFactory.getLogger(WebSocketServer.class);
-  private List<String> messages;
-  /**
-   * 璁板綍褰撳墠鍦ㄧ嚎杩炴帴鏁�
-   */
-  public static final Map<String, WebSocketServer> sessionMap = new ConcurrentHashMap<>();
-
-  String username;
-  Session session;
-
-  public WebSocketServer() {
-    this.messages = new ArrayList<>();
-  }
-
-  /**
-   * 杩炴帴寤虹珛鎴愬姛璋冪敤鐨勬柟娉�
-   */
-  @OnOpen
-  public void onOpen(Session session, @PathParam("username") String username) {
-    this.username = username;
-    this.session = session;
-    sessionMap.put(username, this);
-    log.info("鏈夋柊鐢ㄦ埛鍔犲叆锛寀sername={}, 褰撳墠鍦ㄧ嚎浜烘暟涓猴細{}", username, sessionMap.size());
-
-    // JSONObject result = new JSONObject();
-    // JSONArray array = new JSONArray();
-    // result.set("users", array);
-    // for (Object key : sessionMap.keySet()) {
-    // JSONObject jsonObject = new JSONObject();
-    // jsonObject.set("username", key);
-    // array.add(jsonObject);
-    // }
-
-    // sendAllMessage(JSONUtil.toJsonStr(result)); // 鍚庡彴鍙戦�佹秷鎭粰鎵�鏈夌殑瀹㈡埛绔�
-  }
-
-  /**
-   * 杩炴帴鍏抽棴璋冪敤鐨勬柟娉�
-   */
-  @OnClose
-  public void onClose(Session session, @PathParam("username") String username) {
-    sessionMap.remove(username);
-    log.info("鏈変竴杩炴帴鍏抽棴锛岀Щ闄sername={}鐨勭敤鎴穝ession, 褰撳墠鍦ㄧ嚎浜烘暟涓猴細{}", username, sessionMap.size());
-  }
-
-  /**
-   * 鏀跺埌瀹㈡埛绔秷鎭悗璋冪敤鐨勬柟娉�
-   * 鍚庡彴鏀跺埌瀹㈡埛绔彂閫佽繃鏉ョ殑娑堟伅
-   * onMessage 鏄竴涓秷鎭殑涓浆绔�
-   * 鎺ュ彈 娴忚鍣ㄧ socket.send 鍙戦�佽繃鏉ョ殑 json鏁版嵁
-   * 
-   * @param message 瀹㈡埛绔彂閫佽繃鏉ョ殑娑堟伅
-   */
-  @OnMessage
-  public void onMessage(String message, Session session, @PathParam("username") String username) {
-    log.info("鏈嶅姟绔敹鍒扮敤鎴穟sername={}鐨勬秷鎭�:{}", username, message);
-    JSONObject obj = JSONUtil.parseObj(message);
-    String text = obj.getStr("data");
-
-    JSONObject jsonObject = new JSONObject();
-    jsonObject.set("message", text);
-    this.messages.add(text);
-    this.sendMessage(jsonObject.toString()); // JSONUtil.toJsonStr(jsonObject)
-  }
-
-  @OnError
-  public void onError(Session session, Throwable error) {
-    log.error("鍙戠敓閿欒");
-    error.printStackTrace();
-  }
-
-  /**
-   * 鏈嶅姟绔彂閫佹秷鎭粰瀹㈡埛绔�
-   */
-  public void sendMessage(String message) {
-    try {
-      log.info("鏈嶅姟绔粰瀹㈡埛绔痆{}]鍙戦�佹秷鎭瘂}", this.session.getId(), message);
-      this.session.getBasicRemote().sendText(message);
-    } catch (Exception e) {
-      log.error("鏈嶅姟绔彂閫佹秷鎭粰瀹㈡埛绔け璐�", e);
+    static ConfigurableApplicationContext applicationContext;
+ 
+    //瑙e喅鏃犳硶娉ㄥ叆mapper闂  //浣跨敤鏂规硶 homeMapper=WebSocketServer.applicationContext.getBean(HomeMapper.class);
+    public static void setApplicationContext(ConfigurableApplicationContext configurableApplicationContext) {
+        WebSocketServer.applicationContext = configurableApplicationContext;
     }
-  }
 
-  /**
-   * 鏈嶅姟绔彂閫佹秷鎭粰鎵�鏈夊鎴风
-   */
-  public void sendAllMessage(String message) {
-    try {
-      for (WebSocketServer webSocketServer : sessionMap.values()) {
-        log.info("鏈嶅姟绔粰瀹㈡埛绔痆{}]鍙戦�佹秷鎭瘂}", this.session.getId(), message);
-        webSocketServer.sendMessage(message);
-      }
-    } catch (Exception e) {
-      log.error("鏈嶅姟绔彂閫佹秷鎭粰瀹㈡埛绔け璐�", e);
+
+   private static final Logger log = LoggerFactory.getLogger(WebSocketServer.class);
+    private List<String> messages;
+    /**
+     * 璁板綍褰撳墠鍦ㄧ嚎杩炴帴鏁�
+     */
+    public static final Map<String, WebSocketServer> sessionMap = new ConcurrentHashMap<>();
+    
+    String username;
+    Session session;
+    public WebSocketServer() {
+        this.messages = new ArrayList<>();
     }
-  }
+    /**
+     * 杩炴帴寤虹珛鎴愬姛璋冪敤鐨勬柟娉�
+     */
+    @OnOpen
+    public void onOpen(Session session, @PathParam("username") String username) {
+        this.username=username;
+        this.session=session;
+        sessionMap.put(username, this);
+        log.info("鏈夋柊鐢ㄦ埛鍔犲叆锛寀sername={}, 褰撳墠鍦ㄧ嚎浜烘暟涓猴細{}", username, sessionMap.size());
+       
+        // JSONObject result = new JSONObject();
+        // JSONArray array = new JSONArray();
+        // result.set("users", array);
+        // for (Object key : sessionMap.keySet()) {
+        //     JSONObject jsonObject = new JSONObject();
+        //     jsonObject.set("username", key); 
+        //     array.add(jsonObject);
+        // }
+   
+        //sendAllMessage(JSONUtil.toJsonStr(result));  // 鍚庡彴鍙戦�佹秷鎭粰鎵�鏈夌殑瀹㈡埛绔�
+    }
 
-  public List<String> getMessages() {
-    return messages;
-  }
+    /**
+     * 杩炴帴鍏抽棴璋冪敤鐨勬柟娉�
+     */
+    @OnClose
+    public void onClose(Session session, @PathParam("username") String username) {
+        sessionMap.remove(username);
+        log.info("鏈変竴杩炴帴鍏抽棴锛岀Щ闄sername={}鐨勭敤鎴穝ession, 褰撳墠鍦ㄧ嚎浜烘暟涓猴細{}", username, sessionMap.size());
+    }
 
-  public void clearMessages() {
-    messages.clear();
-  }
+    /**
+     * 鏀跺埌瀹㈡埛绔秷鎭悗璋冪敤鐨勬柟娉�
+     * 鍚庡彴鏀跺埌瀹㈡埛绔彂閫佽繃鏉ョ殑娑堟伅
+     * onMessage 鏄竴涓秷鎭殑涓浆绔�
+     * 鎺ュ彈 娴忚鍣ㄧ socket.send 鍙戦�佽繃鏉ョ殑 json鏁版嵁
+     * @param message 瀹㈡埛绔彂閫佽繃鏉ョ殑娑堟伅
+     */
+    @OnMessage
+    public void onMessage(String message, Session session, @PathParam("username") String username) {
+        log.info("鏈嶅姟绔敹鍒扮敤鎴穟sername={}鐨勬秷鎭�:{}", username, message);
+        JSONObject obj = JSONUtil.parseObj(message);
+         String text = obj.getStr("data"); 
+
+         JSONObject jsonObject = new JSONObject();
+         jsonObject.set("message", text);
+        this.messages.add(text);
+         this.sendMessage(jsonObject.toString()); //JSONUtil.toJsonStr(jsonObject)
+         
+    }
+
+    @OnError
+    public void onError(Session session, Throwable error) {
+        log.error("鍙戠敓閿欒");
+        error.printStackTrace();
+    }
+
+    /**
+     * 鏈嶅姟绔彂閫佹秷鎭粰瀹㈡埛绔�
+     */
+    public void sendMessage(String message) {
+        try {
+            log.info("鏈嶅姟绔粰瀹㈡埛绔痆{}]鍙戦�佹秷鎭瘂}", this.session.getId(), message);
+            this.session.getBasicRemote().sendText(message);
+        } catch (Exception e) {
+            log.error("鏈嶅姟绔彂閫佹秷鎭粰瀹㈡埛绔け璐�", e);
+        }
+    }
+
+    /**
+     * 鏈嶅姟绔彂閫佹秷鎭粰鎵�鏈夊鎴风
+     */
+    public void sendAllMessage(String message) {
+        try {
+            for (WebSocketServer webSocketServer : sessionMap.values()) {
+                log.info("鏈嶅姟绔粰瀹㈡埛绔痆{}]鍙戦�佹秷鎭瘂}", this.session.getId(), message);
+                webSocketServer.sendMessage(message);
+            }
+        } catch (Exception e) {
+            log.error("鏈嶅姟绔彂閫佹秷鎭粰瀹㈡埛绔け璐�", e);
+        }
+    }
+
+    public List<String> getMessages() {
+        return messages;
+        
+    }
+
+    public void clearMessages() {
+        messages.clear();
+    }
 }

--
Gitblit v1.8.0