-
- Enclosing interface:
- WebSocket
public static interface WebSocket.ListenerThe receiving interface ofWebSocket.A
WebSocketinvokes methods of the associated listener passing itself as an argument. These methods are invoked in a thread-safe manner, such that the next invocation may start only after the previous one has finished.When data has been received, the
WebSocketinvokes a receive method. MethodsonText,onBinary,onPingandonPongmust return aCompletionStagethat completes once the message has been received by the listener. If a listener's method returnsnullrather than aCompletionStage,WebSocketwill behave as if the listener returned aCompletionStagethat is already completed normally.An
IOExceptionraised inWebSocketwill result in an invocation ofonErrorwith that exception (if the input is not closed). Unless otherwise stated if the listener's method throws an exception or aCompletionStagereturned from a method completes exceptionally, the WebSocket will invokeonErrorwith this exception.- Since:
- 11
-
-
Method Summary
All Methods Instance Methods Default Methods Modifier and Type Method Description default CompletionStage<?>onBinary(WebSocket webSocket, ByteBuffer data, boolean last)A binary data has been received.default CompletionStage<?>onClose(WebSocket webSocket, int statusCode, String reason)Receives a Close message indicating the WebSocket's input has been closed.default voidonError(WebSocket webSocket, Throwable error)An error has occurred.default voidonOpen(WebSocket webSocket)AWebSockethas been connected.default CompletionStage<?>onPing(WebSocket webSocket, ByteBuffer message)A Ping message has been received.default CompletionStage<?>onPong(WebSocket webSocket, ByteBuffer message)A Pong message has been received.default CompletionStage<?>onText(WebSocket webSocket, CharSequence data, boolean last)A textual data has been received.
-
-
-
Method Detail
-
onOpen
default void onOpen(WebSocket webSocket)
AWebSockethas been connected.This is the initial invocation and it is made once. It is typically used to make a request for more invocations.
- Parameters:
webSocket- the WebSocket that has been connected
-
onText
default CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last)
A textual data has been received.Return a
CompletionStagewhich will be used by theWebSocketas an indication it may reclaim theCharSequence. Do not access theCharSequenceafter thisCompletionStagehas completed.- Parameters:
webSocket- the WebSocket on which the data has been receiveddata- the datalast- whether this invocation completes the message- Returns:
- a
CompletionStagewhich completes when theCharSequencemay be reclaimed; ornullif it may be reclaimed immediately
-
onBinary
default CompletionStage<?> onBinary(WebSocket webSocket, ByteBuffer data, boolean last)
A binary data has been received.This data is located in bytes from the buffer's position to its limit.
Return a
CompletionStagewhich will be used by theWebSocketas an indication it may reclaim theByteBuffer. Do not access theByteBufferafter thisCompletionStagehas completed.- Parameters:
webSocket- the WebSocket on which the data has been receiveddata- the datalast- whether this invocation completes the message- Returns:
- a
CompletionStagewhich completes when theByteBuffermay be reclaimed; ornullif it may be reclaimed immediately
-
onPing
default CompletionStage<?> onPing(WebSocket webSocket, ByteBuffer message)
A Ping message has been received.As guaranteed by the WebSocket Protocol, the message consists of not more than
125bytes. These bytes are located from the buffer's position to its limit.Given that the WebSocket implementation will automatically send a reciprocal pong when a ping is received, it is rarely required to send a pong message explicitly when a ping is received.
Return a
CompletionStagewhich will be used by theWebSocketas a signal it may reclaim theByteBuffer. Do not access theByteBufferafter thisCompletionStagehas completed.- Parameters:
webSocket- the WebSocket on which the message has been receivedmessage- the message- Returns:
- a
CompletionStagewhich completes when theByteBuffermay be reclaimed; ornullif it may be reclaimed immediately
-
onPong
default CompletionStage<?> onPong(WebSocket webSocket, ByteBuffer message)
A Pong message has been received.As guaranteed by the WebSocket Protocol, the message consists of not more than
125bytes. These bytes are located from the buffer's position to its limit.Return a
CompletionStagewhich will be used by theWebSocketas a signal it may reclaim theByteBuffer. Do not access theByteBufferafter thisCompletionStagehas completed.- Parameters:
webSocket- the WebSocket on which the message has been receivedmessage- the message- Returns:
- a
CompletionStagewhich completes when theByteBuffermay be reclaimed; ornullif it may be reclaimed immediately
-
onClose
default CompletionStage<?> onClose(WebSocket webSocket, int statusCode, String reason)
Receives a Close message indicating the WebSocket's input has been closed.This is the last invocation from the specified
WebSocket. By the time this invocation begins the WebSocket's input will have been closed.A Close message consists of a status code and a reason for closing. The status code is an integer from the range
1000 <= code <= 65535. Thereasonis a string which has a UTF-8 representation not longer than123bytes.If the WebSocket's output is not already closed, the
CompletionStagereturned by this method will be used as an indication that the WebSocket's output may be closed. The WebSocket will close its output at the earliest of completion of the returnedCompletionStageor invoking either of thesendCloseorabortmethods.- Parameters:
webSocket- the WebSocket on which the message has been receivedstatusCode- the status codereason- the reason- Returns:
- a
CompletionStagewhich completes when theWebSocketmay be closed; ornullif it may be closed immediately
-
onError
default void onError(WebSocket webSocket, Throwable error)
An error has occurred.This is the last invocation from the specified WebSocket. By the time this invocation begins both the WebSocket's input and output will have been closed. A WebSocket may invoke this method on the associated listener at any time after it has invoked
onOpen, regardless of whether or not any invocations have been requested from the WebSocket.If an exception is thrown from this method, resulting behavior is undefined.
- Parameters:
webSocket- the WebSocket on which the error has occurrederror- the error
-
-