From d37d44123f8b15d9c737286147fe33bb821ab673 Mon Sep 17 00:00:00 2001 From: chendaofei <857448963@qq.com> Date: Thu, 19 Dec 2024 20:39:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/SocketSourceResponseService.java | 8 +++----- .../detection/pojo/enums/SourceResponseCodeEnum.java | 2 +- .../njcn/gather/detection/pojo/vo/WebSocketVO.java | 4 ++-- .../service/impl/PreDetectionServiceImpl.java | 12 ++++++------ .../detection/util/socket/cilent/NettyClient.java | 6 ++++-- .../util/socket/cilent/NettyDevClientHandler.java | 8 +++++++- .../util/socket/cilent/NettySourceClientHandler.java | 12 +++++++++--- .../service/NettyServerChannelInitializer.java | 2 +- 8 files changed, 33 insertions(+), 21 deletions(-) diff --git a/detection/src/main/java/com/njcn/gather/detection/handler/SocketSourceResponseService.java b/detection/src/main/java/com/njcn/gather/detection/handler/SocketSourceResponseService.java index 9b355acf..fd56def4 100644 --- a/detection/src/main/java/com/njcn/gather/detection/handler/SocketSourceResponseService.java +++ b/detection/src/main/java/com/njcn/gather/detection/handler/SocketSourceResponseService.java @@ -78,10 +78,7 @@ public class SocketSourceResponseService { webSocketHandler.sendMsgToUser(param.getUserPageId(), "msg"); String s = param.getUserPageId() + "_Dev"; //开始设备通讯检测(发送设备初始化) - Channel channel = SocketManager.getChannelByUserId(s); - if(channel==null || !channel.isActive()){ - NettyClient.socketClient(ip, port, param.getUserPageId(), new NettyDevClientHandler(param, socketDevResponseService)); - } + List devList = iPqDevService.getDevInfo(param.getDevIds()); Map> map = new HashMap(1); map.put("deviceList", devList); @@ -90,7 +87,8 @@ public class SocketSourceResponseService { socketMsg.setOperateCode(SourceOperateCodeEnum.DEV_INIT_GATHER_01.getValue()); socketMsg.setData(jsonString); String json = JSON.toJSONString(socketMsg); - SocketManager.sendMsg(s,json); + NettyClient.socketClient(ip, port, param.getUserPageId(),json, new NettyDevClientHandler(param, socketDevResponseService)); + // SocketManager.sendMsg(s,json); break; case UNPROCESSED_BUSINESS: break; diff --git a/detection/src/main/java/com/njcn/gather/detection/pojo/enums/SourceResponseCodeEnum.java b/detection/src/main/java/com/njcn/gather/detection/pojo/enums/SourceResponseCodeEnum.java index e41cf4a8..80a4ae96 100644 --- a/detection/src/main/java/com/njcn/gather/detection/pojo/enums/SourceResponseCodeEnum.java +++ b/detection/src/main/java/com/njcn/gather/detection/pojo/enums/SourceResponseCodeEnum.java @@ -34,7 +34,7 @@ public enum SourceResponseCodeEnum { NO_INIT_DEV(10556,"不存在上线的设备"), //自定义前端展示消息 - SOCKET_ERROR(20000,"连接服务端失败") + SOCKET_ERROR(20000,"服务端连接失败") diff --git a/detection/src/main/java/com/njcn/gather/detection/pojo/vo/WebSocketVO.java b/detection/src/main/java/com/njcn/gather/detection/pojo/vo/WebSocketVO.java index 0eded4df..2bd79d7a 100644 --- a/detection/src/main/java/com/njcn/gather/detection/pojo/vo/WebSocketVO.java +++ b/detection/src/main/java/com/njcn/gather/detection/pojo/vo/WebSocketVO.java @@ -2,10 +2,10 @@ package com.njcn.gather.detection.pojo.vo; import com.njcn.gather.detection.pojo.enums.SourceResponseCodeEnum; import lombok.Data; -import org.apache.poi.ss.formula.functions.T; + @Data -public class WebSocketVO { +public class WebSocketVO { private Integer code; diff --git a/detection/src/main/java/com/njcn/gather/detection/service/impl/PreDetectionServiceImpl.java b/detection/src/main/java/com/njcn/gather/detection/service/impl/PreDetectionServiceImpl.java index 4799371d..08c047df 100644 --- a/detection/src/main/java/com/njcn/gather/detection/service/impl/PreDetectionServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/detection/service/impl/PreDetectionServiceImpl.java @@ -101,14 +101,14 @@ public class PreDetectionServiceImpl implements PreDetectionService { Channel channel = SocketManager.getChannelByUserId(s); socketDevResponseService.xuClear(); socketDevResponseService.initList(param); - if(channel==null || !channel.isActive()){ - NettyClient.socketClient(ip, port, param.getUserPageId(), new NettySourceClientHandler(param, sourceResponseService)); - } + msg= new SocketMsg(); msg.setRequestId(SourceOperateCodeEnum.YJC_YTXJY.getValue()); msg.setOperateCode(SourceOperateCodeEnum.INIT_GATHER.getValue()); msg.setData(JSON.toJSONString(sourceParam)); - SocketManager.sendMsg(s, JSON.toJSONString(msg)); + NettyClient.socketClient(ip, port, param.getUserPageId(), JSON.toJSONString(msg),new NettySourceClientHandler(param, sourceResponseService)); + + } else { throw new BusinessException(DetectionResponseEnum.SOURCE_INFO_NOT); } @@ -129,9 +129,9 @@ public class PreDetectionServiceImpl implements PreDetectionService { String ddId = param.getUserPageId() + "_Source"; Channel channel = SocketManager.getChannelByUserId(ddId); if (channel == null || !channel.isActive()) { - NettyClient.socketClient(ip, port, param.getUserPageId(), new NettySourceClientHandler(param, sourceResponseService)); + NettyClient.socketClient(ip, port, param.getUserPageId(),"start\n", new NettySourceClientHandler(param, sourceResponseService)); } - SocketManager.sendMsg(ddId, "start\n"); + } }; runnable.run(); 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 6d4d45d2..af204067 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 @@ -39,7 +39,7 @@ import java.util.concurrent.TimeUnit; @Getter public class NettyClient { - public static void socketClient(String ip, Integer port, String userPageId, SimpleChannelInboundHandler handler) { + public static void socketClient(String ip, Integer port, String userPageId,String msg, SimpleChannelInboundHandler handler) { NioEventLoopGroup group = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap(); try { @@ -70,11 +70,13 @@ public class NettyClient { } }); - bootstrap.connect(ip, port).sync().addListener((ChannelFutureListener) ch -> { + ChannelFuture channelFuture = bootstrap.connect(ip, port).sync(); + channelFuture.addListener((ChannelFutureListener) ch -> { if (!ch.isSuccess()) { System.out.println("链接服务端失败..."); } else { System.out.println("链接服务端成功..."); + channelFuture.channel().writeAndFlush(msg); } }); } catch (Exception e) { diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyDevClientHandler.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyDevClientHandler.java index 85332878..84a25ee6 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyDevClientHandler.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyDevClientHandler.java @@ -3,6 +3,7 @@ package com.njcn.gather.detection.util.socket.cilent; import com.njcn.gather.detection.handler.SocketDevResponseService; import com.njcn.gather.detection.pojo.param.PreDetectionParam; import com.njcn.gather.detection.util.socket.SocketManager; +import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.timeout.TimeoutException; @@ -12,6 +13,7 @@ import org.springframework.beans.factory.annotation.Value; import java.io.IOException; import java.net.ConnectException; import java.net.ProtocolException; +import java.util.Objects; /** @@ -40,7 +42,11 @@ public class NettyDevClientHandler extends SimpleChannelInboundHandler { */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - System.out.println("客户端通道已建立" + ctx.channel().remoteAddress()); + System.out.println("客户端通道已建立" + ctx.channel().id()); + Channel channel = SocketManager.getChannelByUserId(param.getUserPageId()+"_Dev"); + if(Objects.nonNull(channel)){ + channel.close(); + } SocketManager.addUser(param.getUserPageId()+"_Dev",ctx.channel()); } diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettySourceClientHandler.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettySourceClientHandler.java index be39395f..8821d2ff 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettySourceClientHandler.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettySourceClientHandler.java @@ -6,6 +6,7 @@ import com.njcn.gather.detection.pojo.enums.SourceOperateCodeEnum; import com.njcn.gather.detection.pojo.param.PreDetectionParam; import com.njcn.gather.detection.pojo.vo.SocketMsg; import com.njcn.gather.detection.util.socket.SocketManager; +import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.timeout.IdleState; @@ -16,6 +17,7 @@ import lombok.RequiredArgsConstructor; import java.io.IOException; import java.net.ConnectException; import java.net.ProtocolException; +import java.util.Objects; /** @@ -36,9 +38,13 @@ public class NettySourceClientHandler extends SimpleChannelInboundHandler