Class WSPinger

java.lang.Object
com.gmt2001.wspinger.WSPinger
All Implemented Interfaces:
WsClientFrameHandler

public class WSPinger extends Object implements WsClientFrameHandler
Pings the remote endpoint periodically; closes the socket if a PONG is not received after a defined number of PING attempts
Author:
gmt2001
  • Constructor Details

    • WSPinger

      public WSPinger(Duration interval, Duration timeout, int failureLimit)
      Constructor that uses RFC6455 PING/PONG frames
      Parameters:
      interval - A Duration indicating the interval at which to send PING frames
      timeout - A Duration indicating the timeout during which a PONG must be received before the PING fails
      failureLimit - The number of timeouts before the connection is considered failing and is closed
      Throws:
      IllegalArgumentException - interval or timeout was less than 5 seconds; failureLimit was less than 1
    • WSPinger

      public WSPinger(Duration interval, Duration timeout, int failureLimit, Supplier<WebSocketFrame> pingFrameFactory, Predicate<WebSocketFrame> pongParser)
      Constructor
      Parameters:
      interval - A Duration indicating the interval at which to send PING frames
      timeout - A Duration indicating the timeout during which a PONG must be received before the PING fails
      failureLimit - The number of timeouts before the connection is considered failing and is closed
      pingFrameFactory - A factory method that emits a WebSocketFrame that can be sent as a PING
      pongParser - A method that determines if a given frame is a valid PONG
      Throws:
      IllegalArgumentException - interval or timeout was less than 5 seconds; failureLimit was less than 1; Any value was null
    • WSPinger

      public WSPinger(Duration interval, Duration timeout, int failureLimit, PingPongSupplierPredicate supplierPredicate)
      Constructor
      Parameters:
      interval - A Duration indicating the interval at which to send PING frames
      timeout - A Duration indicating the timeout during which a PONG must be received before the PING fails
      failureLimit - The number of timeouts before the connection is considered failing and is closed
      supplierPredicate - An object implementing both the PING supplier and the PONG predicate. Defaults to RFC6455PingPong if null
      Throws:
      IllegalArgumentException - interval or timeout was less than 5 seconds; failureLimit was less than 1; Any value was null
  • Method Details

    • handleFrame

      public void handleFrame(ChannelHandlerContext ctx, WebSocketFrame frame)
      Passed received PongWebSocketFrame to receivedPong()
      Specified by:
      handleFrame in interface WsClientFrameHandler
      Parameters:
      ctx -
      frame -
    • handshakeComplete

      public void handshakeComplete(ChannelHandlerContext ctx)
      Initializes failureCount and lastPong, then starts the ping timer
      Specified by:
      handshakeComplete in interface WsClientFrameHandler
      Parameters:
      ctx -
    • onClose

      public void onClose()
      Cancels future execution of the timers when the socket closes
      Specified by:
      onClose in interface WsClientFrameHandler
    • setClient

      public void setClient(WSClient client)
      Stores the WSClient that this pinger is linked to
      Parameters:
      client -
    • sendPing

      public void sendPing()
      Sends a PING frame with the value of payload, if enabled, then starts the failure timer, increases the payload for the next frame, and ensures payload won't overflow Long.MAX_VALUE. No-op if client is null, {@link WSClient.connected()} is false, or the timerFuture is null, done, or canceled