From 22a44adc0457de158c96a2c672a3a830c6f7a273 Mon Sep 17 00:00:00 2001 From: xd <844539747@qq.com> Date: Mon, 20 May 2024 11:08:48 +0800 Subject: [PATCH] '111' --- .../redisWebsocket/WebsocketEndpointImpl.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/websocket/redisWebsocket/WebsocketEndpointImpl.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/websocket/redisWebsocket/WebsocketEndpointImpl.java index 56315b0..9c149c0 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/websocket/redisWebsocket/WebsocketEndpointImpl.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/websocket/redisWebsocket/WebsocketEndpointImpl.java @@ -30,9 +30,13 @@ public class WebsocketEndpointImpl implements WebsocketEndpoint { */ private static Map> webSocketInfo; + private static Map concurrentHashMap; + static { // concurrent包的线程安全map webSocketInfo = new ConcurrentHashMap>(); + + concurrentHashMap = new ConcurrentHashMap(); } @@ -40,19 +44,26 @@ public class WebsocketEndpointImpl implements WebsocketEndpoint { public void onOpen(Session session, EndpointConfig config, @PathParam("userId") String userId,@PathParam("projectId") String projectId) { WebSocketBean bean = new WebSocketBean(); bean.setSession(session); +/* Map concurrentHashMap = new ConcurrentHashMap(); +*/ concurrentHashMap.put(userId,bean); webSocketInfo.put(projectId, concurrentHashMap); - log.info("ws项目:"+projectId+",客户端连接服务器userId :" + userId + "当前连接数:" + countUser(projectId)); + for (String userid:concurrentHashMap.keySet()){ + log.info("ws项目:"+projectId+",客户端现连接userId :" + userid); + } + log.info("当前连接数:" + countUser(projectId)); } @OnClose public void onClose(Session session, @PathParam("userId") String userId,@PathParam("projectId") String projectId) { // 客户端断开连接移除websocket对象 Map concurrentHashMap = webSocketInfo.get(projectId); - if(concurrentHashMap != null){concurrentHashMap.remove(userId);} - log.info("ws项目:"+projectId+",客户端断开连接,当前连接数:" + countUser(projectId)); - + if(concurrentHashMap != null){ + concurrentHashMap.remove(userId); + log.info("ws项目:"+projectId+",客户端断开userId :" + userId); + } + log.info("当前连接数:" + countUser(projectId)); } @OnMessage