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();