|
@@ -70,7 +70,7 @@ public class TcpClient {
|
|
|
protected void initChannel(SocketChannel socketChannel) throws Exception {
|
|
|
// 这里将LengthFieldBasedFrameDecoder添加到pipeline的首位,因为其需要对接收到的数据
|
|
|
// 进行长度字段解码,这里也会对数据进行粘包和拆包处理
|
|
|
- socketChannel.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024, 0, 2, 0, 2));
|
|
|
+ socketChannel.pipeline().addLast(new LengthFieldBasedFrameDecoder(2048, 0, 2, 0, 2));
|
|
|
// LengthFieldPrepender是一个编码器,主要是在响应字节数据前面添加字节长度字段
|
|
|
socketChannel.pipeline().addLast(new LengthFieldPrepender(2));
|
|
|
//心跳包应当小于服务器间隔
|
|
@@ -105,7 +105,7 @@ public class TcpClient {
|
|
|
}
|
|
|
connecting = true;
|
|
|
|
|
|
- System.out.println("connect start");
|
|
|
+ System.out.println("tcp connect start");
|
|
|
ChannelFuture future = bootstrap.connect().addListener(new ChannelFutureListener() {
|
|
|
@Override
|
|
|
public void operationComplete(ChannelFuture channelFuture) throws Exception {
|
|
@@ -114,11 +114,11 @@ public class TcpClient {
|
|
|
isRunning = true;
|
|
|
channel = channelFuture.channel();
|
|
|
retryTimes = 1;
|
|
|
- System.out.println("connect success");
|
|
|
+ System.out.println("tcp connect success");
|
|
|
} else {
|
|
|
//连接失败时的处理
|
|
|
isRunning = false;
|
|
|
- System.out.println("connect retry : " + retryTimes);
|
|
|
+ System.out.println("tcp connect retry : " + retryTimes);
|
|
|
channelFuture.channel().eventLoop().schedule(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
@@ -137,6 +137,7 @@ public class TcpClient {
|
|
|
}
|
|
|
}
|
|
|
}, 60*2, TimeUnit.SECONDS);
|
|
|
+
|
|
|
}else{
|
|
|
doConnect();
|
|
|
}
|