Package com.tccc.kos.commons.web.client
Class WebsocketClient
java.lang.Object
com.tccc.kos.commons.web.client.WebsocketClient
- All Implemented Interfaces:
AttributeAware
Websocket client with support for automatic reconnects using exponential backoff. This is
useful for applications that expect to be connected to a remote host and don't want to
deal with the connection management. The client also supports changing the endpoint to
connect to while the client is running.
By default the client is configured for single connection mode. A call to start()
will only perform a single connection attempt. In this mode the user can call start()
on failure / close to attempt another connection. By setting the reconnect
flag to
true, a single call to start()
will cause the client to continuously connect to
the configured endpoint.
Lifecycle events for the connection are available by attaching a connection listener. All messages received from the client will be forwarded to the configured message handler.
- Since:
- 1.0
- Version:
- 2022-09-28
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddListener
(WebsocketConnectionListener listener) Add a connection listener to the client.Disable ping timeout checks on the websocket connection.getAttribute
(Object name) Return an attribute set on the client.Return the active connection if there is one.int
int
long
long
long
long
boolean
Return true if the websocket is currently connected.boolean
Return the reconnect flag.boolean
Return true if the client is currently running as a result of callingstart()
.removeAttribute
(Object name) Remove an attribute from the client.removeListener
(WebsocketConnectionListener listener) Remove a connection listener from the client.boolean
sendMessage
(String msg) Send a message to the websocket connection.void
setAttribute
(Object name, Object val) Set an attribute to the client which will be added to any connections created by this client.setEndpoint
(WebsocketEndpoint endpoint) Set the endpoint.setMaxFailedConnects
(int maxFailedConnects) Set the max number of failed connects before stopping the client.setMaxFailureCount
(int count) Set the max failure count when computing the reconnect delay.setMinSuccessfulConnectTimeMS
(long ms) Set the minimum time a connection must be connected before a disconnect is considered a failure.setMsgHandler
(WebsocketMsgHandler msgHandler) Set the message handler to receive incoming messages.setReconnect
(boolean reconnect) Set reconnect flag.setRetryDelayBaseTimeMS
(long ms) Set the base delay time for a reconnect.setRetryDelayMaxTimeMS
(long ms) Set the max retry delay.setRetryDelayPerFailureTimeMS
(long ms) Set the delay to add to the base retry time when there are connection failures.start()
Start the client which will cause it to try to connect to the currently configured endpoint if there is one.void
stop()
Stop the client.
-
Constructor Details
-
WebsocketClient
public WebsocketClient()Create a new websocket client.
-
-
Method Details
-
setMsgHandler
Set the message handler to receive incoming messages.- Parameters:
msgHandler
- the message handler
-
setEndpoint
Set the endpoint. Setting this while a websocket is open will close the websocket. If thereconnect
flag is true then the new endpoint will be used on the next connection attempt. -
start
Start the client which will cause it to try to connect to the currently configured endpoint if there is one. The client will automatically reconnect until stopped or the endpoint is set to null. -
stop
public void stop()Stop the client. Any connection currently open will be closed. -
isRunning
public boolean isRunning()Return true if the client is currently running as a result of callingstart()
. If no endpoint is set, the connection attempt will block until either and endpoing is set orstop()
is called. If and endpoint is set, a connection attempt will be made to the endpoint. Ifreconnect
is false, only a single connection attempt will be made. If true, the client will attempt to reconnect to the endpoint using a backoff algorithm untilstop()
is called. -
isConnected
public boolean isConnected()Return true if the websocket is currently connected. -
setReconnect
Set reconnect flag. When set to true the client will attempt to reconnect as long as the client is running. When set to false, only a single connect attempt will be made for each call tostart()
. -
isReconnect
public boolean isReconnect()Return the reconnect flag. If true, the client will automatically reconnect to the endpoint untilstop()
is called or the endpoint is set to null. -
disablePing
Disable ping timeout checks on the websocket connection. This is useful when connecting to a server that doesn't support ping such as when connecting to the chrome web browser. -
getConnection
Return the active connection if there is one. -
setAttribute
Set an attribute to the client which will be added to any connections created by this client. Does not impact any existing connections.- Specified by:
setAttribute
in interfaceAttributeAware
- Parameters:
name
- the attribute nameval
- the attribute value
-
removeAttribute
Remove an attribute from the client. Does not impact any existing connections.- Specified by:
removeAttribute
in interfaceAttributeAware
- Parameters:
name
- the attribute name- Returns:
- the value of the attribute being removed, or
null
if it didn't exist
-
getAttribute
Return an attribute set on the client.- Specified by:
getAttribute
in interfaceAttributeAware
- Parameters:
name
- the attribute name- Returns:
- the value of the attribute, or
null
if it doesn't exist
-
addListener
Add a connection listener to the client.- Parameters:
listener
- the listener to add
-
removeListener
Remove a connection listener from the client.- Parameters:
listener
- the listener to remove
-
sendMessage
Send a message to the websocket connection. Returns true if the message was sent, false if there is no connection to send the message.- Parameters:
msg
- the message to send
-
setMinSuccessfulConnectTimeMS
Set the minimum time a connection must be connected before a disconnect is considered a failure. For example, if a server connects but then immediately disconnects (such as if a proxy terminates the websocket but then fails to connect to the server behind the proxy) then the connection was really a failure so the backoff algorithm should throttle connections. Default value is 20000. -
setRetryDelayBaseTimeMS
Set the base delay time for a reconnect. This will be the starting delay for every connect attempt before throttling is added. Default value is 1000. -
setRetryDelayPerFailureTimeMS
Set the delay to add to the base retry time when there are connection failures. This will be multiplied by the square of the failure count. Default value is 500; -
setMaxFailureCount
Set the max failure count when computing the reconnect delay. Since the backoff algorithm uses the square of the failure count, the delays can grow very large very quickly. This caps the delay related to failure count. Default value is 100. Using default values, this will result in a max delay of about 1.4 hours from failure related backoff. -
setRetryDelayMaxTimeMS
Set the max retry delay. This ensures that failure based backoff doesn't grow past this upper threshold. Default value is 2 hours. -
setMaxFailedConnects
Set the max number of failed connects before stopping the client. -
getMinSuccessfulConnectTimeMS
public long getMinSuccessfulConnectTimeMS() -
getRetryDelayBaseTimeMS
public long getRetryDelayBaseTimeMS() -
getRetryDelayPerFailureTimeMS
public long getRetryDelayPerFailureTimeMS() -
getRetryDelayMaxTimeMS
public long getRetryDelayMaxTimeMS() -
getMaxFailureCount
public int getMaxFailureCount() -
getMaxFailedConnects
public int getMaxFailedConnects() -
getEndpoint
-
getMsgHandler
-