测试提交
This commit is contained in:
@@ -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);
|
SocketDataMsg socketDataMsg = MsgUtil.socketDataMsg(msg);
|
||||||
SourceOperateCodeEnum sourceOperateCodeEnum = SourceOperateCodeEnum.getDictDataEnumByCode(socketDataMsg.getRequestId());
|
SourceOperateCodeEnum sourceOperateCodeEnum = SourceOperateCodeEnum.getDictDataEnumByCode(socketDataMsg.getRequestId());
|
||||||
|
|
||||||
switch (sourceOperateCodeEnum) {
|
switch (sourceOperateCodeEnum) {
|
||||||
case YJC_SBTXJY:
|
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){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ public class SocketSourceResponseService {
|
|||||||
webSocketHandler.sendMsgToUser(param.getUserPageId(), msg);
|
webSocketHandler.sendMsgToUser(param.getUserPageId(), msg);
|
||||||
String s = param.getUserPageId() + "_dev";
|
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<PreDetection> devList = iPqDevService.getDevInfo(param.getDevIds());
|
List<PreDetection> devList = iPqDevService.getDevInfo(param.getDevIds());
|
||||||
Map<String, List<PreDetection>> map = new HashMap(1);
|
Map<String, List<PreDetection>> map = new HashMap(1);
|
||||||
map.put("deviceList", devList);
|
map.put("deviceList", devList);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.njcn.gather.detection.util.socket.cilent;
|
package com.njcn.gather.detection.util.socket.cilent;
|
||||||
|
|
||||||
import com.njcn.gather.detection.handler.SocketDevResponseService;
|
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.SocketManager;
|
||||||
import com.njcn.gather.detection.util.socket.WebServiceManager;
|
import com.njcn.gather.detection.util.socket.WebServiceManager;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
@@ -25,7 +26,7 @@ import java.net.ProtocolException;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class NettyDevClientHandler extends SimpleChannelInboundHandler<String> {
|
public class NettyDevClientHandler extends SimpleChannelInboundHandler<String> {
|
||||||
|
|
||||||
private final String webUser;
|
private final PreDetectionParam param;
|
||||||
|
|
||||||
private final SocketDevResponseService socketResponseService;
|
private final SocketDevResponseService socketResponseService;
|
||||||
|
|
||||||
@@ -51,7 +52,7 @@ public class NettyDevClientHandler extends SimpleChannelInboundHandler<String> {
|
|||||||
@Override
|
@Override
|
||||||
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws InterruptedException {
|
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws InterruptedException {
|
||||||
System.out.println("devhandler接收server端数据>>>>>>"+msg);
|
System.out.println("devhandler接收server端数据>>>>>>"+msg);
|
||||||
socketResponseService.deal(webUser,msg);
|
socketResponseService.deal(param,msg);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -75,29 +76,12 @@ public class NettyDevClientHandler extends SimpleChannelInboundHandler<String> {
|
|||||||
@Override
|
@Override
|
||||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
|
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
|
||||||
//当连接超过10S和发送消息后10S无响应时候,关闭channel
|
//当连接超过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
|
@Override
|
||||||
public void handlerAdded(ChannelHandlerContext ctx) {
|
public void handlerAdded(ChannelHandlerContext ctx) {
|
||||||
SocketManager.addUser(webUser+"_dev",ctx.channel());
|
SocketManager.addUser(param+"_dev",ctx.channel());
|
||||||
System.out.println("有通道接入" + ctx.channel());
|
System.out.println("有通道接入" + ctx.channel());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.njcn.gather.detection.util.socket.service;
|
package com.njcn.gather.detection.util.socket.service;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
import io.netty.channel.ChannelHandler;
|
import io.netty.channel.ChannelHandler;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
@@ -10,7 +11,7 @@ import io.netty.channel.SimpleChannelInboundHandler;
|
|||||||
* @Author: wr
|
* @Author: wr
|
||||||
* @Date: 2024/12/10 14:18
|
* @Date: 2024/12/10 14:18
|
||||||
*/
|
*/
|
||||||
@ChannelHandler.Sharable
|
|
||||||
public class NettyServerHandler extends SimpleChannelInboundHandler<String> {
|
public class NettyServerHandler extends SimpleChannelInboundHandler<String> {
|
||||||
|
|
||||||
public static final NettyServerHandler INSTANCE = new NettyServerHandler();
|
public static final NettyServerHandler INSTANCE = new NettyServerHandler();
|
||||||
@@ -22,6 +23,7 @@ public class NettyServerHandler extends SimpleChannelInboundHandler<String> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
System.out.println("服务端监听到"+ctx.channel().id()+"连接");
|
||||||
super.channelActive(ctx);
|
super.channelActive(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,8 +32,9 @@ public class NettyServerHandler extends SimpleChannelInboundHandler<String> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void channelRead0(ChannelHandlerContext ctx, String msg) {
|
public void channelRead0(ChannelHandlerContext ctx, String msg) {
|
||||||
|
System.out.println(ctx.channel().id()+"NettyServer服务端接收到客户端消息:"+msg);
|
||||||
Channel channel = ctx.channel();
|
Channel channel = ctx.channel();
|
||||||
System.out.println("socket客户端接收成功:"+msg);
|
/*
|
||||||
if(msg.endsWith("结束")) {
|
if(msg.endsWith("结束")) {
|
||||||
channel.writeAndFlush("socket指令结果:"+msg);
|
channel.writeAndFlush("socket指令结果:"+msg);
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
@@ -39,7 +42,11 @@ public class NettyServerHandler extends SimpleChannelInboundHandler<String> {
|
|||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
channel.writeAndFlush("socket指令结果:成功指令");
|
channel.writeAndFlush("socket指令结果:成功指令");
|
||||||
}
|
}*/
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
jsonObject.put("requestId","yjc_ytxjy");
|
||||||
|
jsonObject.put("code","10200");
|
||||||
|
channel.writeAndFlush(jsonObject.toJSONString()+'\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -47,6 +54,21 @@ public class NettyServerHandler extends SimpleChannelInboundHandler<String> {
|
|||||||
System.out.println("有新连接加入了++++......" + ctx.channel());
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户事件的回调方法(自定义事件用于心跳机制)
|
* 用户事件的回调方法(自定义事件用于心跳机制)
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user