Package gov.usgs.earthquake.distribution
Class WebSocketClient
- java.lang.Object
-
- gov.usgs.earthquake.distribution.WebSocketClient
-
public class WebSocketClient extends java.lang.Object
Manages a simple connection to a websocket. Can also be overridden for more complex behavior.
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_ATTEMPTS
Default number of attemptsstatic boolean
DEFAULT_RETRY_ON_CLOSE
Default for trying to retry on closestatic long
DEFAULT_TIMEOUT_MILLIS
Default timeout in ms
-
Constructor Summary
Constructors Constructor Description WebSocketClient(java.net.URI endpoint, WebSocketListener listener)
Constructs the clientWebSocketClient(java.net.URI endpoint, WebSocketListener listener, int attempts, long timeoutMillis, boolean retryOnClose)
Constructs the client.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
connect()
Connect to serverboolean
isConnected()
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
onMessage(java.lang.String message)
Gives listener the messagevoid
onOpen(javax.websocket.Session session)
Sets the session and listenervoid
setListener(WebSocketListener listener)
void
shutdown()
Sets retry to false, then closes session
-
-
-
Field Detail
-
DEFAULT_ATTEMPTS
public static final int DEFAULT_ATTEMPTS
Default number of attempts- See Also:
- Constant Field Values
-
DEFAULT_TIMEOUT_MILLIS
public static final long DEFAULT_TIMEOUT_MILLIS
Default timeout in ms- See Also:
- Constant Field Values
-
DEFAULT_RETRY_ON_CLOSE
public static final boolean DEFAULT_RETRY_ON_CLOSE
Default for trying to retry on close- See Also:
- Constant Field Values
-
-
Constructor Detail
-
WebSocketClient
public WebSocketClient(java.net.URI endpoint, WebSocketListener listener, int attempts, long timeoutMillis, boolean retryOnClose) throws java.lang.Exception
Constructs 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:
java.lang.Exception
- on thread interrupt or connection failure
-
WebSocketClient
public WebSocketClient(java.net.URI endpoint, WebSocketListener listener) throws java.lang.Exception
Constructs the client- Parameters:
endpoint
- the URI to connect tolistener
- a WebSocketListener to handle incoming messages- Throws:
java.lang.Exception
- thread interrupt or connection failure
-
-
Method Detail
-
connect
public void connect() throws java.lang.Exception
Connect to server- Throws:
java.lang.Exception
- if error occurs
-
onOpen
public void onOpen(javax.websocket.Session session) throws java.io.IOException
Sets the session and listener- Parameters:
session
- Session- Throws:
java.io.IOException
- if IO error occurs
-
onClose
public void onClose(javax.websocket.Session session, javax.websocket.CloseReason reason) throws java.io.IOException
Closes the session on the lister, sets constructor session to null Check if should be retryed- Parameters:
session
- Sessionreason
- for close- Throws:
java.io.IOException
- if IO error occurs
-
onMessage
public void onMessage(java.lang.String message) throws java.io.IOException
Gives listener the message- Parameters:
message
- String- Throws:
java.io.IOException
- if IO error occurs
-
shutdown
public void shutdown() throws java.lang.Exception
Sets retry to false, then closes session- Throws:
java.lang.Exception
- if error occurs
-
setListener
public void setListener(WebSocketListener listener)
- Parameters:
listener
- set WebSocketListener
-
isConnected
public boolean isConnected() throws java.io.IOException
Checks if there is an open session- Returns:
- boolean
- Throws:
java.io.IOException
- if IO error occurs
-
-