张延森 5 년 전
부모
커밋
05e6ed1254
1개의 변경된 파일12개의 추가작업 그리고 4개의 파일을 삭제
  1. 12
    4
      src/main/java/com/huiju/estateagents/websocket/ChatServer.java

+ 12
- 4
src/main/java/com/huiju/estateagents/websocket/ChatServer.java 파일 보기

60
      */
60
      */
61
     @OnClose
61
     @OnClose
62
     public void onClose() {
62
     public void onClose() {
63
-        webSocketSet.remove(this);
64
-        subOnlineCount();
63
+        removeFromSets();
65
         log.info("有一连接关闭!当前在线人数为" + getOnlineCount());
64
         log.info("有一连接关闭!当前在线人数为" + getOnlineCount());
66
     }
65
     }
67
 
66
 
83
 
82
 
84
         JSONObject jsnMsg = JSONObject.parseObject(message);
83
         JSONObject jsnMsg = JSONObject.parseObject(message);
85
 
84
 
86
-        if ( null == ChatServer.taChatService) {
85
+        if (null == ChatServer.taChatService) {
87
             ChatServer.taChatService = applicationContext.getBean(ITaChatService.class);
86
             ChatServer.taChatService = applicationContext.getBean(ITaChatService.class);
88
         }
87
         }
89
         System.out.println("收到的聊天消息的信息=========" + jsnMsg.toJSONString());
88
         System.out.println("收到的聊天消息的信息=========" + jsnMsg.toJSONString());
106
     public void onError(Session session, Throwable error) {
105
     public void onError(Session session, Throwable error) {
107
         log.error("发生错误");
106
         log.error("发生错误");
108
         error.printStackTrace();
107
         error.printStackTrace();
109
-        webSocketSet.remove(this);
108
+        removeFromSets();
110
     }
109
     }
111
 
110
 
112
     /**
111
     /**
116
         this.session.getBasicRemote().sendText(message);
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
      * */