-
- Enclosing interface:
- WebSocket
public static interface WebSocket.Listener
The receiving interface ofWebSocket
.
Incubating Feature. Will be removed in a future release.A
WebSocket
invokes methods on its listener when it receives messages or encounters events. The invokingWebSocket
is passed as an argument toListener
's methods. AWebSocket
invokes methods on its listener in a thread-safe manner.Unless otherwise stated if a listener's method throws an exception or a
CompletionStage
returned from a method completes exceptionally, theWebSocket
will invokeonError
with this exception.If a listener's method returns
null
rather than aCompletionStage
,WebSocket
will behave as if the listener returned aCompletionStage
that is already completed normally.- Since:
- 9
-
-
Method Summary
All Methods Instance Methods Default Methods Modifier and Type Method Description default CompletionStage<?>
onBinary(WebSocket webSocket, ByteBuffer message, WebSocket.MessagePart part)
A Binary message has been received.default CompletionStage<?>
onClose(WebSocket webSocket, int statusCode, String reason)
A Close message has been received.default void
onError(WebSocket webSocket, Throwable error)
An unrecoverable error has occurred.default void
onOpen(WebSocket webSocket)
AWebSocket
has 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 message, WebSocket.MessagePart part)
A Text message has been received.
-
-
-
Method Detail
-
onOpen
default void onOpen(WebSocket webSocket)
AWebSocket
has been connected.This is the first invocation and it is made at most once. This method is typically used to make an initial request for messages.
- Implementation Requirements:
- The default implementation of this method behaves as if:
webSocket.request(1);
- Parameters:
webSocket
- the WebSocket that has been connected
-
onText
default CompletionStage<?> onText(WebSocket webSocket, CharSequence message, WebSocket.MessagePart part)
A Text message has been received.If a whole message has been received, this method will be invoked with
MessagePart.WHOLE
marker. Otherwise, it will be invoked withFIRST
, possibly a number of times withPART
and, finally, withLAST
markers. If this message is partial, it may be an incomplete UTF-16 sequence. However, the concatenation of all messages through the last will be a complete UTF-16 sequence.Return a
CompletionStage
which will be used by theWebSocket
as a signal it may reclaim theCharSequence
. Do not access theCharSequence
after this has completed.- Implementation Requirements:
- The default implementation of this method behaves as if:
webSocket.request(1); return null;
- Implementation Note:
- This method is always invoked with character sequences which are complete UTF-16 sequences.
- Parameters:
webSocket
- the WebSocket on which the message has been receivedmessage
- the messagepart
- the part- Returns:
- a
CompletionStage
which completes when theCharSequence
may be reclaimed; ornull
if it may be reclaimed immediately
-
onBinary
default CompletionStage<?> onBinary(WebSocket webSocket, ByteBuffer message, WebSocket.MessagePart part)
A Binary message has been received.If a whole message has been received, this method will be invoked with
MessagePart.WHOLE
marker. Otherwise, it will be invoked withFIRST
, possibly a number of times withPART
and, finally, withLAST
markers.This message consists of bytes from the buffer's position to its limit.
Return a
CompletionStage
which will be used by theWebSocket
as a signal it may reclaim theByteBuffer
. Do not access theByteBuffer
after this has completed.- Implementation Requirements:
- The default implementation of this method behaves as if:
webSocket.request(1); return null;
- Parameters:
webSocket
- the WebSocket on which the message has been receivedmessage
- the messagepart
- the part- Returns:
- a
CompletionStage
which completes when theByteBuffer
may be reclaimed; ornull
if it may be reclaimed immediately
-
onPing
default CompletionStage<?> onPing(WebSocket webSocket, ByteBuffer message)
A Ping message has been received.The message consists of not more than
125
bytes from the buffer's position to its limit.Return a
CompletionStage
which will be used by theWebSocket
as a signal it may reclaim theByteBuffer
. Do not access theByteBuffer
after this has completed.- Implementation Requirements:
- The default implementation of this method behaves as if:
webSocket.request(1); return null;
- Parameters:
webSocket
- the WebSocket on which the message has been receivedmessage
- the message- Returns:
- a
CompletionStage
which completes when theByteBuffer
may be reclaimed; ornull
if it may be reclaimed immediately
-
onPong
default CompletionStage<?> onPong(WebSocket webSocket, ByteBuffer message)
A Pong message has been received.The message consists of not more than
125
bytes from the buffer's position to its limit.Return a
CompletionStage
which will be used by theWebSocket
as a signal it may reclaim theByteBuffer
. Do not access theByteBuffer
after this has completed.- Implementation Requirements:
- The default implementation of this method behaves as if:
webSocket.request(1); return null;
- Parameters:
webSocket
- the WebSocket on which the message has been receivedmessage
- the message- Returns:
- a
CompletionStage
which completes when theByteBuffer
may be reclaimed; ornull
if it may be reclaimed immediately
-
onClose
default CompletionStage<?> onClose(WebSocket webSocket, int statusCode, String reason)
A Close message has been received.This is the last invocation from the
WebSocket
. By the time this invocation begins theWebSocket
's input will have been closed. Be prepared to receive this invocation at any time afteronOpen
regardless of whether or not any messages have been requested from theWebSocket
.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
. Thereason
is a string which has an UTF-8 representation not longer than123
bytes.Return a
CompletionStage
that will be used by theWebSocket
as a signal that it may close the output. TheWebSocket
will close the output at the earliest of completion of the returnedCompletionStage
or invoking asendClose
method.If an exception is thrown from this method or a
CompletionStage
returned from it completes exceptionally, the resulting behaviour is undefined.- Parameters:
webSocket
- the WebSocket on which the message has been receivedstatusCode
- the status codereason
- the reason- Returns:
- a
CompletionStage
which completes when theWebSocket
may be closed; ornull
if it may be closed immediately
-
onError
default void onError(WebSocket webSocket, Throwable error)
An unrecoverable error has occurred.This is the last invocation from the
WebSocket
. By the time this invocation begins bothWebSocket
's input and output will have been closed. Be prepared to receive this invocation at any time afteronOpen
regardless of whether or not any messages have been requested from theWebSocket
.If an exception is thrown from this method, resulting behavior is undefined.
- Parameters:
webSocket
- the WebSocket on which the error has occurrederror
- the error
-
-