From 5529093453474d071c73d812810a8f52a436f9df Mon Sep 17 00:00:00 2001 From: chendaofei <857448963@qq.com> Date: Wed, 18 Dec 2024 10:40:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/SocketDevResponseService.java | 13 ++++++++- .../handler/SocketSourceResponseService.java | 2 +- .../socket/cilent/NettyDevClientHandler.java | 24 +++------------- .../socket/service/NettyServerHandler.java | 28 +++++++++++++++++-- 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/detection/src/main/java/com/njcn/gather/detection/handler/SocketDevResponseService.java b/detection/src/main/java/com/njcn/gather/detection/handler/SocketDevResponseService.java index 0b17c813..4174ea98 100644 --- a/detection/src/main/java/com/njcn/gather/detection/handler/SocketDevResponseService.java +++ b/detection/src/main/java/com/njcn/gather/detection/handler/SocketDevResponseService.java @@ -35,9 +35,10 @@ public class SocketDevResponseService { - public void deal(String userId,String msg){ + public void deal(PreDetectionParam param,String msg){ SocketDataMsg socketDataMsg = MsgUtil.socketDataMsg(msg); SourceOperateCodeEnum sourceOperateCodeEnum = SourceOperateCodeEnum.getDictDataEnumByCode(socketDataMsg.getRequestId()); + switch (sourceOperateCodeEnum) { case YJC_SBTXJY: @@ -56,6 +57,16 @@ public class SocketDevResponseService { + private void devComm(SocketDataMsg socketDataMsg,PreDetectionParam param){ + SourceResponseCodeEnum dictDataEnumByCode = SourceResponseCodeEnum.getDictDataEnumByCode(socketDataMsg.getCode()); + switch (dictDataEnumByCode){ + + } + } + + + + 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 1a90d6c5..8ea3d125 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 @@ -81,7 +81,7 @@ public class SocketSourceResponseService { webSocketHandler.sendMsgToUser(param.getUserPageId(), msg); String s = param.getUserPageId() + "_dev"; //开始设备通讯检测 - NettyClient.socketClient(ip, port, param.getUserPageId(), new NettyDevClientHandler(s, socketDevResponseService)); + 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); 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 a10861c3..f8ca5791 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 @@ -1,6 +1,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 com.njcn.gather.detection.util.socket.WebServiceManager; import io.netty.channel.ChannelHandlerContext; @@ -25,7 +26,7 @@ import java.net.ProtocolException; @RequiredArgsConstructor public class NettyDevClientHandler extends SimpleChannelInboundHandler { - private final String webUser; + private final PreDetectionParam param; private final SocketDevResponseService socketResponseService; @@ -51,7 +52,7 @@ public class NettyDevClientHandler extends SimpleChannelInboundHandler { @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws InterruptedException { System.out.println("devhandler接收server端数据>>>>>>"+msg); - socketResponseService.deal(webUser,msg); + socketResponseService.deal(param,msg); } @@ -75,29 +76,12 @@ public class NettyDevClientHandler extends SimpleChannelInboundHandler { @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { //当连接超过10S和发送消息后10S无响应时候,关闭channel - if (evt instanceof IdleStateEvent) { - IdleStateEvent idleEvent = (IdleStateEvent) evt; - if (idleEvent.state() == IdleState.READER_IDLE) { - System.out.println("读空闲超时!"); - // 处理读空闲超时逻辑,例如关闭连接或发送心跳包 - ctx.close(); - WebServiceManager.sendMsg(webUser,"读取socket服务端等待时长超过10s,自动中断连接"); - SocketManager.removeUser(webUser); - } - // 可以添加对WRITER_IDLE和ALL_IDLE的处理逻辑 - } else { - try { - super.userEventTriggered(ctx, evt); - } catch (Exception e) { - throw new RuntimeException(e); - } - } } @Override public void handlerAdded(ChannelHandlerContext ctx) { - SocketManager.addUser(webUser+"_dev",ctx.channel()); + SocketManager.addUser(param+"_dev",ctx.channel()); System.out.println("有通道接入" + ctx.channel()); } diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/service/NettyServerHandler.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/service/NettyServerHandler.java index 92a5aca9..dd143b70 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/service/NettyServerHandler.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/service/NettyServerHandler.java @@ -1,5 +1,6 @@ package com.njcn.gather.detection.util.socket.service; +import com.alibaba.fastjson.JSONObject; import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; @@ -10,7 +11,7 @@ import io.netty.channel.SimpleChannelInboundHandler; * @Author: wr * @Date: 2024/12/10 14:18 */ -@ChannelHandler.Sharable + public class NettyServerHandler extends SimpleChannelInboundHandler { public static final NettyServerHandler INSTANCE = new NettyServerHandler(); @@ -22,6 +23,7 @@ public class NettyServerHandler extends SimpleChannelInboundHandler { */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { + System.out.println("服务端监听到"+ctx.channel().id()+"连接"); super.channelActive(ctx); } @@ -30,8 +32,9 @@ public class NettyServerHandler extends SimpleChannelInboundHandler { */ @Override public void channelRead0(ChannelHandlerContext ctx, String msg) { + System.out.println(ctx.channel().id()+"NettyServer服务端接收到客户端消息:"+msg); Channel channel = ctx.channel(); - System.out.println("socket客户端接收成功:"+msg); + /* if(msg.endsWith("结束")) { channel.writeAndFlush("socket指令结果:"+msg); for (int i = 0; i < 5; i++) { @@ -39,7 +42,11 @@ public class NettyServerHandler extends SimpleChannelInboundHandler { } }else{ channel.writeAndFlush("socket指令结果:成功指令"); - } + }*/ + JSONObject jsonObject = new JSONObject(); + jsonObject.put("requestId","yjc_ytxjy"); + jsonObject.put("code","10200"); + channel.writeAndFlush(jsonObject.toJSONString()+'\n'); } @Override @@ -47,6 +54,21 @@ public class NettyServerHandler extends SimpleChannelInboundHandler { System.out.println("有新连接加入了++++......" + ctx.channel()); } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + cause.printStackTrace(); + ctx.close(); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + // Client disconnected, close the server-side channel too + System.out.println("NettyServer服务端监听到"+ctx.channel().id()+"关闭连接"); + ctx.close(); // This will trigger the close event on the server side + super.channelInactive(ctx); + } + /** * 用户事件的回调方法(自定义事件用于心跳机制) */