我想实现一个断开/重连机制。
我已经看到大多数时候重新连接会创建一个新的引导,我想知道是否有可能在ChannelHandler中进行重新连接。
例如,如果客户端捕捉到一个WRITE_IDLE事件,它可以像下面这样关闭通道并在ChannelHandler中重新连接它吗?
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleState state= ((IdleStateEvent) evt).state();
if (state == IdleState.WRITER_IDLE) {
// read timeout, break the channel
System.out.println("client write timeout");
SocketAddress remoteAddress = ctx.channel().remoteAddress();
// close channel
ctx.channel().close();
// reconnect
ctx.connect(remoteAddress);
}
}
}我测试了几次,但都不起作用。
https://stackoverflow.com/questions/51399481
复制相似问题