管道,流程,数据源切换,大屏,测量点位

This commit is contained in:
Timer
2026-03-08 04:08:33 +08:00
parent b79f345ecc
commit 28e1b2a9f1
21 changed files with 605 additions and 207 deletions

View File

@ -5,10 +5,18 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Date;
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
private NettyClient nettyClient;
public NettyClientHandler(NettyClient nettyClient) {
this.nettyClient = nettyClient;
}
/**
* 通道信息读取处理
* @param ctx
@ -25,6 +33,8 @@ public class NettyClientHandler extends ChannelInboundHandlerAdapter {
* 给服务端回复消息
*/
ctx.writeAndFlush("客户端收到! 消息为: " + m.toString(Charset.defaultCharset()));
} catch (Exception e) {
System.err.println("读取消息异常: " + e.getMessage());
} finally {
m.release();
}
@ -42,15 +52,56 @@ public class NettyClientHandler extends ChannelInboundHandlerAdapter {
* 消息格式必须是ByteBuf才行!!!!!
* 如果是其他格式服务端是接收不到的!!!!
*/
System.out.println("首次连接完成!");
String helo = "你好呀!";
ByteBuf byteBuf = Unpooled.wrappedBuffer(helo.getBytes());
ctx.channel().writeAndFlush(byteBuf);
System.out.println("首次连接完成!");
}
/**
* 当连接断开时调用
* @param ctx
* @throws Exception
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("连接已断开,准备重连...");
if (nettyClient != null) {
nettyClient.reconnect();
}
}
/**
* 异常处理
* @param ctx
* @param cause
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
// 处理远程主机强制关闭连接的异常
if (cause instanceof IOException) {
String message = cause.getMessage();
if (message != null && (message.contains("远程主机强迫关闭了一个现有的连接")
|| message.contains("Connection reset")
|| message.contains("远程主机强迫关闭")
|| message.contains("An existing connection was forcibly closed"))) {
System.out.println("[" + java.time.LocalDateTime.now() + "] 远程主机已关闭连接,等待重连...");
ctx.close();
// 触发重连
if (nettyClient != null) {
nettyClient.reconnect();
}
return;
}
}
// 其他异常只打印简要信息,不打印完整堆栈
System.err.println("[" + java.time.LocalDateTime.now() + "] 客户端异常:" + cause.getClass().getSimpleName() + " - " + cause.getMessage());
ctx.close();
// 触发重连
if (nettyClient != null) {
nettyClient.reconnect();
}
}
}