static final 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.
CompletableFuture<WebSocket> sendText(CharSequence data, boolean last)
Sends textual data with characters from the given character sequence.
The character sequence must not be modified until the CompletableFuture
returned from this method has completed.
A CompletableFuture
returned from this method can complete exceptionally with:
IllegalStateException
- if there is a pending text or binary send operation or if the previous binary data does not complete the messageIOException
- if an I/O error occurs, or if the output is closeddata
is a malformed UTF-16 sequence, the operation will fail with IOException
.
data
- the data
last
- true
if this invocation completes the message, false
otherwise
CompletableFuture
that completes, with this WebSocket, when the data has been sent
CompletableFuture<WebSocket> sendBinary(ByteBuffer data, boolean last)
Sends binary data with bytes from the given buffer.
The data is located in bytes from the buffer's position to its limit. Upon normal completion of a CompletableFuture
returned from this method the buffer will have no remaining bytes. The buffer must not be accessed until after that.
The CompletableFuture
returned from this method can complete exceptionally with:
IllegalStateException
- if there is a pending text or binary send operation or if the previous textual data does not complete the messageIOException
- if an I/O error occurs, or if the output is closeddata
- the data
last
- true
if this invocation completes the message, false
otherwise
CompletableFuture
that completes, with this WebSocket, when the data has been sent
CompletableFuture<WebSocket> sendPing(ByteBuffer message)
Sends a Ping message with bytes from the given buffer.
The message consists of not more than 125
bytes from the buffer's position to its limit. Upon normal completion of a CompletableFuture
returned from this method the buffer will have no remaining bytes. The buffer must not be accessed until after that.
The CompletableFuture
returned from this method can complete exceptionally with:
IllegalStateException
- if there is a pending ping or pong send operationIllegalArgumentException
- if the message is too longIOException
- if an I/O error occurs, or if the output is closedmessage
- the message
CompletableFuture
that completes, with this WebSocket, when the Ping message has been sent
CompletableFuture<WebSocket> sendPong(ByteBuffer message)
Sends a Pong message with bytes from the given buffer.
The message consists of not more than 125
bytes from the buffer's position to its limit. Upon normal completion of a CompletableFuture
returned from this method the buffer will have no remaining bytes. The buffer must not be accessed until after that.
Given that the WebSocket implementation will automatically send a reciprocal pong when a ping is received, it is rarely required to send a pong message explicitly.
The CompletableFuture
returned from this method can complete exceptionally with:
IllegalStateException
- if there is a pending ping or pong send operationIllegalArgumentException
- if the message is too longIOException
- if an I/O error occurs, or if the output is closedmessage
- the message
CompletableFuture
that completes, with this WebSocket, when the Pong message has been sent
CompletableFuture<WebSocket> sendClose(int statusCode, String reason)
Initiates an orderly closure of this WebSocket's output by sending a Close message with the given status code and the reason.
The statusCode
is an integer from the range 1000 <= code <= 4999
. Status codes 1002
, 1003
, 1006
, 1007
, 1009
, 1010
, 1012
, 1013
and 1015
are illegal. Behaviour in respect to other status codes is implementation-specific. A legal reason
is a string that has a UTF-8 representation not longer than 123
bytes.
A CompletableFuture
returned from this method can complete exceptionally with:
IllegalArgumentException
- if statusCode
is illegal, or if reason
is illegalIOException
- if an I/O error occurs, or if the output is closedUnless the CompletableFuture
returned from this method completes with IllegalArgumentException
, or the method throws NullPointerException
, the output will be closed.
If not already closed, the input remains open until a Close message received, or abort
is invoked, or an error occurs.
NORMAL_CLOSURE
as a status code and an empty string as a reason in a typical case:
CompletableFuture<WebSocket> webSocket = ...
webSocket.thenCompose(ws -> ws.sendText("Hello, ", false))
.thenCompose(ws -> ws.sendText("world!", true))
.thenCompose(ws -> ws.sendClose(WebSocket.NORMAL_CLOSURE, ""))
.join();
The sendClose
method does not close this WebSocket's input. It merely closes this WebSocket's output by sending a Close message. To enforce closing the input, invoke the abort
method. Here is an example of an application that sends a Close message, and then starts a timer. Once no data has been received within the specified timeout, the timer goes off and the alarm aborts WebSocket
:
MyAlarm alarm = new MyAlarm(webSocket::abort);
WebSocket.Listener listener = new WebSocket.Listener() {
public CompletionStage<?> onText(WebSocket webSocket,
CharSequence data,
boolean last) {
alarm.snooze();
...
}
...
};
...
Runnable startTimer = () -> {
MyTimer idleTimer = new MyTimer();
idleTimer.add(alarm, 30, TimeUnit.SECONDS);
};
webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "ok").thenRun(startTimer);
statusCode
- the status code
reason
- the reason
CompletableFuture
that completes, with this WebSocket, when the Close message has been sent
void request(long n)
This WebSocket will invoke onText
, onBinary
, onPing
, onPong
or onClose
methods on the associated listener (i.e. receive methods) up to n
more times.
onPing
, onPong
and onClose
methods respectively. However, whether or not Text and Binary messages are delivered in a single invocation of onText
and onBinary
methods depends on the boolean argument (last
) of these methods. If last
is false
, then there is more to a message than has been delivered to the invocation.
Here is an example of a listener that requests invocations, one at a time, until a complete message has been accumulated, and then processes the result:
WebSocket.Listener listener = new WebSocket.Listener() {
StringBuilder text = new StringBuilder();
public CompletionStage<?> onText(WebSocket webSocket,
CharSequence message,
boolean last) {
text.append(message);
if (last) {
processCompleteTextMessage(text);
text = new StringBuilder();
}
webSocket.request(1);
return null;
}
...
}
n
- the number of invocations
IllegalArgumentException
- if n <= 0
String getSubprotocol()
Returns the subprotocol used by this WebSocket.
boolean isOutputClosed()
If this method returns true
, subsequent invocations will also return true
.
true
if closed, false
otherwise
boolean isInputClosed()
If this method returns true
, subsequent invocations will also return true
.
true
if closed, false
otherwise
void abort()
When this method returns both the input and the output will have been closed. Any pending send operations will fail with IOException
. Subsequent invocations of abort
will have no effect.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4