Bladeren bron

TCP心跳超时优化

allen 4 jaren geleden
bovenliggende
commit
489d6fade5

+ 0 - 5
home/src/main/code/com/wdkl/ncs/android/component/home/activity/WatchRegisterActivity.kt

@@ -89,11 +89,6 @@ class WatchRegisterActivity : BaseActivity<WatchDevicePresenter, WatchActivityRe
             run {
                 //TcpClient.getInstance().init("192.168.1.138", 5080, 9)
                 //TcpClient.getInstance().init("47.106.200.55", 5080, 9)
-                if( Constants.heart_beat>40){
-                    Constants.heart_beat =  Constants.heart_beat-10
-                }else{
-                    Constants.heart_beat =  Constants.heart_beat
-                }
 
                 TcpClient.getInstance().init(Constants.tcp_server, Constants.tcp_port, Constants.heart_beat)
             }

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

@@ -2,6 +2,8 @@ package com.wdkl.ncs.android.middleware.tcp;
 
 import android.util.Log;
 
+import com.wdkl.ncs.android.component.nursehome.common.Constants;
+
 import java.util.Scanner;
 import java.util.concurrent.TimeUnit;
 
@@ -59,7 +61,7 @@ public class TcpClient {
                         // LengthFieldPrepender是一个编码器,主要是在响应字节数据前面添加字节长度字段
                         socketChannel.pipeline().addLast(new LengthFieldPrepender(2));
                         //心跳包应当小于服务器间隔
-                        socketChannel.pipeline().addLast(new IdleStateHandler(0, hbSeconds, 0, TimeUnit.SECONDS));
+                        socketChannel.pipeline().addLast(new IdleStateHandler(hbSeconds+1, hbSeconds, 0, TimeUnit.SECONDS));
                         socketChannel.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));
                         socketChannel.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
                         socketChannel.pipeline().addLast(tcpClientHandler);
@@ -93,8 +95,8 @@ public class TcpClient {
                             if (retryTimes > 30) {
                                 System.out.println("TcpClient 重试" + (retryTimes - 1) + "次,结束");
                                 workGroup.shutdownGracefully();
-                                //todo: 从API获取新的serverIP和serverPort,全新连接
-                                //TcpClient.getInstance().init();
+                                //从API获取新的serverIP和serverPort,全新连接
+                                TcpClient.getInstance().init(Constants.Companion.getTcp_server(), Constants.Companion.getTcp_port(), Constants.Companion.getHeart_beat());
                                 return;
                             }
                             doConnect();

+ 9 - 4
middleware/src/main/code/com/wdkl/ncs/android/middleware/tcp/TcpClientHandler.java

@@ -36,7 +36,7 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<String> {
     @Override
     public void channelActive(ChannelHandlerContext ctx) throws Exception {
         super.channelActive(ctx);
-        System.out.println("tcp连接成功");
+        Log.i(TAG,"tcp连接成功");
         this.ctx = ctx;
         connected = true;
         retryTimes = 0;
@@ -49,16 +49,16 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<String> {
     public void channelInactive(ChannelHandlerContext ctx) throws Exception {
         super.channelInactive(ctx);
         connected = false;
-        System.out.println("TcpClientHandler 失去连接");
+        Log.i(TAG, "TcpClientHandler 失去连接");
         reConnect(ctx);
     }
 
     //读取String消息
     @Override
     protected void channelRead0(ChannelHandlerContext ctx, String source) throws Exception {
-        System.out.println("TcpClientHandler from server ===> " + source);
+        //System.out.println("TcpClientHandler from server ===> " + source);
         if(source.equals("1")){
-        Log.e(TAG,"收到服务器返回的心跳"+"source "+source);
+            Log.i(TAG,"收到服务器返回的心跳 "+source);
         }else {
             TcpModel tcpModel = TcpModel.getModelByJson(source);
 
@@ -79,6 +79,11 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<String> {
             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.close();
+            }
         }
     }