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