Package pl.morgwai.base.servlet.utils
Class WebsocketPingerService
- java.lang.Object
-
- pl.morgwai.base.servlet.utils.WebsocketPingerService
-
public class WebsocketPingerService extends Object
Automatically pings and handles pongs from websocket connections. Depending on constructor used, operates in eitherping-pong mode
orkeep-alive-only mode
.Instances are usually created at app startup and stored in a location easily reachable for endpoint instances (for example on static var in app's ServletContextListener).
Endpoint instances should register themselves for pinging in their
Endpoint.onOpen(Session, jakarta.websocket.EndpointConfig)
method usingaddConnection(Session)
and deregister inEndpoint.onClose(Session, CloseReason)
usingremoveConnection(Session)
.
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_FAILURE_LIMIT
Arbitrarily chosen number.static int
DEFAULT_INTERVAL
Majority of proxy and NAT routers have timeout of at least 60s.static int
DEFAULT_PING_SIZE
Economic value to reduce use ofRandom
.
-
Constructor Summary
Constructors Constructor Description WebsocketPingerService()
CallsWebsocketPingerService
(
(ping-pong mode).DEFAULT_INTERVAL
,DEFAULT_FAILURE_LIMIT
,DEFAULT_PING_SIZE
, false)WebsocketPingerService(int intervalSeconds)
CallsWebsocketPingerService
(intervalSeconds, false)
(keep-alive-only mode).WebsocketPingerService(int intervalSeconds, boolean synchronizeSending)
Configures and starts the service in keep-alive-only mode: just 1-byte-unsolicited pong is sent each time and responses are not expected.WebsocketPingerService(int intervalSeconds, int failureLimit, int pingSize)
CallsWebsocketPingerService(intervalSeconds, failureLimit, pingSize, false)
(ping-pong mode).WebsocketPingerService(int intervalSeconds, int failureLimit, int pingSize, boolean synchronizeSending)
Configures and starts the service in ping-pong mode: timely pongs are expected and validated.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addConnection(Session connection)
Registersconnection
for pinging by this service.int
getNumberOfConnections()
Returns the number of currently registered connections.void
removeConnection(Session connection)
Deregistersconnection
from this service.Set<Session>
stop()
Stops the service.
-
-
-
Field Detail
-
DEFAULT_INTERVAL
public static final int DEFAULT_INTERVAL
Majority of proxy and NAT routers have timeout of at least 60s.- See Also:
- Constant Field Values
-
DEFAULT_FAILURE_LIMIT
public static final int DEFAULT_FAILURE_LIMIT
Arbitrarily chosen number.- See Also:
- Constant Field Values
-
DEFAULT_PING_SIZE
public static final int DEFAULT_PING_SIZE
Economic value to reduce use ofRandom
.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
WebsocketPingerService
public WebsocketPingerService(int intervalSeconds, int failureLimit, int pingSize, boolean synchronizeSending)
Configures and starts the service in ping-pong mode: timely pongs are expected and validated.- Parameters:
intervalSeconds
- interval between pings and also timeout for pongs.failureLimit
- limit of lost, malformed or timed out pongs after which the given connection is closed. Each valid, timely pong resets connection's failure counter.pingSize
- size of the ping data to send. This comes fromRandom
, so an economic value is recommended.synchronizeSending
- whether to synchronize packet sending on the given connection. Whether it is necessary depends on the implementation of the container. For example it is not necessary on Jetty, but it is on Tomcat: see this bug report.
-
WebsocketPingerService
public WebsocketPingerService(int intervalSeconds, int failureLimit, int pingSize)
CallsWebsocketPingerService(intervalSeconds, failureLimit, pingSize, false)
(ping-pong mode).
-
WebsocketPingerService
public WebsocketPingerService()
CallsWebsocketPingerService
(
(ping-pong mode).DEFAULT_INTERVAL
,DEFAULT_FAILURE_LIMIT
,DEFAULT_PING_SIZE
, false)
-
WebsocketPingerService
public WebsocketPingerService(int intervalSeconds, boolean synchronizeSending)
Configures and starts the service in keep-alive-only mode: just 1-byte-unsolicited pong is sent each time and responses are not expected. The params have the same meaning as inWebsocketPingerService(int, int, int, boolean)
.
-
WebsocketPingerService
public WebsocketPingerService(int intervalSeconds)
CallsWebsocketPingerService
(intervalSeconds, false)
(keep-alive-only mode).
-
-
Method Detail
-
addConnection
public void addConnection(Session connection)
Registersconnection
for pinging by this service. Usually called inEndpoint.onOpen(Session, jakarta.websocket.EndpointConfig)
.
-
removeConnection
public void removeConnection(Session connection)
Deregistersconnection
from this service. Usually called inEndpoint.onClose(Session, CloseReason)
.
-
getNumberOfConnections
public int getNumberOfConnections()
Returns the number of currently registered connections.
-
-