-
- Enclosing interface:
- WebSocket
public static interface WebSocket.ListenerThe receiving interface ofWebSocket.
Incubating Feature. Will be removed in a future release.A
WebSocketinvokes methods on its listener when it receives messages or encounters events. The invokingWebSocketis passed as an argument toListener's methods. AWebSocketinvokes methods on its listener in a thread-safe manner.Unless otherwise stated if a listener's method throws an exception or a
CompletionStagereturned from a method completes exceptionally, theWebSocketwill invokeonErrorwith this exception.If a listener's method returns
nullrather than aCompletionStage,WebSocketwill behave as if the listener returned aCompletionStagethat 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 voidonError(WebSocket webSocket, Throwable error)An unrecoverable 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 message, WebSocket.MessagePart part)A Text message has been received.
-
-
-
Method Detail
-
onOpen
default void onOpen(WebSocket webSocket)
AWebSockethas 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.WHOLEmarker. Otherwise, it will be invoked withFIRST, possibly a number of times withPARTand, finally, withLASTmarkers. 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
CompletionStagewhich will be used by theWebSocketas a signal it may reclaim theCharSequence. Do not access theCharSequenceafter 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
CompletionStagewhich completes when theCharSequencemay be reclaimed; ornullif 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.WHOLEmarker. Otherwise, it will be invoked withFIRST, possibly a number of times withPARTand, finally, withLASTmarkers.This message consists of bytes 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 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
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.The message consists of not more than
125bytes 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 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
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.The message consists of not more than
125bytes 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 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
CompletionStagewhich completes when theByteBuffermay be reclaimed; ornullif 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 afteronOpenregardless 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. Thereasonis a string which has an UTF-8 representation not longer than123bytes.Return a
CompletionStagethat will be used by theWebSocketas a signal that it may close the output. TheWebSocketwill close the output at the earliest of completion of the returnedCompletionStageor invoking asendClosemethod.If an exception is thrown from this method or a
CompletionStagereturned 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
CompletionStagewhich completes when theWebSocketmay be closed; ornullif 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 afteronOpenregardless 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
-
-