From 977ab4bf1b6f55c74ed81e5341bd61c3d605ea65 Mon Sep 17 00:00:00 2001 From: chendaofei <857448963@qq.com> Date: Thu, 19 Jun 2025 14:33:28 +0800 Subject: [PATCH] =?UTF-8?q?socket=E9=80=9A=E8=AE=AF=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../util/socket/cilent/NettyClient.java | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyClient.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyClient.java index f3550cd4..3cb48661 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyClient.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyClient.java @@ -73,29 +73,28 @@ public class NettyClient { channelFuture.addListener((ChannelFutureListener) ch -> { if (!ch.isSuccess()) { System.out.println("链接服务端失败..."); + // 连接失败时关闭 group + group.shutdownGracefully(); } else { System.out.println("链接服务端成功..."); + if (handler instanceof NettySourceClientHandler) { + NioEventLoopGroup groupByUserId = SocketManager.getGroupByUserId(param.getUserPageId() + source); + if(ObjectUtil.isNotNull(groupByUserId)){ + groupByUserId.shutdownGracefully().sync(); + } + SocketManager.addGroup(param.getUserPageId() + source, group); + } else { + NioEventLoopGroup groupByUserId = SocketManager.getGroupByUserId(param.getUserPageId() + dev); + if(ObjectUtil.isNotNull(groupByUserId)){ + groupByUserId.shutdownGracefully().sync(); + } + SocketManager.addGroup(param.getUserPageId() + dev, group); + } + System.out.println("客户端向服务端发送消息:" + port + msg); - channelFuture.channel().writeAndFlush(msg + "\n").sync(); + channelFuture.channel().writeAndFlush(msg + "\n"); } }); - - if (handler instanceof NettySourceClientHandler) { - NioEventLoopGroup groupByUserId = SocketManager.getGroupByUserId(param.getUserPageId() + source); - if(ObjectUtil.isNotNull(groupByUserId)){ - groupByUserId.shutdownGracefully().sync(); - } - SocketManager.addGroup(param.getUserPageId() + source, group); - } else { - NioEventLoopGroup groupByUserId = SocketManager.getGroupByUserId(param.getUserPageId() + dev); - if(ObjectUtil.isNotNull(groupByUserId)){ - groupByUserId.shutdownGracefully().sync(); - } - SocketManager.addGroup(param.getUserPageId() + dev, group); - } - - - } catch (Exception e) { System.out.println("连接socket服务端发送异常............" + e.getMessage()); group.shutdownGracefully();