From 190cc52625bea627b6266f0124bd0d638f03394c Mon Sep 17 00:00:00 2001
From: clll <1320612696@qq.com>
Date: 星期三, 06 九月 2023 17:15:56 +0800
Subject: [PATCH] 更新代码

---
 springboot-vue3/src/main/java/com/example/springboot/component/WebSocketServer.java |  198 +++++++++++++++++++++++++++----------------------
 1 files changed, 110 insertions(+), 88 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 18785c1..3302c9b 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
@@ -1,10 +1,11 @@
 package com.example.springboot.component;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
 
 import javax.websocket.OnClose;
 import javax.websocket.OnError;
@@ -14,8 +15,8 @@
 import javax.websocket.server.PathParam;
 import javax.websocket.server.ServerEndpoint;
 
+import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.stereotype.Component;
-
 
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
@@ -23,96 +24,117 @@
 @ServerEndpoint(value = "/api/talk/{username}")
 @Component
 public class WebSocketServer {
-    
-   private static final Logger log = LoggerFactory.getLogger(WebSocketServer.class);
 
-    /**
-     * 璁板綍褰撳墠鍦ㄧ嚎杩炴帴鏁�
-     */
-    public static final Map<String, WebSocketServer> sessionMap = new ConcurrentHashMap<>();
-    
-    private String username;
-    private Session session;
-    /**
-     * 杩炴帴寤虹珛鎴愬姛璋冪敤鐨勬柟娉�
-     */
-    @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));  // 鍚庡彴鍙戦�佹秷鎭粰鎵�鏈夌殑瀹㈡埛绔�
+  static ConfigurableApplicationContext applicationContext;
+  // 瑙e喅鏃犳硶娉ㄥ叆mapper闂 聽//浣跨敤鏂规硶
+  // homeMapper=WebSocketServer.applicationContext.getBean(HomeMapper.class);
+
+  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);
     }
+  }
 
-    /**
-     * 杩炴帴鍏抽棴璋冪敤鐨勬柟娉�
-     */
-    @OnClose
-    public void onClose(Session session, @PathParam("username") String username) {
-        sessionMap.remove(username);
-        log.info("鏈変竴杩炴帴鍏抽棴锛岀Щ闄sername={}鐨勭敤鎴穝ession, 褰撳墠鍦ㄧ嚎浜烘暟涓猴細{}", username, sessionMap.size());
+  /**
+   * 鏈嶅姟绔彂閫佹秷鎭粰鎵�鏈夊鎴风
+   */
+  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);
     }
+  }
 
-    /**
-     * 鏀跺埌瀹㈡埛绔秷鎭悗璋冪敤鐨勬柟娉�
-     * 鍚庡彴鏀跺埌瀹㈡埛绔彂閫佽繃鏉ョ殑娑堟伅
-     * 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"); 
+  public List<String> getMessages() {
+    return messages;
+  }
 
-         JSONObject jsonObject = new JSONObject();
-         jsonObject.set("message", "ngng"); 
-         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 void clearMessages() {
+    messages.clear();
+  }
 }

--
Gitblit v1.8.0