【fix】

This commit is contained in:
bug-fix
2024-10-31 20:48:38 +08:00
parent 810579178f
commit aa056dbe07
8 changed files with 48 additions and 37 deletions

View File

@ -26,6 +26,10 @@ public class NettyServerHandler extends SimpleChannelInboundHandler<NettyProxyMs
* 空闲次数
*/
private int idle_count = 1;
/**
* 传出数据延迟次数* 心跳时间作为关闭时间
*/
private int transfer_count = 1;
public NettyServerHandler(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter;
@ -60,21 +64,22 @@ public class NettyServerHandler extends SimpleChannelInboundHandler<NettyProxyMs
if (IdleState.READER_IDLE.equals(event.state())) { //如果读通道处于空闲状态,说明没有接收到心跳命令
String clientId = ChannelAttributeKeyUtils.getClientId(channel);
String visitorId = ChannelAttributeKeyUtils.getVisitorId(channel);
// 已经5秒没有接收到客户端{}的信息了
log.warn("I haven't received any information from client: {} for 5 seconds", clientId);
if (idle_count > 2) {
if (ObjectUtils.isEmpty(visitorId)) {
// 关闭这个不活跃的channel client:{}
log.warn("close this inactive channel client:{} with no visitor", clientId);
// 给所有客户端发送 这个客户端离线了
NettyProxyMsg nettyMsg = new NettyProxyMsg();
nettyMsg.setClientId(clientId);
nettyMsg.setType(MessageType.REPORT_CLIENT_DISCONNECTION);
channelTypeAdapter.handler(channel, nettyMsg);
channel.close();
} else {
log.warn("close client:{} visitor: [{}]'s connection",clientId, visitorId);
if (ObjectUtils.isEmpty(visitorId)) {
// 已经5秒没有接收到客户端{}的信息了
log.warn("I haven't received any information from client: {} with channel:{} for 5 seconds", clientId, channel.id().toString());
// 关闭这个不活跃的channel client:{}
log.warn("close this inactive channel client:{} with no visitor", clientId);
// 给所有客户端发送 这个客户端离线了
NettyProxyMsg nettyMsg = new NettyProxyMsg();
nettyMsg.setClientId(clientId);
nettyMsg.setType(MessageType.REPORT_CLIENT_DISCONNECTION);
channelTypeAdapter.handler(channel, nettyMsg);
channel.close();
} else {
// 访客通道数据 5*100秒后关闭
if (transfer_count > 100) {
log.warn("close client:{} visitor: [{}]'s connection", clientId, visitorId);
NettyCommunicationIdContext.clear(visitorId);
NettyRealIdContext.clear(visitorId);
// 关闭通信通道
@ -84,9 +89,9 @@ public class NettyServerHandler extends SimpleChannelInboundHandler<NettyProxyMs
nextChannel.close();
transferNextChannel.close();
}
transfer_count++;
}
idle_count++;
}
} else {
super.userEventTriggered(ctx, obj);
@ -113,7 +118,7 @@ public class NettyServerHandler extends SimpleChannelInboundHandler<NettyProxyMs
if (!ObjectUtils.isEmpty(visitorId)) {
// 客户端:{},断开访客的连接:{}
log.warn("Client: {}, disconnect with visitorId:{}", clientId, visitorId);
log.warn("client: {} channel:{}, disconnect with visitorId:{}", clientId, channel.id().toString(), visitorId);
// 访客通道 关闭访客通道
NettyCommunicationIdContext.clear(visitorId);
// 关闭通信通道