-
- Enclosing interface:
- WebSocket
public static interface WebSocket.Listener
A listener for events and messages on aWebSocket
.
Incubating Feature. Will be removed in a future release.Each method of
Listener
corresponds to a type of event or a type of message. TheWebSocket
argument of the method is theWebSocket
the event has occurred (the message has been received) on. All methods with the sameWebSocket
argument are invoked in a sequential (and happens-before) order, one after another, possibly by different threads.-
onOpen
This method is invoked first. -
onText
,onBinary
,onPing
andonPong
These methods are invoked zero or more times afteronOpen
. -
onClose
,onError
Only one of these methods is invoked, and that method is invoked last.
Messages received by the
Listener
conform to the WebSocket Protocol, otherwiseonError
with aProtocolException
is invoked.If a whole message is received, then the corresponding method (
onText
oronBinary
) will be invoked withWHOLE
marker. Otherwise the method will be invoked withFIRST
, zero or more times withPART
and, finally, withLAST
markers. If any of the methods above throws an exception,onError
is then invoked with the sameWebSocket
and this exception. Exceptions thrown fromonError
oronClose
are ignored.When the method returns, the message is deemed received (in particular, if contained in a
ByteBuffer buffer
, the data is deemed received completely regardless of the resultbuffer.hasRemaining()
upon the method's return. After this further messages may be received.These invocations begin asynchronous processing which might not end with the invocation. To provide coordination, methods of
Listener
return aCompletionStage
. TheCompletionStage
signals theWebSocket
that the processing of a message has ended. For convenience, methods may returnnull
, which (by convention) means the same as returning an already completed (normally)CompletionStage
. If the returnedCompletionStage
completes exceptionally, thenonError
will be invoked with the sameWebSocket
and this exception.Control of the message passes to the
Listener
with the invocation of the method. Control of the message returns to theWebSocket
at the earliest of, either returningnull
from the method, or the completion of theCompletionStage
returned from the method. TheWebSocket
does not access the message while it's not in its control. TheListener
must not access the message after its control has been returned to theWebSocket
.A
WebSocket
implementation never invokesListener
's methods withnull
s as their arguments.- 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)
Receives a Binary message.default CompletionStage<?>
onClose(WebSocket webSocket, int statusCode, String reason)
Receives a Close message.default void
onError(WebSocket webSocket, Throwable error)
Notifies an I/O or protocol error has occurred.default void
onOpen(WebSocket webSocket)
Notifies theListener
that it is connected to the providedWebSocket
.default CompletionStage<?>
onPing(WebSocket webSocket, ByteBuffer message)
Receives a Ping message.default CompletionStage<?>
onPong(WebSocket webSocket, ByteBuffer message)
Receives a Pong message.default CompletionStage<?>
onText(WebSocket webSocket, CharSequence message, WebSocket.MessagePart part)
Receives a Text message.
-
-
-
Method Detail
-
onOpen
default void onOpen(WebSocket webSocket)
Notifies theListener
that it is connected to the providedWebSocket
.The
onOpen
method does not correspond to any message from the WebSocket Protocol. It is a synthetic event and the firstListener
's method to be invoked.This method is usually used to make an initial request for messages.
If an exception is thrown from this method then
onError
will be invoked with the sameWebSocket
and this exception.- Implementation Requirements:
- The default implementation of this method behaves as if:
webSocket.request(1);
- Parameters:
webSocket
- the WebSocket
-
onText
default CompletionStage<?> onText(WebSocket webSocket, CharSequence message, WebSocket.MessagePart part)
Receives a Text message.The
onText
method is invoked zero or more times betweenonOpen
and (onClose
oronError
).This message may be a partial UTF-16 sequence. However, the concatenation of all messages through the last will be a whole UTF-16 sequence.
If an exception is thrown from this method or the returned
CompletionStage
completes exceptionally, thenonError
will be invoked with the sameWebSocket
and this exception.- Implementation Requirements:
- The default implementation of this method behaves as if:
webSocket.request(1); return null;
- Implementation Note:
- This implementation passes only complete UTF-16 sequences
to the
onText
method. - Parameters:
webSocket
- the WebSocketmessage
- the messagepart
- the part- Returns:
- a
CompletionStage
which completes when the message processing is done; ornull
if already done
-
onBinary
default CompletionStage<?> onBinary(WebSocket webSocket, ByteBuffer message, WebSocket.MessagePart part)
Receives a Binary message.The
onBinary
method is invoked zero or more times betweenonOpen
and (onClose
oronError
).If an exception is thrown from this method or the returned
CompletionStage
completes exceptionally, thenonError
will be invoked with the sameWebSocket
and this exception.- Implementation Requirements:
- The default implementation of this method behaves as if:
webSocket.request(1); return null;
- Parameters:
webSocket
- the WebSocketmessage
- the messagepart
- the part- Returns:
- a
CompletionStage
which completes when the message processing is done; ornull
if already done
-
onPing
default CompletionStage<?> onPing(WebSocket webSocket, ByteBuffer message)
Receives a Ping message.A Ping message may be sent or received by either client or server. It may serve either as a keepalive or as a means to verify that the remote endpoint is still responsive.
The
WebSocket
handles Ping messages by replying with appropriate Pong messages using a strategy of its choice, but within the boundaries of the WebSocket Protocol. TheWebSocket
may invokeonPing
after handling a Ping message, before doing so or in parallel with it. In other words no particular ordering is guaranteed. If an error occurs while implementation handles this Ping message, thenonError
will be invoked with this error. For more details on handling Ping messages see RFC 6455 sections 5.5.2. Ping and 5.5.3. Pong.The message will consist of not more than
125
bytes:message.remaining() <= 125
.The
onPing
is invoked zero or more times in betweenonOpen
and (onClose
oronError
).If an exception is thrown from this method or the returned
CompletionStage
completes exceptionally, thenonError
will be invoked with this exception.- Implementation Requirements:
- The default implementation of this method behaves as if:
webSocket.request(1); return null;
- Parameters:
webSocket
- the WebSocketmessage
- the message- Returns:
- a
CompletionStage
which completes when the message processing is done; ornull
if already done
-
onPong
default CompletionStage<?> onPong(WebSocket webSocket, ByteBuffer message)
Receives a Pong message.A Pong message may be unsolicited or may be received in response to a previously sent Ping. In the latter case, the contents of the Pong is identical to the originating Ping.
The message will consist of not more than
125
bytes:message.remaining() <= 125
.The
onPong
method is invoked zero or more times in betweenonOpen
and (onClose
oronError
).If an exception is thrown from this method or the returned
CompletionStage
completes exceptionally, thenonError
will be invoked with this exception.- Implementation Requirements:
- The default implementation of this method behaves as if:
webSocket.request(1); return null;
- Parameters:
webSocket
- the WebSocketmessage
- the message- Returns:
- a
CompletionStage
which completes when the message processing is done; ornull
if already done
-
onClose
default CompletionStage<?> onClose(WebSocket webSocket, int statusCode, String reason)
Receives a Close message.A Close message consists of a status code and a reason for closing. The status code is an integer in the range
1000 <= code <= 65535
. Thereason
is a short string that has an UTF-8 representation not longer than123
bytes. For more details on Close message, status codes and reason see RFC 6455 sections 5.5.1. Close and 7.4. Status Codes.After the returned
CompletionStage
has completed (normally or exceptionally), theWebSocket
completes the closing handshake by replying with an appropriate Close message.This implementation replies with a Close message that has the same code this message has and an empty reason.
onClose
is the last invocation on theListener
. It is invoked at most once, but afteronOpen
. If an exception is thrown from this method, it is ignored.The
WebSocket
will close at the earliest of completion of the returnedCompletionStage
or sending a Close message. In particular, if a Close message has been sent before, then this invocation completes the closing handshake and by the time this method is invoked, theWebSocket
will have been closed.- Implementation Requirements:
- The default implementation of this method behaves as if:
return null;
- Parameters:
webSocket
- the WebSocketstatusCode
- the status codereason
- the reason- Returns:
- a
CompletionStage
which completes when theWebSocket
can be closed; ornull
if it can be closed immediately - See Also:
WebSocket.NORMAL_CLOSURE
-
onError
default void onError(WebSocket webSocket, Throwable error)
Notifies an I/O or protocol error has occurred.The
onError
method does not correspond to any message from the WebSocket Protocol. It is a synthetic event and the lastListener
's method to be invoked. It is invoked at most once but afteronOpen
. If an exception is thrown from this method, it is ignored.Note that the WebSocket Protocol requires some errors occur in the incoming destination must be fatal to the connection. In such cases the implementation takes care of Failing the WebSocket Connection: by the time
onError
is invoked, theWebSocket
will have been closed. Any outstanding or subsequent send operation will complete exceptionally with anIOException
. For more details on Failing the WebSocket Connection see RFC 6455 section 7.1.7. Fail the WebSocket Connection.- API Note:
- Errors associated with sending messages are reported to the
CompletableFuture
ssendX
methods return, rather than to this this method. - Implementation Requirements:
- The default implementation of this method does nothing.
- Parameters:
webSocket
- the WebSocketerror
- the error
-
-