<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 功能強大 支持多語言、二開方便! 廣告
                ## 1. 心跳機制 ### 1.1 作用 > 1. Netty提供了對心跳機制的支持,以檢測Netty的狀態。具體實現方法是加入**IdleStateHandler**。 > 2. 在一定時間內處于長連接下,client與server沒有數據交互時,應該進行心跳機制檢查。 ~~~ /** * Creates a new instance firing {@link IdleStateEvent}s. * * @param readerIdleTimeSeconds * an {@link IdleStateEvent} whose state is {@link IdleState#READER_IDLE} * will be triggered when no read was performed for the specified * period of time. Specify {@code 0} to disable. * @param writerIdleTimeSeconds * an {@link IdleStateEvent} whose state is {@link IdleState#WRITER_IDLE} * will be triggered when no write was performed for the specified * period of time. Specify {@code 0} to disable. * @param allIdleTimeSeconds * an {@link IdleStateEvent} whose state is {@link IdleState#ALL_IDLE} * will be triggered when neither read nor write was performed for * the specified period of time. Specify {@code 0} to disable. */ public IdleStateHandler( int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds) { this(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds, TimeUnit.SECONDS); } ~~~ 實例化IdleStateHandler對象需要傳遞三個參數,上邊的注釋解釋的很清楚: > 首先,這個實例是為了觸發IdleStateEvent事件 > 1. readerIdleTimeSeconds: 當沒有可讀數據的時長達到這個時間時,觸發事件 > 2. writerIdleTimeSeconds:當沒有可寫數據的時長達到這個時間時,觸發事件 > 3. allIdleTimeSeconds:當沒有可讀數據和可寫數據的時長達到這個時間時,觸發事件,0表示取消這一觸發條件 > 4. 默認時間單位是秒 如果超出以上條件規定的時間就會調用userEventTriggered()方法: ### 1.2 使用 1. 歸根結底IdleStateHandler是Handler,所以一定要加入pipeline中,通常是在client端。 ~~~ bootstrap.handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new IdleStateHandler(20,10,0)); //加入心跳機制Handler socketChannel.pipeline().addLast(new ObjectEncoder()); socketChannel.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null))); socketChannel.pipeline().addLast(new NettyClientHandler()); } }); ~~~ 2. 觸發的作用函數 為了響應心跳機制事件,需要重寫userEventTriggered()方法,對事件進行響應,這里對write進行了處理,想server發送數據,就是對read和write的時間進行記錄,發現超過一定時間沒有進行讀寫,就觸發對應的事件。 ~~~ @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent e = (IdleStateEvent) evt; switch (e.state()) { case WRITER_IDLE: PingMsg pingMsg=new PingMsg(); ctx.writeAndFlush(pingMsg); System.out.println("send ping to server----------"); break; default: break; } } ~~~ ### 1.2 Handler方法 #### 1.2.1 messageReceived() > 1. 和channelRead有一樣的作用,用來接收網絡數據 > 2. 需要編解碼的才會去用messageReceived,一般都是使用ChannelRead來讀取的。讀一下SimpleChannelInboundHandler的源代碼你就知道了,泛型不匹配,不會調用messageReceived的。 #### 1.2.2 channelRead 接收網絡數據
                  <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>

                              哎呀哎呀视频在线观看