netty4 断线重连

监听到连接服务器失败时,会在3秒后重新连接(执行doConnect方法)

这还不够,当客户端掉线时要进行重新连接

在我们自己定义逻辑处理的Handler中

 

@Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Log.d(Config.TAG, "与服务器断开连接服务器");
        super.channelInactive(ctx);
        MsgHandle.getInstance().channel = null;

        //重新连接服务器
        ctx.channel().eventLoop().schedule(new Runnable() {
            @Override
            public void run() {
                MyClient.doConnect();
            }
        }, 2, TimeUnit.SECONDS);
        ctx.close();
    }
posted @ 2017-08-29 11:01  我在途中  阅读(385)  评论(0编辑  收藏  举报