From 29ddc8c2dede7549a17f7e44d3281efd3123fc28 Mon Sep 17 00:00:00 2001 From: caozehui <2427765068@qq.com> Date: Tue, 9 Jun 2026 14:48:12 +0800 Subject: [PATCH] =?UTF-8?q?webSocket=E6=B8=85=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../util/socket/websocket/WebServiceManager.java | 10 ++++++++++ .../util/socket/websocket/WebSocketHandler.java | 8 ++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebServiceManager.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebServiceManager.java index 6880f680..2ea7a0d8 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebServiceManager.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebServiceManager.java @@ -87,6 +87,16 @@ public class WebServiceManager { return userSessions.remove(userId); } + /** + * 仅当当前会话仍绑定到指定通道时才移除,避免旧连接误删新连接映射。 + */ + public static boolean removeByUserId(String userId, Channel channel) { + if (userId == null || channel == null) { + return false; + } + return userSessions.remove(userId, channel); + } + /** * 根据channelId移除会话(兼容老版本) * 时间复杂度:O(n),建议使用removeByUserId替代 diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebSocketHandler.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebSocketHandler.java index ff472c39..2f968d89 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebSocketHandler.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebSocketHandler.java @@ -111,7 +111,7 @@ public class WebSocketHandler extends SimpleChannelInboundHandler