<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                檢測空閑連接和超時是為了及時釋放資源。常見的方法發送消息用于測試一個不活躍的連接來,通常稱為“心跳”,到遠端來確定它是否還活著。(一個更激進的方法是簡單地斷開那些指定的時間間隔的不活躍的連接)。 處理空閑連接是一項常見的任務,Netty 提供了幾個 ChannelHandler 實現此目的。表8.4概述。 Table 8.4 ChannelHandlers for idle connections and timeouts | 名稱 | 描述 | | --- | --- | | IdleStateHandler | fires an IdleStateEvent if the connection idles too long. You can then handle the IdleStateEvent by overriding the userEventTriggered(...) method in your ChannelInboundHandler. | | ReadTimeoutHandler | throws a ReadTimeoutException and closes the Channel when no inbound | data is received for a specified interval. The ReadTimeoutException can be detected by overriding the exceptionCaught(...) method of your ChannelHandler. WriteTimeoutHandler | throws a WriteTimeoutException and closes the Channel when no inbound data is received for a specified interval. The WriteTimeoutException can be detected by overriding the exceptionCaught(...) method of your ChannelHandler. 詳細看下 IdleStateHandler,下面是一個例子,當超過60秒沒有數據收到時,就會得到通知,此時就發送心跳到遠端,如果沒有回應,連接就關閉。 Listing 8.7 Sending heartbeats ~~~ public class IdleStateHandlerInitializer extends ChannelInitializer<Channel> { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new IdleStateHandler(0, 0, 60, TimeUnit.SECONDS)); //1 pipeline.addLast(new HeartbeatHandler()); } public static final class HeartbeatHandler extends ChannelInboundHandlerAdapter { private static final ByteBuf HEARTBEAT_SEQUENCE = Unpooled.unreleasableBuffer( Unpooled.copiedBuffer("HEARTBEAT", CharsetUtil.ISO_8859_1)); //2 @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { ctx.writeAndFlush(HEARTBEAT_SEQUENCE.duplicate()) .addListener(ChannelFutureListener.CLOSE_ON_FAILURE); //3 } else { super.userEventTriggered(ctx, evt); //4 } } } } ~~~ 1. IdleStateHandler 將通過 IdleStateEvent 調用 userEventTriggered ,如果連接沒有接收或發送數據超過60秒鐘 2. 心跳發送到遠端 3. 發送的心跳并添加一個偵聽器,如果發送操作失敗將關閉連接 4. 事件不是一個 IdleStateEvent 的話,就將它傳遞給下一個處理程序 總而言之,這個例子說明了如何使用 IdleStateHandler 測試遠端是否還活著,如果不是就關閉連接釋放資源。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看