Class WebSocketClient

java.lang.Object
gov.usgs.earthquake.distribution.WebSocketClient
All Implemented Interfaces:
Runnable

public class WebSocketClient extends Object implements Runnable
Manages a simple connection to a websocket. Can also be overridden for more complex behavior.
  • Field Details

    • DEFAULT_ATTEMPTS

      public static final int DEFAULT_ATTEMPTS
      Default number of attempts
      See Also:
    • DEFAULT_TIMEOUT_MILLIS

      public static final long DEFAULT_TIMEOUT_MILLIS
      Default timeout in ms
      See Also:
    • DEFAULT_RETRY_ON_CLOSE

      public static final boolean DEFAULT_RETRY_ON_CLOSE
      Default for trying to retry on close
      See Also:
    • DEFAULT_PING_INTERVAL_MILLIS

      public static final long DEFAULT_PING_INTERVAL_MILLIS
      Default for time in between pings to server
      See Also:
    • DEFAULT_PING_WAIT_MILLIS

      public static final long DEFAULT_PING_WAIT_MILLIS
      Default 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_MILLIS
      Default 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
      Constructs the client. Also connects to the server.
      Parameters:
      endpoint - the URI to connect to
      listener - a WebSocketListener to handle incoming messages
      attempts - an integer number of times to try the connection
      timeoutMillis - a long for the wait time between attempts
      retryOnClose - 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 to
      listener - a WebSocketListener to handle incoming messages
      attempts - an integer number of times to try the connection
      timeoutMillis - a long for the wait time between attempts
      retryOnClose - boolean for if the connection should retry when closed
      pingIntervalMillis - 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

      public WebSocketClient(URI endpoint, WebSocketListener listener) throws Exception
      Constructs the client
      Parameters:
      endpoint - the URI to connect to
      listener - 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

      public void connect() throws Exception
      Connect to server
      Throws:
      Exception - if error occurs
    • isConnected

      public boolean isConnected() throws IOException
      Checks if there is an open session
      Returns:
      boolean
      Throws:
      IOException - if IO error occurs
    • onOpen

      public void onOpen(javax.websocket.Session session) throws IOException
      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 - Session
      reason - for close
      Throws:
      IOException - if IO error occurs
    • onMessage

      public void onMessage(String message) throws IOException
      Gives listener the message
      Parameters:
      message - String
      Throws:
      IOException - if IO error occurs
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • shutdown

      public void shutdown() throws Exception
      Sets retry to false, then closes session
      Throws:
      Exception - if error occurs
    • setListener

      public void setListener(WebSocketListener listener)
      Parameters:
      listener - set WebSocketListener