-
public interface WebSocket
A WebSocket client conforming to RFC 6455.
Incubating Feature. Will be removed in a future release.A
WebSocket
provides full-duplex communication over a TCP connection.To create a
WebSocket
use a builder. Once aWebSocket
is built, it's ready to send and receive messages. When theWebSocket
is no longer needed it must be closed: a Close message must both be sent and received. TheWebSocket
may be also closed abruptly.Once closed the
WebSocket
remains closed and cannot be reopened.Messages of type
X
(whereX
is 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 aCompletionStage
which 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
CompletableFuture
completes (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
'sposition
to thebuffer
'slimit
.Unless otherwise stated,
null
parameter 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 interface
WebSocket.Builder
A builder for creatingWebSocket
instances.static interface
WebSocket.Listener
A listener for events and messages on aWebSocket
.static class
WebSocket.MessagePart
A marker used byWebSocket.Listener
in cases where a partial message may be received.
-
Field Summary
Fields Modifier and Type Field Description static int
NORMAL_CLOSURE
The 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 void
abort()
Closes theWebSocket
abruptly.String
getSubprotocol()
Returns a subprotocol which has been chosen for thisWebSocket
.boolean
isClosed()
Tells whether theWebSocket
is closed.void
request(long n)
Allowsn
more 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
CharSequence
must not be modified until the returnedCompletableFuture
completes (either normally or exceptionally).The returned
CompletableFuture
can complete exceptionally with:-
IllegalArgumentException
- ifmessage
is a malformed UTF-16 sequence -
IllegalStateException
- if theWebSocket
is 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 theWebSocket
has 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
CompletableFuture
completes exceptionally withIOException
. - Parameters:
message
- the messageisLast
-true
if this is the last part of the message,false
otherwise- Returns:
- a
CompletableFuture
with 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
CompletableFuture
can complete exceptionally with:-
IllegalStateException
- if theWebSocket
is 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 theWebSocket
has been closed due to an error
- Parameters:
message
- the messageisLast
-true
if this is the last part of the message,false
otherwise- Returns:
- a
CompletableFuture
with 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
125
bytes:message.remaining() <= 125
.The returned
CompletableFuture
can complete exceptionally with:-
IllegalArgumentException
- ifmessage.remaining() > 125
-
IllegalStateException
- if theWebSocket
is 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 theWebSocket
has been closed due to an error
- Parameters:
message
- the message- Returns:
- a
CompletableFuture
with 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
125
bytes:message.remaining() <= 125
.The returned
CompletableFuture
can complete exceptionally with:-
IllegalArgumentException
- ifmessage.remaining() > 125
-
IllegalStateException
- if theWebSocket
is 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 theWebSocket
has been closed due to an error
- Parameters:
message
- the message- Returns:
- a
CompletableFuture
with 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
statusCode
is an integer in the range1000 <= code <= 4999
. However, not all status codes may be legal in some implementations. Regardless of an implementation,1000
is always legal and1002
,1003
,1005
,1006
,1007
,1009
,1010
,1012
,1013
and1015
are always illegal codes.The
reason
is a short string that must have 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.The method returns a
CompletableFuture<WebSocket>
which completes normally when the message has been sent or completes exceptionally if an error occurs.The returned
CompletableFuture
can complete exceptionally with:-
IllegalArgumentException
- if thestatusCode
has an illegal value; or ifreason
doesn't have an UTF-8 representation of length<= 123
-
IOException
- if an I/O error occurs during this operation; or theWebSocket
has been closed due to an error
If this method has already been invoked or the
WebSocket
is closed, then subsequent invocations of this method have no effect and the returnedCompletableFuture
completes normally.If a Close message has been received before, then this invocation completes the closing handshake and by the time the returned
CompletableFuture
completes, theWebSocket
will have been closed.- Parameters:
statusCode
- the status codereason
- the reason- Returns:
- a
CompletableFuture
with thisWebSocket
-
-
request
void request(long n)
Allowsn
more messages to be received by theListener
.The actual number of received messages might be fewer if a Close message is received, the
WebSocket
closes or an error occurs.A
WebSocket
that has just been built, hasn't requested anything yet. Usually the initial request for messages is made inListener.onOpen
.If the
WebSocket
is 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
String
if there is none
-
isClosed
boolean isClosed()
Tells whether theWebSocket
is closed.When a
WebSocket
is closed no further messages can be sent or received.- Returns:
true
if theWebSocket
is closed,false
otherwise
-
abort
void abort() throws IOException
Closes theWebSocket
abruptly.This method may be invoked at any time. This method closes the underlying TCP connection and puts the
WebSocket
into a closed state.As the result
Listener.onClose
will be invoked unless eitheronClose
oronError
has been invoked before. In which case no additional invocation will happen.If the
WebSocket
is already closed then invoking this method has no effect.- Throws:
IOException
- if an I/O error occurs
-
-