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 TypeMethodDescriptionvoidhandleFrame(ChannelHandlerContext ctx, WebSocketFrame frame) Passed receivedPongWebSocketFrametoreceivedPong()voidInitializes failureCount and lastPong, then starts the ping timervoidonClose()Cancels future execution of the timers when the socket closesvoidsendPing()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.voidStores theWSClientthat this pinger is linked to
-
Constructor Details
-
WSPinger
Constructor that uses RFC6455 PING/PONG frames- Parameters:
interval- ADurationindicating the interval at which to send PING framestimeout- ADurationindicating 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- ADurationindicating the interval at which to send PING framestimeout- ADurationindicating 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 aWebSocketFramethat 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- ADurationindicating the interval at which to send PING framestimeout- ADurationindicating 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 toRFC6455PingPongifnull- Throws:
IllegalArgumentException- interval or timeout was less than 5 seconds; failureLimit was less than 1; Any value was null
-
-
Method Details
-
handleFrame
Passed receivedPongWebSocketFrametoreceivedPong()- Specified by:
handleFramein interfaceWsClientFrameHandler- Parameters:
ctx-frame-
-
handshakeComplete
Initializes failureCount and lastPong, then starts the ping timer- Specified by:
handshakeCompletein interfaceWsClientFrameHandler- Parameters:
ctx-
-
onClose
public void onClose()Cancels future execution of the timers when the socket closes- Specified by:
onClosein interfaceWsClientFrameHandler
-
setClient
Stores theWSClientthat 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
-