张延森 hace 5 años
padre
commit
05e6ed1254
Se han modificado 1 ficheros con 12 adiciones y 4 borrados
  1. 12
    4
      src/main/java/com/huiju/estateagents/websocket/ChatServer.java

+ 12
- 4
src/main/java/com/huiju/estateagents/websocket/ChatServer.java Ver fichero

@@ -60,8 +60,7 @@ public class ChatServer {
60 60
      */
61 61
     @OnClose
62 62
     public void onClose() {
63
-        webSocketSet.remove(this);
64
-        subOnlineCount();
63
+        removeFromSets();
65 64
         log.info("有一连接关闭!当前在线人数为" + getOnlineCount());
66 65
     }
67 66
 
@@ -83,7 +82,7 @@ public class ChatServer {
83 82
 
84 83
         JSONObject jsnMsg = JSONObject.parseObject(message);
85 84
 
86
-        if ( null == ChatServer.taChatService) {
85
+        if (null == ChatServer.taChatService) {
87 86
             ChatServer.taChatService = applicationContext.getBean(ITaChatService.class);
88 87
         }
89 88
         System.out.println("收到的聊天消息的信息=========" + jsnMsg.toJSONString());
@@ -106,7 +105,7 @@ public class ChatServer {
106 105
     public void onError(Session session, Throwable error) {
107 106
         log.error("发生错误");
108 107
         error.printStackTrace();
109
-        webSocketSet.remove(this);
108
+        removeFromSets();
110 109
     }
111 110
 
112 111
     /**
@@ -116,6 +115,15 @@ public class ChatServer {
116 115
         this.session.getBasicRemote().sendText(message);
117 116
     }
118 117
 
118
+    private void removeFromSets() {
119
+        for(ChatServer c: webSocketSet) {
120
+            if (sid.equals(c.sid)) {
121
+                webSocketSet.remove(c);
122
+                subOnlineCount();
123
+            }
124
+        }
125
+    }
126
+
119 127
     /**
120 128
      * 发自定义消息
121 129
      * */