-
public interface WebSocketA WebSocket client conforming to RFC 6455.
Incubating Feature. Will be removed in a future release.A
WebSocketprovides full-duplex communication over a TCP connection.To create a
WebSocketuse a builder. Once aWebSocketis built, it's ready to send and receive messages. When theWebSocketis no longer needed it must be closed: a Close message must both be sent and received. TheWebSocketmay be also closed abruptly.Once closed the
WebSocketremains closed and cannot be reopened.Messages of type
X(whereXis one of: Text, Binary, Ping, Pong or Close) are sent and received asynchronously through theWebSocket.send{X}andWebSocket.Listener.on{X}methods respectively. Each method returns aCompletionStagewhich completes when the operation has completed.Note that messages (of any type) are received only if requested.
One outstanding send operation is permitted. No further send operation can be initiated before the previous one has completed. When sending, a message must not be modified until the returned
CompletableFuturecompletes (either normally or exceptionally).Text and Binary messages can be sent and received as a whole or in parts. A whole message is transferred as a sequence of one or more invocations of a corresponding method where the last invocation is identified via an additional method argument.
If the message is contained in a
ByteBuffer, bytes are considered arranged from thebuffer'spositionto thebuffer'slimit.Unless otherwise stated,
nullparameter values will cause methods and constructors to throwNullPointerException.- Implementation Note:
- This implementation's methods do not block before returning
a
CompletableFuture. - Since:
- 9
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceWebSocket.BuilderA builder for creatingWebSocketinstances.static interfaceWebSocket.ListenerA listener for events and messages on aWebSocket.static classWebSocket.MessagePartA marker used byWebSocket.Listenerin cases where a partial message may be received.
-
Field Summary
Fields Modifier and Type Field Description static intNORMAL_CLOSUREThe WebSocket Close message status code (1000), indicating normal closure, meaning that the purpose for which the connection was established has been fulfilled.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidabort()Closes theWebSocketabruptly.StringgetSubprotocol()Returns a subprotocol which has been chosen for thisWebSocket.booleanisClosed()Tells whether theWebSocketis closed.voidrequest(long n)Allowsnmore messages to be received by theListener.CompletableFuture<WebSocket>sendBinary(ByteBuffer message, boolean isLast)Sends a Binary message with bytes from the givenByteBuffer.CompletableFuture<WebSocket>sendClose(int statusCode, String reason)Sends a Close message with the given status code and the reason.CompletableFuture<WebSocket>sendPing(ByteBuffer message)Sends a Ping message with bytes from the given ByteBuffer.CompletableFuture<WebSocket>sendPong(ByteBuffer message)Sends a Pong message with bytes from the given ByteBuffer.CompletableFuture<WebSocket>sendText(CharSequence message, boolean isLast)Sends a Text message with characters from the givenCharSequence.
-
-
-
Method Detail
-
sendText
CompletableFuture<WebSocket> sendText(CharSequence message, boolean isLast)
Sends a Text message with characters from the givenCharSequence.Returns a
CompletableFuture<WebSocket>which completes normally when the message has been sent or completes exceptionally if an error occurs.The
CharSequencemust not be modified until the returnedCompletableFuturecompletes (either normally or exceptionally).The returned
CompletableFuturecan complete exceptionally with:-
IllegalArgumentException- ifmessageis a malformed UTF-16 sequence -
IllegalStateException- if theWebSocketis closed; or if a Close message has been sent; or if there is an outstanding send operation; or if a previous Binary message was sent withisLast == false -
IOException- if an I/O error occurs during this operation; or if theWebSockethas been closed due to an error;
- Implementation Note:
- This implementation does not accept partial UTF-16 sequences.
In case such a sequence is passed, a returned
CompletableFuturecompletes exceptionally withIOException. - Parameters:
message- the messageisLast-trueif this is the last part of the message,falseotherwise- Returns:
- a
CompletableFuturewith thisWebSocket
-
-
sendBinary
CompletableFuture<WebSocket> sendBinary(ByteBuffer message, boolean isLast)
Sends a Binary message with bytes from the givenByteBuffer.Returns a
CompletableFuture<WebSocket>which completes normally when the message has been sent or completes exceptionally if an error occurs.The returned
CompletableFuturecan complete exceptionally with:-
IllegalStateException- if theWebSocketis closed; or if a Close message has been sent; or if there is an outstanding send operation; or if a previous Text message was sent withisLast == false -
IOException- if an I/O error occurs during this operation; or if theWebSockethas been closed due to an error
- Parameters:
message- the messageisLast-trueif this is the last part of the message,falseotherwise- Returns:
- a
CompletableFuturewith thisWebSocket
-
-
sendPing
CompletableFuture<WebSocket> sendPing(ByteBuffer message)
Sends a Ping message with bytes from the given ByteBuffer.Returns a
CompletableFuture<WebSocket>which completes normally when the message has been sent or completes exceptionally if an error occurs.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 message must consist of not more than
125bytes:message.remaining() <= 125.The returned
CompletableFuturecan complete exceptionally with:-
IllegalArgumentException- ifmessage.remaining() > 125 -
IllegalStateException- if theWebSocketis closed; or if a Close message has been sent; or if there is an outstanding send operation -
IOException- if an I/O error occurs during this operation; or if theWebSockethas been closed due to an error
- Parameters:
message- the message- Returns:
- a
CompletableFuturewith thisWebSocket
-
-
sendPong
CompletableFuture<WebSocket> sendPong(ByteBuffer message)
Sends a Pong message with bytes from the given ByteBuffer.Returns a
CompletableFuture<WebSocket>which completes normally when the message has been sent or completes exceptionally if an error occurs.A Pong message may be unsolicited or may be sent in response to a previously received Ping. In latter case the contents of the Pong must be identical to the originating Ping.
The message must consist of not more than
125bytes:message.remaining() <= 125.The returned
CompletableFuturecan complete exceptionally with:-
IllegalArgumentException- ifmessage.remaining() > 125 -
IllegalStateException- if theWebSocketis closed; or if a Close message has been sent; or if there is an outstanding send operation -
IOException- if an I/O error occurs during this operation; or if theWebSockethas been closed due to an error
- Parameters:
message- the message- Returns:
- a
CompletableFuturewith thisWebSocket
-
-
sendClose
CompletableFuture<WebSocket> sendClose(int statusCode, String reason)
Sends a Close message with the given status code and the reason.When this method has been invoked, no further messages can be sent.
The
statusCodeis an integer in the range1000 <= code <= 4999. However, not all status codes may be legal in some implementations. Regardless of an implementation,1000is always legal and1002,1003,1005,1006,1007,1009,1010,1012,1013and1015are always illegal codes.The
reasonis a short string that must have an UTF-8 representation not longer than123bytes. For more details on Close message, status codes and reason see RFC 6455 sections 5.5.1. Close and 7.4. Status Codes.The method returns a
CompletableFuture<WebSocket>which completes normally when the message has been sent or completes exceptionally if an error occurs.The returned
CompletableFuturecan complete exceptionally with:-
IllegalArgumentException- if thestatusCodehas an illegal value; or ifreasondoesn't have an UTF-8 representation of length<= 123 -
IOException- if an I/O error occurs during this operation; or theWebSockethas been closed due to an error
If this method has already been invoked or the
WebSocketis closed, then subsequent invocations of this method have no effect and the returnedCompletableFuturecompletes normally.If a Close message has been received before, then this invocation completes the closing handshake and by the time the returned
CompletableFuturecompletes, theWebSocketwill have been closed.- Parameters:
statusCode- the status codereason- the reason- Returns:
- a
CompletableFuturewith thisWebSocket
-
-
request
void request(long n)
Allowsnmore messages to be received by theListener.The actual number of received messages might be fewer if a Close message is received, the
WebSocketcloses or an error occurs.A
WebSocketthat has just been built, hasn't requested anything yet. Usually the initial request for messages is made inListener.onOpen.If the
WebSocketis closed then invoking this method has no effect.- Implementation Note:
- This implementation does not distinguish between partial and
whole messages, because it's not known beforehand how a message will be
received.
If a server sends more messages than requested, this implementation queues up these messages on the TCP connection and may eventually force the sender to stop sending through TCP flow control.
- Parameters:
n- the number of messages- Throws:
IllegalArgumentException- ifn < 0
-
getSubprotocol
String getSubprotocol()
Returns a subprotocol which has been chosen for thisWebSocket.- Returns:
- a subprotocol, or an empty
Stringif there is none
-
isClosed
boolean isClosed()
Tells whether theWebSocketis closed.When a
WebSocketis closed no further messages can be sent or received.- Returns:
trueif theWebSocketis closed,falseotherwise
-
abort
void abort() throws IOExceptionCloses theWebSocketabruptly.This method may be invoked at any time. This method closes the underlying TCP connection and puts the
WebSocketinto a closed state.As the result
Listener.onClosewill be invoked unless eitheronCloseoronErrorhas been invoked before. In which case no additional invocation will happen.If the
WebSocketis already closed then invoking this method has no effect.- Throws:
IOException- if an I/O error occurs
-
-