Interface WebSocketListener
- All Known Implementing Classes:
WebSocketAdapter
An implementation of this interface should be added by WebSocket.addListener(WebSocketListener)
to a WebSocket
instance before calling WebSocket.connect()
.
WebSocketAdapter
is an empty implementation of this interface.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoid
handleCallbackError
(WebSocket websocket, Throwable cause) Called when anonXxx()
method threw aThrowable
.void
onBinaryFrame
(WebSocket websocket, WebSocketFrame frame) Called when a binary frame (opcode = 0x2) was received.void
onBinaryMessage
(WebSocket websocket, byte[] binary) Called when a binary message was received.void
onCloseFrame
(WebSocket websocket, WebSocketFrame frame) Called when a close frame (opcode = 0x8) was received.void
Called after the opening handshake of the WebSocket connection succeeded.void
onConnectError
(WebSocket websocket, WebSocketException cause) Called whenWebSocket.connectAsynchronously()
failed.void
onContinuationFrame
(WebSocket websocket, WebSocketFrame frame) Called when a continuation frame (opcode = 0x0) was received.void
onDisconnected
(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) Called after the WebSocket connection was closed.void
onError
(WebSocket websocket, WebSocketException cause) Call when an error occurred.void
onFrame
(WebSocket websocket, WebSocketFrame frame) Called when a frame was received.void
onFrameError
(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) Called when a WebSocket frame failed to be read from the WebSocket.void
onFrameSent
(WebSocket websocket, WebSocketFrame frame) Called when a WebSocket frame was sent to the server (but not flushed yet).void
onFrameUnsent
(WebSocket websocket, WebSocketFrame frame) Called when a WebSocket frame was not sent to the server because a close frame has already been sent.void
onMessageDecompressionError
(WebSocket websocket, WebSocketException cause, byte[] compressed) Called when a message failed to be decompressed.void
onMessageError
(WebSocket websocket, WebSocketException cause, List<WebSocketFrame> frames) Called when it failed to concatenate payloads of multiple frames to construct a message.void
onPingFrame
(WebSocket websocket, WebSocketFrame frame) Called when a ping frame (opcode = 0x9) was received.void
onPongFrame
(WebSocket websocket, WebSocketFrame frame) Called when a pong frame (opcode = 0xA) was received.void
onSendError
(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) Called when an error occurred when a frame was tried to be sent to the server.void
onSendingFrame
(WebSocket websocket, WebSocketFrame frame) Called before a WebSocket frame is sent.void
onSendingHandshake
(WebSocket websocket, String requestLine, List<String[]> headers) Called before an opening handshake is sent to the server.void
onStateChanged
(WebSocket websocket, WebSocketState newState) Called after the state of the WebSocket changed.void
onTextFrame
(WebSocket websocket, WebSocketFrame frame) Called when a text frame (opcode = 0x1) was received.void
onTextMessage
(WebSocket websocket, byte[] data) Called when a text message was received instead ofonTextMessage(WebSocket, String)
whenWebSocket.isDirectTextMessage()
returnstrue
.void
onTextMessage
(WebSocket websocket, String text) Called when a text message was received.void
onTextMessageError
(WebSocket websocket, WebSocketException cause, byte[] data) Called when it failed to convert payload data into a string.void
onThreadCreated
(WebSocket websocket, ThreadType threadType, Thread thread) Called between after a thread is created and before the thread'sstart()
method is called.void
onThreadStarted
(WebSocket websocket, ThreadType threadType, Thread thread) Called at the very beginning of the thread'srun()
method implementation.void
onThreadStopping
(WebSocket websocket, ThreadType threadType, Thread thread) Called at the very end of the thread'srun()
method implementation.void
onUnexpectedError
(WebSocket websocket, WebSocketException cause) 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).
-
Method Details
-
onStateChanged
Called after the state of the WebSocket changed.- Parameters:
websocket
- The WebSocket.newState
- The new state of the WebSocket.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.- Since:
- 1.1
-
onConnected
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 isString.CASE_INSENSITIVE_ORDER
.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onConnectError
Called whenWebSocket.connectAsynchronously()
failed.Note that this method is called only when
connectAsynchronously()
was used and theconnect()
executed in the background thread failed. Neither direct synchronousconnect()
norconnect(ExecutorService)
will trigger this callback method.- Parameters:
websocket
- The WebSocket.cause
- The exception thrown byconnect()
method.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.- Since:
- 1.8
-
onDisconnected
void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws 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 benull
.clientCloseFrame
- The close frame which this client sent to the server. This may benull
.closedByServer
-true
if the closing handshake was started by the server.false
if the closing handshake was started by the client.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onFrame
Called when a frame was received. This method is called before anonXxxFrame
method is called.- Parameters:
websocket
- The WebSocket.frame
- The frame.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onContinuationFrame
Called when a continuation frame (opcode = 0x0) was received.- Parameters:
websocket
- The WebSocket.frame
- The continuation frame.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onTextFrame
Called when a text frame (opcode = 0x1) was received.- Parameters:
websocket
- The WebSocket.frame
- The text frame.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onBinaryFrame
Called when a binary frame (opcode = 0x2) was received.- Parameters:
websocket
- The WebSocket.frame
- The binary frame.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onCloseFrame
Called when a close frame (opcode = 0x8) was received.- Parameters:
websocket
- The WebSocket.frame
- The close frame.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onPingFrame
Called when a ping frame (opcode = 0x9) was received.- Parameters:
websocket
- The WebSocket.frame
- The ping frame.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onPongFrame
Called when a pong frame (opcode = 0xA) was received.- Parameters:
websocket
- The WebSocket.frame
- The pong frame.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onTextMessage
Called when a text message was received.When
WebSocket.isDirectTextMessage()
returnstrue
,onTextMessage(WebSocket, byte[])
will be called instead of this method (since version 2.6).- Parameters:
websocket
- The WebSocket.text
- The text message.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onTextMessage
Called when a text message was received instead ofonTextMessage(WebSocket, String)
whenWebSocket.isDirectTextMessage()
returnstrue
.- Parameters:
websocket
- The WebSocket.data
- The UTF-8 byte sequence of the text message.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.- Since:
- 2.6
-
onBinaryMessage
Called when a binary message was received.- Parameters:
websocket
- The WebSocket.binary
- The binary message.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onSendingFrame
Called before a WebSocket frame is sent.- Parameters:
websocket
- The WebSocket.frame
- The WebSocket frame to be sent.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.- Since:
- 1.15
-
onFrameSent
Called when a WebSocket frame was sent to the server (but not flushed yet).- Parameters:
websocket
- The WebSocket.frame
- The sent frame.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onFrameUnsent
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 whenonSendError
is called.- Parameters:
websocket
- The WebSocket.frame
- The unsent frame.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onThreadCreated
Called between after a thread is created and before the thread'sstart()
method is called.- Parameters:
websocket
- The WebSocket.threadType
- The thread type.thread
- The newly created thread instance.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.- Since:
- 2.0
-
onThreadStarted
Called at the very beginning of the thread'srun()
method implementation.- Parameters:
websocket
- The WebSocket.threadType
- The thread type.thread
- The thread instance.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.- Since:
- 2.0
-
onThreadStopping
Called at the very end of the thread'srun()
method implementation.- Parameters:
websocket
- The WebSocket.threadType
- The thread type.thread
- The thread instance.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.- Since:
- 2.0
-
onError
Call when an error occurred. This method is called before anonXxxError
method is called.- Parameters:
websocket
- The WebSocket.cause
- An exception that represents the error.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onFrameError
void onFrameError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) throws 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 noronFrameError()
method. If you want to make this library report an error in the case, passfalse
toWebSocket.setMissingCloseFrameAllowed(boolean)
method.- Parameters:
websocket
- The WebSocket.cause
- An exception that represents the error. When the error occurred because ofInterruptedIOException
,exception.getError()
returnsWebSocketError.INTERRUPTED_IN_READING
. For other IO errors,exception.getError()
returnsWebSocketError.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 notnull
, it means that verification of the frame failed.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onMessageError
void onMessageError(WebSocket websocket, WebSocketException cause, List<WebSocketFrame> frames) throws 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:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onMessageDecompressionError
void onMessageDecompressionError(WebSocket websocket, WebSocketException cause, byte[] compressed) throws 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:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.- Since:
- 1.16
-
onTextMessageError
void onTextMessageError(WebSocket websocket, WebSocketException cause, byte[] data) throws 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:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onSendError
void onSendError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) throws 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 isnull
when the error code of the exception isFLUSH_ERROR
.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
onUnexpectedError
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:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.
-
handleCallbackError
Called when anonXxx()
method threw aThrowable
.- Parameters:
websocket
- The WebSocket.cause
- TheThrowable
anonXxx
method threw.- Throws:
Exception
- An exception thrown by an implementation of this method. The exception is just ignored.- Since:
- 1.9
-
onSendingHandshake
void onSendingHandshake(WebSocket websocket, String requestLine, List<String[]> headers) throws 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:
Exception
- An exception thrown by an implementation of this method. The exception is passed tohandleCallbackError(WebSocket, Throwable)
.- Since:
- 1.21
-