首页 > 技术文章 > netty4 断线重连

zhengtu2015 2017-08-29 11:01 原文

监听到连接服务器失败时,会在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();
    }

推荐阅读