This commit is contained in:
wr
2024-12-19 10:13:59 +08:00
parent 1e7647dadc
commit 7d88776633
7 changed files with 48 additions and 5 deletions

View File

@@ -17,6 +17,7 @@ public enum SourceOperateCodeEnum {
INIT_GATHER("INIT_GATHER", "源初始化"),
OPER_GATHER("OPER_GATHER", "源输出"),
CLOSE_GATHER("CLOSE_GATHER", "源停止"),
HEARTBEAT("HeartBeat", "心跳报文"),
/**
* 终端 INIT_GATHER$01 INIT_GATHER采集初始化01 统计采集、02 暂态采集、03 实时采集
@@ -30,6 +31,8 @@ public enum SourceOperateCodeEnum {
DEV_DATA_REQUEST_03("DATA_REQUEST$03", "暂态采集申请"),
YJC_YTXJY("yjc_ytxjy", "预检测_源通讯检测"),
YJC_SBTXJY("yjc_sbtxjy", "预检测_设备通讯检测"),
YJC_XYJY("yjc_xyjy", "预检测_协议校验"),

View File

@@ -48,7 +48,7 @@ public class NettyClient {
.handler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) {
if(port==10086){
if(handler instanceof NettySourceClientHandler){
ch.pipeline()
//空闲状态的handler
// 添加LineBasedFrameDecoder来按行分割数据

View File

@@ -3,11 +3,8 @@ 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;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.TimeoutException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;

View File

@@ -5,8 +5,12 @@ import com.njcn.gather.detection.pojo.param.PreDetectionParam;
import com.njcn.gather.detection.util.socket.SocketManager;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.TimeoutException;
import lombok.RequiredArgsConstructor;
import java.io.IOException;
import java.net.ConnectException;
import java.net.ProtocolException;
/**
@@ -85,7 +89,30 @@ public class NettySourceClientHandler extends SimpleChannelInboundHandler<Strin
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// 处理异常,例如记录日志、关闭连接等
cause.printStackTrace();
// 根据异常类型进行不同的处理
if (cause instanceof ConnectException) {
// 处理连接异常,例如重试连接或记录特定的连接错误信息
System.out.println("连接socket服务端异常");
} else if (cause instanceof IOException) {
// 处理I/O异常例如读写错误
System.out.println("IOException caught: There was an I/O error.");
// 例如可以记录更详细的I/O错误信息
} else if (cause instanceof TimeoutException) {
// 处理超时异常
System.out.println("TimeoutException caught: Operation timed out.");
// 可以根据业务逻辑决定是否重试或记录超时信息
} else if (cause instanceof ProtocolException) {
// 处理协议异常,例如消息格式不正确
System.out.println("ProtocolException caught: Invalid protocol message.");
// 可以记录协议错误信息或向客户端发送错误响应
} else {
// 处理其他类型的异常
System.out.println("Unknown exception caught: " + cause.getMessage());
// 可以记录未知异常信息
}
ctx.close();
}