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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章