Class AuthenticatedWebSocketHandler

  • All Implemented Interfaces:
    java.lang.AutoCloseable
    Direct Known Subclasses:
    ActionWebSocket, EventWebSocket

    public abstract class AuthenticatedWebSocketHandler
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Handles websockets. Tries to renew any aborted connection until the websocket gets closed from this side.
    • Field Detail

      • name

        protected final java.lang.String name
      • apiName

        protected final java.lang.String apiName
      • endpoint

        protected final java.lang.String endpoint
      • protocol

        protected final java.lang.String protocol
      • maxRetries

        protected final int maxRetries
      • fragment

        protected final java.lang.String fragment
      • reconnectOnFailedSend

        protected final boolean reconnectOnFailedSend
      • webSocketRequestTimeout

        protected final long webSocketRequestTimeout
    • Constructor Detail

      • AuthenticatedWebSocketHandler

        protected AuthenticatedWebSocketHandler​(AuthenticatedWebSocketHandler.Conf<?> builder)
        Protected Constructor
        Parameters:
        builder - The builder for this Handler.
    • Method Detail

      • createWebSocket

        protected void createWebSocket()
                                throws HiroException,
                                       java.io.IOException,
                                       java.lang.InterruptedException
        Create new WebSocket. Closes any old websocket.
        Throws:
        HiroException - When creation of the webSocket fails.
        java.io.IOException - On IO problems.
        java.lang.InterruptedException - When the call gets interrupted.
      • closeWebSocket

        protected void closeWebSocket​(int status,
                                      java.lang.String reason)
        Close the websocket and set webSocket to null.
        Parameters:
        status - The status for the close message
        reason - The reason string for the close message.
      • restartWebSocket

        protected void restartWebSocket​(boolean refreshToken)
                                 throws HiroException,
                                        java.io.IOException,
                                        java.lang.InterruptedException
        Restarts the websocket by closing the old and creating a new one. Uses the same internalListener. Delays the restart according to backoff(int reconnectDelay).
        Parameters:
        refreshToken - Refresh the current token.
        Throws:
        HiroException - When creation of the webSocket fails.
        java.io.IOException - On IO problems.
        java.lang.InterruptedException - When the call gets interrupted.
      • backoff

        protected int backoff​(int reconnectDelay)
                       throws java.lang.InterruptedException
        Backoff operations that fail. The delay increases with every try up to 10 minutes.
        Parameters:
        reconnectDelay - The old delay value (seconds).
        Returns:
        The new delay value (seconds).
        Throws:
        java.lang.InterruptedException - When Thread.sleep(long) gets interrupted.
        See Also:
        send(String), restartWebSocket(boolean)
      • send

        public void send​(java.lang.String message)
                  throws HiroException,
                         java.lang.InterruptedException,
                         java.io.IOException
        Send a message across the websocket. Tries maxRetries times to send a message before resetting the websocket.
        Parameters:
        message - The message to send.
        Throws:
        HiroException - When sending finally fails.
        java.io.IOException - On IO problems.
        java.lang.InterruptedException - When the call gets interrupted.
      • start

        public void start()
                   throws HiroException,
                          java.io.IOException,
                          java.lang.InterruptedException
        Throws:
        HiroException
        java.io.IOException
        java.lang.InterruptedException
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable