Class WebSocketClient


  • public class WebSocketClient
    extends java.lang.Object
    Manages a simple connection to a websocket. Can also be overridden for more complex behavior.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void connect()
      Connect to server
      boolean isConnected()
      Checks if there is an open session
      void onClose​(javax.websocket.Session session, javax.websocket.CloseReason reason)
      Closes the session on the lister, sets constructor session to null Check if should be retryed
      void onMessage​(java.lang.String message)
      Gives listener the message
      void onOpen​(javax.websocket.Session session)
      Sets the session and listener
      void setListener​(WebSocketListener listener)  
      void shutdown()
      Sets retry to false, then closes session
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 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:
        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 to
        listener - 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 - Session
        reason - 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