Netty入门之客户端闲置重连

Netty使用时有个问题,一般客户端不是任何时刻都需要发送信息的,在客户端闲置一段事件后应该断开连接,后面需要使用时再重新连接

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;


/**
 * 定期连接到服务器以测量并打印*服务器的正常运行时间。此示例演示了如何在Netty中实现可靠的重新连接*机制。
 */
@Slf4j
public final class UptimeClient {

    static final String HOST = System.getProperty("host", "127.0.0.1");
    static final int PORT = Integer.parseInt(System.getProperty("port", "8080"));

    /* 尝试重新连接前请等待5秒钟。 */
    static final int RECONNECT_DELAY = Integer.parseInt(System.getProperty("reconnectDelay", "5"));

    /* 当服务器在10秒钟内未发送任何内容时,请重新连接。 */
    private static final int R
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章