Package com.gmt2001.wspinger
Class WSPinger
java.lang.Object
com.gmt2001.wspinger.WSPinger
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionConstructor that uses RFC6455 PING/PONG framesWSPinger
(Duration interval, Duration timeout, int failureLimit, PingPongSupplierPredicate supplierPredicate) ConstructorWSPinger
(Duration interval, Duration timeout, int failureLimit, Supplier<WebSocketFrame> pingFrameFactory, Predicate<WebSocketFrame> pongParser) Constructor -
Method Summary
Modifier and TypeMethodDescriptionvoid
handleFrame
(ChannelHandlerContext ctx, WebSocketFrame frame) Passed receivedPongWebSocketFrame
toreceivedPong()
void
Initializes failureCount and lastPong, then starts the ping timervoid
onClose()
Cancels future execution of the timers when the socket closesvoid
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 overflowLong.MAX_VALUE
.void
Stores theWSClient
that this pinger is linked to
-
Constructor Details
-
WSPinger
Constructor that uses RFC6455 PING/PONG frames- Parameters:
interval
- ADuration
indicating the interval at which to send PING framestimeout
- ADuration
indicating the timeout during which a PONG must be received before the PING failsfailureLimit
- 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
- ADuration
indicating the interval at which to send PING framestimeout
- ADuration
indicating the timeout during which a PONG must be received before the PING failsfailureLimit
- The number of timeouts before the connection is considered failing and is closedpingFrameFactory
- A factory method that emits aWebSocketFrame
that can be sent as a PINGpongParser
- 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
- ADuration
indicating the interval at which to send PING framestimeout
- ADuration
indicating the timeout during which a PONG must be received before the PING failsfailureLimit
- The number of timeouts before the connection is considered failing and is closedsupplierPredicate
- An object implementing both the PING supplier and the PONG predicate. Defaults toRFC6455PingPong
ifnull
- Throws:
IllegalArgumentException
- interval or timeout was less than 5 seconds; failureLimit was less than 1; Any value was null
-
-
Method Details
-
handleFrame
Passed receivedPongWebSocketFrame
toreceivedPong()
- Specified by:
handleFrame
in interfaceWsClientFrameHandler
- Parameters:
ctx
-frame
-
-
handshakeComplete
Initializes failureCount and lastPong, then starts the ping timer- Specified by:
handshakeComplete
in interfaceWsClientFrameHandler
- Parameters:
ctx
-
-
onClose
public void onClose()Cancels future execution of the timers when the socket closes- Specified by:
onClose
in interfaceWsClientFrameHandler
-
setClient
Stores theWSClient
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 overflowLong.MAX_VALUE
. No-op if client isnull
, {@link WSClient.connected()} isfalse
, or the timerFuture is null, done, or canceled
-