java websocket中的ping-pong 機制


參考源碼:

https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/java/org/java_websocket/client/WebSocketClient.java

https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/java/org/java_websocket/AbstractWebSocket.java

https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/java/org/java_websocket/WebSocket.java

https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/java/org/java_websocket/WebSocketImpl.java

https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/java/org/java_websocket/drafts/Draft_6455.java

 文章參考:

注:websocket基於tcp協議,它在第一次連接時發起http請求,之后建立握手

在websocket中設置setConnectionLostTimeout參數,解釋為:Setter for the interval checking for lost connections,意思是間隔檢查連接是否丟失

整體是調用順序為:onWebsocketOpen -》 startConnectionLostTimer -》 restartConnectionLostTimer-》 scheduleAtFixedRate -》 executeConnectionLostDetection

關鍵代碼
this.connectionLostTimeout = TimeUnit.SECONDS.toNanos(connectionLostTimeout);
if (this.connectionLostTimeout <= 0) {
  log.trace("Connection lost timer stopped");
   cancelConnectionLostTimer();
   return;
}
long minimumPongTime = (long) (System.nanoTime() - ( connectionLostTimeout * 1.5 ));
for( WebSocket conn : connections ) {
  executeConnectionLostDetection(conn, minimumPongTime);
}
WebSocketImpl webSocketImpl = (WebSocketImpl) webSocket;
if( webSocketImpl.getLastPong() < minimumPongTime ) {
  log.trace("Closing connection due to no pong received: {}", webSocketImpl);
  webSocketImpl.closeConnection( CloseFrame.ABNORMAL_CLOSE, "The connection was closed because the other endpoint did not respond with a pong in time. For more information check: https://github.com/TooTallNate/Java-WebSocket/wiki/Lost-connection-detection" );
} else {
  if( webSocketImpl.isOpen() ) {
    webSocketImpl.sendPing();
  } else {
    log.trace("Trying to ping a non open connection: {}", webSocketImpl);
  }
}

connectionLostTimeout在設置后會轉為納秒時間, minimumPongTime為當前納秒時間減去connectionLostTimeout的1.5倍,當最后一次Pong的時間小於minimumPongTime時產生close,即在間隔時間內未收到Pong響應關閉連接。如果正常則繼續發送ping,即調用sendPing。

在服務端收到ping的時候,立即下發pong,兩者的容忍時間為connectionLostTimeout是1.5倍,即設十秒的話就是容忍十五秒。當網絡發生異常時,兩者情況,服務端沒有收到ping亦或者客戶端沒有收到pong,觸發close。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM