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.Initial 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). Additional instances may be added later if number of connections is too big to handle for the existing ones.
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_MAX_MALFORMED_PONG_COUNT
Arbitrarily chosen number.static int
DEFAULT_PING_INTERVAL
Majority of proxy and NAT routers have timeout of at least 60s.
-
Constructor Summary
Constructors Constructor Description WebsocketPingerService()
WebsocketPingerService(int pingIntervalSeconds, int maxMalformedPongCount)
Configures and starts the service.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addConnection(Session connection)
Registersconnection
for pinging.int
getConnectionsSize()
Returns the number of currently registered connections.void
removeConnection(Session connection)
Deregistersconnection
.ConcurrentMap<Session,pl.morgwai.base.servlet.utils.WebsocketPingerService.PingPongPlayer>
stop()
Stops the service.
-
-
-
Field Detail
-
DEFAULT_PING_INTERVAL
public static final int DEFAULT_PING_INTERVAL
Majority of proxy and NAT routers have timeout of at least 60s.- See Also:
- Constant Field Values
-
DEFAULT_MAX_MALFORMED_PONG_COUNT
public static final int DEFAULT_MAX_MALFORMED_PONG_COUNT
Arbitrarily chosen number.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
WebsocketPingerService
public WebsocketPingerService(int pingIntervalSeconds, int maxMalformedPongCount)
Configures and starts the service.- Parameters:
pingIntervalSeconds
- how often to ping all connections.maxMalformedPongCount
- limit after which a given connection is closed. Each valid, timely pong resets connection's counter. Pongs received afterpingIntervalSeconds
count as malformed.
-
WebsocketPingerService
public WebsocketPingerService()
-
-
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)
.
-
getConnectionsSize
public int getConnectionsSize()
Returns the number of currently registered connections.
-
stop
public ConcurrentMap<Session,pl.morgwai.base.servlet.utils.WebsocketPingerService.PingPongPlayer> stop()
Stops the service. After a call to this method the service becomes no longer usable and should be discarded.- Returns:
- remaining registered connections.
-
-