Interface WebSocketListener

    • Method Detail

      • onStateChanged

        void onStateChanged​(WebSocket websocket,
                            WebSocketState newState)
                     throws java.lang.Exception
        Called after the state of the WebSocket changed.
        Parameters:
        websocket - The WebSocket.
        newState - The new state of the WebSocket.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
        Since:
        1.1
      • onConnected

        void onConnected​(WebSocket websocket,
                         java.util.Map<java.lang.String,​java.util.List<java.lang.String>> headers)
                  throws java.lang.Exception
        Called after the opening handshake of the WebSocket connection succeeded.
        Parameters:
        websocket - The WebSsocket.
        headers - HTTP headers received from the server. Keys of the map are HTTP header names such as "Sec-WebSocket-Accept". Note that the comparator used by the map is String.CASE_INSENSITIVE_ORDER.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
      • onDisconnected

        void onDisconnected​(WebSocket websocket,
                            WebSocketFrame serverCloseFrame,
                            WebSocketFrame clientCloseFrame,
                            boolean closedByServer)
                     throws java.lang.Exception
        Called after the WebSocket connection was closed.
        Parameters:
        websocket - The WebSocket.
        serverCloseFrame - The close frame which the server sent to this client. This may be null.
        clientCloseFrame - The close frame which this client sent to the server. This may be null.
        closedByServer - true if the closing handshake was started by the server. false if the closing handshake was started by the client.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
      • onFrame

        void onFrame​(WebSocket websocket,
                     WebSocketFrame frame)
              throws java.lang.Exception
        Called when a frame was received. This method is called before an onXxxFrame method is called.
        Parameters:
        websocket - The WebSocket.
        frame - The frame.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
      • onContinuationFrame

        void onContinuationFrame​(WebSocket websocket,
                                 WebSocketFrame frame)
                          throws java.lang.Exception
        Called when a continuation frame (opcode = 0x0) was received.
        Parameters:
        websocket - The WebSocket.
        frame - The continuation frame.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
      • onTextFrame

        void onTextFrame​(WebSocket websocket,
                         WebSocketFrame frame)
                  throws java.lang.Exception
        Called when a text frame (opcode = 0x1) was received.
        Parameters:
        websocket - The WebSocket.
        frame - The text frame.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
      • onBinaryFrame

        void onBinaryFrame​(WebSocket websocket,
                           WebSocketFrame frame)
                    throws java.lang.Exception
        Called when a binary frame (opcode = 0x2) was received.
        Parameters:
        websocket - The WebSocket.
        frame - The binary frame.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
      • onBinaryMessage

        void onBinaryMessage​(WebSocket websocket,
                             byte[] binary)
                      throws java.lang.Exception
        Called when a binary message was received.
        Parameters:
        websocket - The WebSocket.
        binary - The binary message.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
      • onSendingFrame

        void onSendingFrame​(WebSocket websocket,
                            WebSocketFrame frame)
                     throws java.lang.Exception
        Called before a WebSocket frame is sent.
        Parameters:
        websocket - The WebSocket.
        frame - The WebSocket frame to be sent.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
        Since:
        1.15
      • onFrameSent

        void onFrameSent​(WebSocket websocket,
                         WebSocketFrame frame)
                  throws java.lang.Exception
        Called when a WebSocket frame was sent to the server (but not flushed yet).
        Parameters:
        websocket - The WebSocket.
        frame - The sent frame.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
      • onFrameUnsent

        void onFrameUnsent​(WebSocket websocket,
                           WebSocketFrame frame)
                    throws java.lang.Exception
        Called when a WebSocket frame was not sent to the server because a close frame has already been sent.

        Note that onFrameUnsent is not called when onSendError is called.

        Parameters:
        websocket - The WebSocket.
        frame - The unsent frame.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
      • onThreadCreated

        void onThreadCreated​(WebSocket websocket,
                             ThreadType threadType,
                             java.lang.Thread thread)
                      throws java.lang.Exception
        Called between after a thread is created and before the thread's start() method is called.
        Parameters:
        websocket - The WebSocket.
        threadType - The thread type.
        thread - The newly created thread instance.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
        Since:
        2.0
      • onThreadStarted

        void onThreadStarted​(WebSocket websocket,
                             ThreadType threadType,
                             java.lang.Thread thread)
                      throws java.lang.Exception
        Called at the very beginning of the thread's run() method implementation.
        Parameters:
        websocket - The WebSocket.
        threadType - The thread type.
        thread - The thread instance.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
        Since:
        2.0
      • onThreadStopping

        void onThreadStopping​(WebSocket websocket,
                              ThreadType threadType,
                              java.lang.Thread thread)
                       throws java.lang.Exception
        Called at the very end of the thread's run() method implementation.
        Parameters:
        websocket - The WebSocket.
        threadType - The thread type.
        thread - The thread instance.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
        Since:
        2.0
      • onError

        void onError​(WebSocket websocket,
                     WebSocketException cause)
              throws java.lang.Exception
        Call when an error occurred. This method is called before an onXxxError method is called.
        Parameters:
        websocket - The WebSocket.
        cause - An exception that represents the error.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
      • onFrameError

        void onFrameError​(WebSocket websocket,
                          WebSocketException cause,
                          WebSocketFrame frame)
                   throws java.lang.Exception
        Called when a WebSocket frame failed to be read from the WebSocket.

        Some WebSocket server implementations close a WebSocket connection without sending a close frame to a client in some cases. Strictly speaking, this behavior is a violation against the specification (RFC 6455). However, this library has allowed the behavior by default since the version 1.29. Even if the end of the input stream of a WebSocket connection were reached without a close frame being received, it would trigger neither onError() method nor onFrameError() method. If you want to make this library report an error in the case, pass false to WebSocket.setMissingCloseFrameAllowed(boolean) method.

        Parameters:
        websocket - The WebSocket.
        cause - An exception that represents the error. When the error occurred because of InterruptedIOException, exception.getError() returns WebSocketError.INTERRUPTED_IN_READING. For other IO errors, exception.getError() returns WebSocketError.IO_ERROR_IN_READING. Other error codes denote protocol errors, which imply that some bugs may exist in either or both of the client-side and the server-side implementations.
        frame - The WebSocket frame. If this is not null, it means that verification of the frame failed.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
      • onMessageError

        void onMessageError​(WebSocket websocket,
                            WebSocketException cause,
                            java.util.List<WebSocketFrame> frames)
                     throws java.lang.Exception
        Called when it failed to concatenate payloads of multiple frames to construct a message. The reason of the failure is probably out-of-memory.
        Parameters:
        websocket - The WebSocket.
        cause - An exception that represents the error.
        frames - The list of frames that form a message. The first element is either a text frame and a binary frame, and the other frames are continuation frames.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
      • onMessageDecompressionError

        void onMessageDecompressionError​(WebSocket websocket,
                                         WebSocketException cause,
                                         byte[] compressed)
                                  throws java.lang.Exception
        Called when a message failed to be decompressed.
        Parameters:
        websocket - The WebSocket.
        cause - An exception that represents the error.
        compressed - The compressed message that failed to be decompressed.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
        Since:
        1.16
      • onTextMessageError

        void onTextMessageError​(WebSocket websocket,
                                WebSocketException cause,
                                byte[] data)
                         throws java.lang.Exception
        Called when it failed to convert payload data into a string. The reason of the failure is probably out-of-memory.
        Parameters:
        websocket - The WebSocket.
        cause - An exception that represents the error.
        data - The payload data that failed to be converted to a string.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
      • onSendError

        void onSendError​(WebSocket websocket,
                         WebSocketException cause,
                         WebSocketFrame frame)
                  throws java.lang.Exception
        Called when an error occurred when a frame was tried to be sent to the server.
        Parameters:
        websocket - The WebSocket.
        cause - An exception that represents the error.
        frame - The frame which was tried to be sent. This is null when the error code of the exception is FLUSH_ERROR.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
      • onUnexpectedError

        void onUnexpectedError​(WebSocket websocket,
                               WebSocketException cause)
                        throws java.lang.Exception
        Called when an uncaught throwable was detected in either the reading thread (which reads frames from the server) or the writing thread (which sends frames to the server).
        Parameters:
        websocket - The WebSocket.
        cause - The cause of the error.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
      • handleCallbackError

        void handleCallbackError​(WebSocket websocket,
                                 java.lang.Throwable cause)
                          throws java.lang.Exception
        Called when an onXxx() method threw a Throwable.
        Parameters:
        websocket - The WebSocket.
        cause - The Throwable an onXxx method threw.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is just ignored.
        Since:
        1.9
      • onSendingHandshake

        void onSendingHandshake​(WebSocket websocket,
                                java.lang.String requestLine,
                                java.util.List<java.lang.String[]> headers)
                         throws java.lang.Exception
        Called before an opening handshake is sent to the server.
        Parameters:
        websocket - The WebSocket.
        requestLine - The request line. For example, "GET /chat HTTP/1.1".
        headers - The HTTP headers.
        Throws:
        java.lang.Exception - An exception thrown by an implementation of this method. The exception is passed to handleCallbackError(WebSocket, Throwable).
        Since:
        1.21