浏览代码

tcp优化

allen 4 年之前
父节点
当前提交
d312870d8e

+ 6 - 0
gradle/wrapper/gradle-wrapper.properties

@@ -0,0 +1,6 @@
+#Fri Jan 22 17:49:32 CST 2021
+distributionBase=GRADLE_USER_HOME
+distributionPath=wrapper/dists
+zipStoreBase=GRADLE_USER_HOME
+zipStorePath=wrapper/dists
+distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip

+ 1 - 1
middleware/src/main/code/com/wdkl/ncs/android/middleware/tcp/TcpClient.java

@@ -61,7 +61,7 @@ public class TcpClient {
                         // LengthFieldPrepender是一个编码器,主要是在响应字节数据前面添加字节长度字段
                         socketChannel.pipeline().addLast(new LengthFieldPrepender(2));
                         //心跳包应当小于服务器间隔
-                        socketChannel.pipeline().addLast(new IdleStateHandler(hbSeconds+1, hbSeconds, 0, TimeUnit.SECONDS));
+                        socketChannel.pipeline().addLast(new IdleStateHandler(hbSeconds*2, hbSeconds, 0, TimeUnit.SECONDS));
                         socketChannel.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));
                         socketChannel.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
                         socketChannel.pipeline().addLast(tcpClientHandler);

+ 1 - 3
middleware/src/main/code/com/wdkl/ncs/android/middleware/tcp/TcpClientHandler.java

@@ -89,11 +89,9 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<String> {
             IdleStateEvent event = (IdleStateEvent)evt;
             if (event.state()== IdleState.WRITER_IDLE){
                 ctx.writeAndFlush("0");
-              //读心跳包超时执行
             }else if (event.state() == IdleState.READER_IDLE){
                 Log.i(TAG,"TcpClientHandler ===> pong from server failed");
-               ctx.writeAndFlush("0");
-//                ctx.close();
+                ctx.close();
             }
         }
     }