对话session移除问题修复

This commit is contained in:
liyj 2025-11-24 15:03:42 +08:00
parent 32bd2fab91
commit 3802044eea

View File

@ -3,6 +3,7 @@ package com.suisung.mall.im.common.websocket.service.onchat;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil; 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.DistributedMessageService;
import com.suisung.mall.im.common.websocket.service.DistributedSessionService; import com.suisung.mall.im.common.websocket.service.DistributedSessionService;
import com.suisung.mall.im.common.websocket.service.LocalSessionManager; import com.suisung.mall.im.common.websocket.service.LocalSessionManager;
@ -191,13 +192,13 @@ public class MallsuiteImSocketHandler implements WebSocketHandler {
if (session.isOpen()) { if (session.isOpen()) {
session.close(); session.close();
} }
logger.debug("websocket connection closed......"); logger.info("websocket connection closed......{}",session);
cleanupSession(session); cleanupSession(session);
} }
@Override @Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
logger.debug("websocket connection closed......"); logger.info("websocket connection closed......{}",session);
cleanupSession(session); cleanupSession(session);
} }
@ -207,14 +208,20 @@ public class MallsuiteImSocketHandler implements WebSocketHandler {
private void cleanupSession(WebSocketSession session) { private void cleanupSession(WebSocketSession session) {
String loginUserId = String.valueOf(session.getAttributes().get("user_id")) ; String loginUserId = String.valueOf(session.getAttributes().get("user_id")) ;
String sessionId = session.getId(); 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){ if(localSessionManager.getUserSessions()==null){
localSessionManager.setUserSessions(new ConcurrentHashMap<>()); 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); localSessionManager.removeGroupSession(groupId, session);
} }