webSocket清除
This commit is contained in:
@@ -87,6 +87,16 @@ public class WebServiceManager {
|
|||||||
return userSessions.remove(userId);
|
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移除会话(兼容老版本)
|
* 根据channelId移除会话(兼容老版本)
|
||||||
* 时间复杂度:O(n),建议使用removeByUserId替代
|
* 时间复杂度:O(n),建议使用removeByUserId替代
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketF
|
|||||||
public void handlerRemoved(ChannelHandlerContext ctx) {
|
public void handlerRemoved(ChannelHandlerContext ctx) {
|
||||||
log.info("webSocket客户端退出,channelId: {}, userId: {}", ctx.channel().id(), this.userId);
|
log.info("webSocket客户端退出,channelId: {}, userId: {}", ctx.channel().id(), this.userId);
|
||||||
if (this.userId != null) {
|
if (this.userId != null) {
|
||||||
WebServiceManager.removeByUserId(this.userId);
|
WebServiceManager.removeByUserId(this.userId, ctx.channel());
|
||||||
} else {
|
} else {
|
||||||
// 备用方案:如果userId为空,使用传统方法
|
// 备用方案:如果userId为空,使用传统方法
|
||||||
WebServiceManager.removeChannel(ctx.channel().id().toString());
|
WebServiceManager.removeChannel(ctx.channel().id().toString());
|
||||||
@@ -168,7 +168,7 @@ public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketF
|
|||||||
log.error("客户端心跳检测空闲次数超过{}次,关闭连接,channelId: {}, userId: {}", MAX_HEARTBEAT_MISS_COUNT, ctx.channel().id(), this.userId);
|
log.error("客户端心跳检测空闲次数超过{}次,关闭连接,channelId: {}, userId: {}", MAX_HEARTBEAT_MISS_COUNT, ctx.channel().id(), this.userId);
|
||||||
ctx.channel().close();
|
ctx.channel().close();
|
||||||
if (this.userId != null) {
|
if (this.userId != null) {
|
||||||
WebServiceManager.removeByUserId(this.userId);
|
WebServiceManager.removeByUserId(this.userId, ctx.channel());
|
||||||
} else {
|
} else {
|
||||||
WebServiceManager.removeChannel(ctx.channel().id().toString());
|
WebServiceManager.removeChannel(ctx.channel().id().toString());
|
||||||
}
|
}
|
||||||
@@ -326,7 +326,7 @@ public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketF
|
|||||||
|
|
||||||
// 清理会话
|
// 清理会话
|
||||||
if (this.userId != null) {
|
if (this.userId != null) {
|
||||||
WebServiceManager.removeByUserId(this.userId);
|
WebServiceManager.removeByUserId(this.userId, ctx.channel());
|
||||||
log.debug("已清理WebSocket会话,userId: {}, channelId: {}", this.userId, channelId);
|
log.debug("已清理WebSocket会话,userId: {}, channelId: {}", this.userId, channelId);
|
||||||
} else {
|
} else {
|
||||||
WebServiceManager.removeChannel(channelId);
|
WebServiceManager.removeChannel(channelId);
|
||||||
|
|||||||
Reference in New Issue
Block a user