Package gov.usgs.earthquake.distribution
Class WebSocketClient
java.lang.Object
gov.usgs.earthquake.distribution.WebSocketClient
- All Implemented Interfaces:
Runnable
Manages a simple connection to a websocket. Can also be overridden for more
complex behavior.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final long
Default for how long to wait for any Message(excluding Pongs)static final int
Default number of attemptsstatic final long
Default for time in between pings to serverstatic final long
Default for how long to wait for pong response to ping before closing or restarting connectionstatic final boolean
Default for trying to retry on closestatic final long
Default timeout in ms -
Constructor Summary
ConstructorsConstructorDescriptionWebSocketClient
(URI endpoint, WebSocketListener listener) Constructs the clientWebSocketClient
(URI endpoint, WebSocketListener listener, int attempts, long timeoutMillis, boolean retryOnClose) Deprecated.WebSocketClient
(URI endpoint, WebSocketListener listener, int attempts, long timeoutMillis, boolean retryOnClose, long pingIntervalMillis, long pingWaitMillis, long anyMessageIntervalMillis) Creates a Websocket Client Default values for attempts ant timeoutMillis create an instance which is designed to be up and running at all times -
Method Summary
Modifier and TypeMethodDescriptionvoid
catchPong
(javax.websocket.PongMessage pongMessage, javax.websocket.Session session) Called by Websocket interface when a Pong is received in response to a ping that was sent.void
connect()
Connect to serverboolean
Checks if there is an open sessionvoid
onClose
(javax.websocket.Session session, javax.websocket.CloseReason reason) Closes the session on the lister, sets constructor session to null Check if should be retryedvoid
Gives listener the messagevoid
onOpen
(javax.websocket.Session session) Sets the session and listenervoid
run()
void
setListener
(WebSocketListener listener) void
shutdown()
Sets retry to false, then closes session
-
Field Details
-
DEFAULT_ATTEMPTS
public static final int DEFAULT_ATTEMPTSDefault number of attempts- See Also:
-
DEFAULT_TIMEOUT_MILLIS
public static final long DEFAULT_TIMEOUT_MILLISDefault timeout in ms- See Also:
-
DEFAULT_RETRY_ON_CLOSE
public static final boolean DEFAULT_RETRY_ON_CLOSEDefault for trying to retry on close- See Also:
-
DEFAULT_PING_INTERVAL_MILLIS
public static final long DEFAULT_PING_INTERVAL_MILLISDefault for time in between pings to server- See Also:
-
DEFAULT_PING_WAIT_MILLIS
public static final long DEFAULT_PING_WAIT_MILLISDefault for how long to wait for pong response to ping before closing or restarting connection- See Also:
-
DEFAULT_ANY_MESSAGE_INTERVAL_MILLIS
public static final long DEFAULT_ANY_MESSAGE_INTERVAL_MILLISDefault for how long to wait for any Message(excluding Pongs)- See Also:
-
-
Constructor Details
-
WebSocketClient
@Deprecated public WebSocketClient(URI endpoint, WebSocketListener listener, int attempts, long timeoutMillis, boolean retryOnClose) throws Exception Deprecated.useWebSocketClient(URI, WebSocketListener, int, long, boolean, long, long, long)
, includes ping configurationConstructs the client. Also connects to the server.- Parameters:
endpoint
- the URI to connect tolistener
- a WebSocketListener to handle incoming messagesattempts
- an integer number of times to try the connectiontimeoutMillis
- a long for the wait time between attemptsretryOnClose
- boolean for if the connection should retry when closed- Throws:
Exception
- on thread interrupt or connection failure
-
WebSocketClient
public WebSocketClient(URI endpoint, WebSocketListener listener, int attempts, long timeoutMillis, boolean retryOnClose, long pingIntervalMillis, long pingWaitMillis, long anyMessageIntervalMillis) throws Exception Creates a Websocket Client Default values for attempts ant timeoutMillis create an instance which is designed to be up and running at all times- Parameters:
endpoint
- the URI to connect tolistener
- a WebSocketListener to handle incoming messagesattempts
- an integer number of times to try the connectiontimeoutMillis
- a long for the wait time between attemptsretryOnClose
- boolean for if the connection should retry when closedpingIntervalMillis
- how often to send ping in milliseconds. If you don't want to send pings set to 0 or negative value.pingWaitMillis
- how long to wait, in milliseconds, before declaring socket down and closing and retrying if retryOnClose is set.anyMessageIntervalMillis
- how often to check if any message has arrived(excluding Pongs) restart if one has not.- Throws:
Exception
- on thread interrupt or connection failure
-
WebSocketClient
Constructs the client- Parameters:
endpoint
- the URI to connect tolistener
- a WebSocketListener to handle incoming messages- Throws:
Exception
- thread interrupt or connection failure
-
-
Method Details
-
catchPong
public void catchPong(javax.websocket.PongMessage pongMessage, javax.websocket.Session session) Called by Websocket interface when a Pong is received in response to a ping that was sent.- Parameters:
pongMessage
- Message that was populated from ping.session
- The websocket session.
-
connect
Connect to server- Throws:
Exception
- if error occurs
-
isConnected
Checks if there is an open session- Returns:
- boolean
- Throws:
IOException
- if IO error occurs
-
onOpen
Sets the session and listener- Parameters:
session
- Session- Throws:
IOException
- if IO error occurs
-
onClose
public void onClose(javax.websocket.Session session, javax.websocket.CloseReason reason) throws IOException Closes the session on the lister, sets constructor session to null Check if should be retryed- Parameters:
session
- Sessionreason
- for close- Throws:
IOException
- if IO error occurs
-
onMessage
Gives listener the message- Parameters:
message
- String- Throws:
IOException
- if IO error occurs
-
run
public void run() -
shutdown
Sets retry to false, then closes session- Throws:
Exception
- if error occurs
-
setListener
- Parameters:
listener
- set WebSocketListener
-
WebSocketClient(URI, WebSocketListener, int, long, boolean, long, long, long)
, includes ping configuration