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 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addConnection(Session connection)
Registersconnection
for pinging.int
getNumberOfConnections()
Returns the number of currently registered connections.void
removeConnection(Session connection)
Deregistersconnection
.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.- Parameters:
intervalSeconds
- interval between pings.failureLimit
- limit of lost or malformed pongs after which the given connection is closed. Pongs received afterpingIntervalSeconds
count as failures. 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 ping 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 is sent each time and responses are not expected. Remaining 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. Usually called inEndpoint.onOpen(Session, jakarta.websocket.EndpointConfig)
.
-
removeConnection
public void removeConnection(Session connection)
Deregistersconnection
. Usually called inEndpoint.onClose(Session, CloseReason)
.
-
getNumberOfConnections
public int getNumberOfConnections()
Returns the number of currently registered connections.
-
-