From 3802044eeac5516834a7452bf12c626d6cd5a507 Mon Sep 17 00:00:00 2001 From: liyj <1617420630@qq.com> Date: Mon, 24 Nov 2025 15:03:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E8=AF=9Dsession=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../onchat/MallsuiteImSocketHandler.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/mall-im/src/main/java/com/suisung/mall/im/common/websocket/service/onchat/MallsuiteImSocketHandler.java b/mall-im/src/main/java/com/suisung/mall/im/common/websocket/service/onchat/MallsuiteImSocketHandler.java index e2621931..5141d15d 100644 --- a/mall-im/src/main/java/com/suisung/mall/im/common/websocket/service/onchat/MallsuiteImSocketHandler.java +++ b/mall-im/src/main/java/com/suisung/mall/im/common/websocket/service/onchat/MallsuiteImSocketHandler.java @@ -3,6 +3,7 @@ package com.suisung.mall.im.common.websocket.service.onchat; import cn.hutool.core.convert.Convert; import cn.hutool.core.util.IdUtil; import cn.hutool.json.JSONUtil; +import com.suisung.mall.common.utils.StringUtils; import com.suisung.mall.im.common.websocket.service.DistributedMessageService; import com.suisung.mall.im.common.websocket.service.DistributedSessionService; import com.suisung.mall.im.common.websocket.service.LocalSessionManager; @@ -191,13 +192,13 @@ public class MallsuiteImSocketHandler implements WebSocketHandler { if (session.isOpen()) { session.close(); } - logger.debug("websocket connection closed......"); + logger.info("websocket connection closed......{}",session); cleanupSession(session); } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { - logger.debug("websocket connection closed......"); + logger.info("websocket connection closed......{}",session); cleanupSession(session); } @@ -207,14 +208,20 @@ public class MallsuiteImSocketHandler implements WebSocketHandler { private void cleanupSession(WebSocketSession session) { String loginUserId = String.valueOf(session.getAttributes().get("user_id")) ; String sessionId = session.getId(); - String groupId = (String) session.getAttributes().get(Constants.WEBSOCKET_GROUP_KEY); - + String groupId =""; + if(null!=session.getAttributes().get(Constants.WEBSOCKET_GROUP_KEY)){ + groupId = String.valueOf(session.getAttributes().get(Constants.WEBSOCKET_GROUP_KEY)) ; + } // 从本地管理移除 if(localSessionManager.getUserSessions()==null){ localSessionManager.setUserSessions(new ConcurrentHashMap<>()); } - localSessionManager.removeUserSession(loginUserId, session); - if (groupId != null) { + + if(StringUtils.isNotEmpty(loginUserId)){ + localSessionManager.removeUserSession(loginUserId, session); + } + + if (StringUtils.isNotEmpty(groupId)) { localSessionManager.removeGroupSession(groupId, session); }