From 03a556fe76d4705142ee856c04c1baa90099e430 Mon Sep 17 00:00:00 2001 From: limqhz <540344226@qq.com> Date: Fri, 17 Apr 2026 09:40:27 +0800 Subject: [PATCH] =?UTF-8?q?fix(netty):=20=E4=BF=AE=E5=A4=8D=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E5=BF=83=E8=B7=B3=E5=92=8C=E7=A6=BB=E7=BA=BF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将心跳间隔从60秒调整为30秒 - 修复平台ID设置顺序问题,确保context正确初始化 - 优化在线处理逻辑,支持重复连接时替换旧通道 - 更新离线方法签名,传入通道参数进行精确匹配 - 添加通道比较逻辑,避免错误的离线操作 --- .../java/com/sv/api/aop/PlatformIdAOP.java | 2 +- .../com/sv/netty/netty/ServerHandler.java | 4 +- .../netty/netty/service/MessageService.java | 3 +- .../impl/ServerMessageHandlerAdapter.java | 38 +++++++++++++------ .../java/com/sv/netty/ClientInitializer.java | 2 +- 5 files changed, 32 insertions(+), 17 deletions(-) diff --git a/api/src/main/java/com/sv/api/aop/PlatformIdAOP.java b/api/src/main/java/com/sv/api/aop/PlatformIdAOP.java index 84c2bdf..fa24f81 100644 --- a/api/src/main/java/com/sv/api/aop/PlatformIdAOP.java +++ b/api/src/main/java/com/sv/api/aop/PlatformIdAOP.java @@ -32,8 +32,8 @@ public class PlatformIdAOP { Method method = ((MethodSignature)pjp.getSignature()).getMethod(); PlatformKey platformKey = method.getAnnotation(PlatformKey.class); if(platformKey != null){ - PlatformContext.changeKey(platformKey.value()); PlatformContext.set("platform_id",1); + PlatformContext.changeKey(platformKey.value()); } NoPlatform noPlatform = method.getAnnotation(NoPlatform.class); if(noPlatform != null){ diff --git a/api/src/main/java/com/sv/netty/netty/ServerHandler.java b/api/src/main/java/com/sv/netty/netty/ServerHandler.java index 61dd3d1..3facd46 100644 --- a/api/src/main/java/com/sv/netty/netty/ServerHandler.java +++ b/api/src/main/java/com/sv/netty/netty/ServerHandler.java @@ -115,7 +115,7 @@ public class ServerHandler extends SimpleChannelInboundHandler { logger.error("Client ip [" + clientIP + "] has inactive"); Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId(); String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName(); - messageService.Offline(deviceName,venueId); + messageService.Offline(deviceName, venueId, ctx.channel()); } } @@ -133,7 +133,7 @@ public class ServerHandler extends SimpleChannelInboundHandler { ChannelParam param = channel.attr(NettyConstant.CHANNEL_PARAM).get(); // 无论是合法连接还是非法扫描发来的乱码,发生错误立马掐断连接,绝不能手软 if(channel.isActive() && param != null && param.getVenueId() != null) { - messageService.Offline(param.getDeviceName(),param.getVenueId()); + messageService.Offline(param.getDeviceName(), param.getVenueId(), channel); logger.info("{} - {} ServerHandler exceptionCaught,{}",channel.hashCode(),param.getDeviceName(),param.getVenueId(),cause); } ctx.close(); diff --git a/api/src/main/java/com/sv/netty/netty/service/MessageService.java b/api/src/main/java/com/sv/netty/netty/service/MessageService.java index 85d3474..28b9ab8 100644 --- a/api/src/main/java/com/sv/netty/netty/service/MessageService.java +++ b/api/src/main/java/com/sv/netty/netty/service/MessageService.java @@ -26,8 +26,9 @@ public interface MessageService { * * @param deviceName * @param venueId + * @param channel */ - void Offline(String deviceName, Integer venueId); + void Offline(String deviceName, Integer venueId, Channel channel); /** * 统计目前的链接数 diff --git a/api/src/main/java/com/sv/netty/netty/service/impl/ServerMessageHandlerAdapter.java b/api/src/main/java/com/sv/netty/netty/service/impl/ServerMessageHandlerAdapter.java index be4d338..3b7c99a 100644 --- a/api/src/main/java/com/sv/netty/netty/service/impl/ServerMessageHandlerAdapter.java +++ b/api/src/main/java/com/sv/netty/netty/service/impl/ServerMessageHandlerAdapter.java @@ -86,18 +86,29 @@ public class ServerMessageHandlerAdapter implements MessageService { @Override public void online(String clientId, Channel channel, HeartBeat heartBeat) { logger.info("=========" + JsonUtils.encode(heartBeat) + clientId); - // 处理心跳信息 - if (!contains(heartBeat.getDeviceName(),heartBeat.getVenueId())){ - // 此处存储客户端的channel 信息key 为 deviceId + venueId - Venue thisVenue = venueService.findById(heartBeat.getVenueId()); + String deviceName = heartBeat.getDeviceName(); + Integer venueId = heartBeat.getVenueId(); + + Channel oldChannel = getCurrentChannel(deviceName, venueId); + if (oldChannel == null) { + Venue thisVenue = venueService.findById(venueId); if (thisVenue == null ){ - logger.error("this client choose venue Error! venueId == " + heartBeat.getVenueId()); + logger.error("this client choose venue Error! venueId == " + venueId); } else { - deviceService.online(heartBeat.getDeviceName(),heartBeat.getVenueId(),thisVenue.getType(),clientId); - addLinks(heartBeat.getDeviceName(),heartBeat.getVenueId(),channel); -// VenueMessage venueMessage = new VenueMessage(MessageType.LINK,"欢迎扫码进场!"); -// ServerMessageUtils.INSTANCE.sendMsg(channel,venueMessage); + deviceService.online(deviceName, venueId, thisVenue.getType(), clientId); + addLinks(deviceName, venueId, channel); } + } else if (oldChannel != channel) { + logger.info("New connection reporting heartbeat for device {}_{}, overwriting old channel.", deviceName, venueId); + addLinks(deviceName, venueId, channel); + // 也可能需要更新一下 IP + Venue thisVenue = venueService.findById(venueId); + if (thisVenue != null) { + deviceService.online(deviceName, venueId, thisVenue.getType(), clientId); + } + try { + oldChannel.close(); + } catch (Exception e) {} } } @@ -107,10 +118,13 @@ public class ServerMessageHandlerAdapter implements MessageService { * @param venueId */ @Override - public void Offline(String deviceName, Integer venueId) { + public void Offline(String deviceName, Integer venueId, Channel channel) { if (deviceName != null && venueId != null){ - removeChannelType(deviceName,venueId); - deviceService.offline(deviceName,venueId); + Channel currentChannel = getCurrentChannel(deviceName, venueId); + if (currentChannel == null || currentChannel == channel) { + removeChannelType(deviceName, venueId); + deviceService.offline(deviceName, venueId); + } } } diff --git a/netty-client/src/main/java/com/sv/netty/ClientInitializer.java b/netty-client/src/main/java/com/sv/netty/ClientInitializer.java index 111026b..5c585f4 100644 --- a/netty-client/src/main/java/com/sv/netty/ClientInitializer.java +++ b/netty-client/src/main/java/com/sv/netty/ClientInitializer.java @@ -12,7 +12,7 @@ import io.netty.handler.timeout.IdleStateHandler; public class ClientInitializer extends ChannelInitializer { - private final static int TIME_HEART_BEAT = 60; + private final static int TIME_HEART_BEAT = 30; public ClientThread.ReConnectHandler reConnectHandler; public ClientHandler dmClientHandler;