fctom1215
2020-02-23 b81d0cf47a08b3b5de01bf753fb71f6d1fda783a
im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java
@@ -17,6 +17,7 @@
import java.nio.ByteBuffer;
import java.util.Vector;
import java.util.concurrent.TimeUnit;
import java.util.function.ToDoubleBiFunction;
import io.netty.bootstrap.Bootstrap;
@@ -330,7 +331,7 @@
        // TODO: 2019/12/12  根据MSG_ID 来加入 超时,  暂不写
        if (channel == null){
            Log.i(TAG, "sendMsg fail,channel为空"+msg);
            MyLog.i("PlatformMessage", "sendMsg fail,channel为空"+msg);
        }
        try {
            MyLog.i("PlatformMessage", "sendMsg: "+ BytesUtils.bytesToHexString(msg));
@@ -338,7 +339,7 @@
            byteBuf.writeBytes(msg);
            channel.writeAndFlush(byteBuf);
        } catch (Exception e) {
            Log.i(TAG, "发送消息失败,reason="+e.getMessage()+"\t"+msg);
            Log.i("PlatformMessage", "发送消息失败,reason="+e.getMessage()+"\t"+msg);
        }
    }
@@ -403,6 +404,34 @@
            return mOnEventListener.getRegisterMessage();
        }
        return null;
    }
    @Override
    public byte[] getHeartbeatMsg() {
        if (mOnEventListener != null){
            return mOnEventListener.getHearbeatMsg();
        }
        return null;
    }
    /**
     * 增加心跳,
     * @param heartBeatInterval 秒
     */
    @Override
    public void addHeartbeatHandler(int heartBeatInterval){
        this.heartBeatInterval = heartBeatInterval*1000;
        _addHeartbeatHandler();
    }
    public int getHeartbeatInterval() {
        return this.heartBeatInterval;
    }
    public ExecutorServiceFactory getLoopGroup() {
        return loopGroup;
    }
@@ -522,6 +551,37 @@
    }
    private void _addHeartbeatHandler(){
        MyLog.i("add HeartbeatHandler");
        if (channel == null || !channel.isActive() || channel.pipeline()==null){
            MyLog.i("PlatformMessage","添加心跳handler失败");
            return;
        }
        try {
            if(channel.pipeline().get(IdleStateHandler.class.getSimpleName()) != null){
                MyLog.i("PlatformMessage","之前存在的读写超时handler,先移除掉,再重新添加");
                channel.pipeline().remove(IdleStateHandler.class.getSimpleName());
            }
            // 3次心跳没响应,代表连接已断开
            //设定IdleStateHandler心跳检测每四秒进行一次写检测,如果四秒内write()方法未被调用则触发一次userEventTrigger()方法,实现客户端每四秒向服务端发送一次消息;
            channel.pipeline().addFirst(IdleStateHandler.class.getSimpleName(), new IdleStateHandler(
                    0, heartBeatInterval, 0, TimeUnit.MILLISECONDS));
            // 重新添加HeartbeatHandler
            if (channel.pipeline().get(HeartbeatHandler.class.getSimpleName()) != null) {
                channel.pipeline().remove(HeartbeatHandler.class.getSimpleName());
            }
            if (channel.pipeline().get(TCPReadHandler.class.getSimpleName()) != null) {
                MyLog.i("之前存在的先移除掉,再重新添加HeartbeatHandler");
                channel.pipeline().addBefore(TCPReadHandler.class.getSimpleName(), HeartbeatHandler.class.getSimpleName(),
                        new HeartbeatHandler(this));
            }
        } catch (Exception e) {
            e.printStackTrace();
            MyLog.i("添加心跳消息管理handler失败,reason:" + e.getMessage());
        }
    }
}