Top Description Inners Fields Methods
java.net.http

public Interface WebSocket

Known Direct Implementers
jdk.internal.net.http.websocket.WebSocketImpl
Imports
java.io.IOException, java.net.URI, java.nio.ByteBuffer, java.time.Duration, java.util.concurrent.CompletableFuture, .CompletionStage

A WebSocket Client.

WebSocket instances are created through WebSocket.Builder.

WebSocket has an input and an output side. These sides are independent from each other. A side can either be open or closed. Once closed, the side remains closed. WebSocket messages are sent through a WebSocket and received through a WebSocket.Listener associated with it. Messages can be sent until the WebSocket's output is closed, and received until the WebSocket's input is closed.

A send method is any of the sendText, sendBinary, sendPing, sendPong and sendClose methods of WebSocket. A send method initiates a send operation and returns a CompletableFuture which completes once the operation has completed. If the CompletableFuture completes normally the operation is considered succeeded. If the CompletableFuture completes exceptionally, the operation is considered failed. An operation that has been initiated but not yet completed is considered pending.

A receive method is any of the onText, onBinary, onPing, onPong and onClose methods of Listener. WebSocket initiates a receive operation by invoking a receive method on the listener. The listener then must return a CompletionStage which completes once the operation has completed.

To control receiving of messages, a WebSocket maintains an internal counter. This counter's value is a number of times the WebSocket has yet to invoke a receive method. While this counter is zero the WebSocket does not invoke receive methods. The counter is incremented by n when request(n) is called. The counter is decremented by one when the WebSocket invokes a receive method. onOpen and onError are not receive methods. WebSocket invokes onOpen prior to any other methods on the listener. WebSocket invokes onOpen at most once. WebSocket may invoke onError at any given time. If the WebSocket invokes onError or onClose, then no further listener's methods will be invoked, no matter the value of the counter. For a newly built WebSocket the counter is zero.

Unless otherwise stated, null arguments will cause methods of WebSocket to throw NullPointerException, similarly, WebSocket will not pass null arguments to methods of Listener. The state of a WebSocket is not changed by the invocations that throw or return a CompletableFuture that completes with one of the NullPointerException, IllegalArgumentException, IllegalStateException exceptions.

WebSocket handles received Ping and Close messages automatically (as per the WebSocket Protocol) by replying with Pong and Close messages. If the listener receives Ping or Close messages, no mandatory actions from the listener are required.

API Note

The relationship between a WebSocket and the associated Listener is analogous to that of a Subscription and the associated Subscriber of type java.util.concurrent.Flow.

Since
11

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
public static interface
WebSocket.Listener

The receiving interface of WebSocket.

Field Summary

Modifier and TypeField and Description
public static final int
NORMAL_CLOSURE

The WebSocket Close message status code (), indicating normal closure, meaning that the purpose for which the connection was established has been fulfilled.

Method Summary

Modifier and TypeMethod and Description
public void
abort()

Closes this WebSocket's input and output abruptly.

public String

Returns:

the subprotocol, or an empty string if there's no subprotocol
getSubprotocol
()

Returns the subprotocol used by this WebSocket.

public boolean

Returns:

true if closed, false otherwise
isInputClosed
()

Tells whether this WebSocket's input is closed.

public boolean

Returns:

true if closed, false otherwise
isOutputClosed
()

Tells whether this WebSocket's output is closed.

public void
request(long
the number of invocations
n
)

Increments the counter of invocations of receive methods.

public CompletableFuture<WebSocket>

Returns:

a CompletableFuture that completes, with this WebSocket, when the data has been sent
sendBinary
(ByteBuffer
the data
data
,
boolean
true if this invocation completes the message, false otherwise
last
)

Sends binary data with bytes from the given buffer.

public CompletableFuture<WebSocket>

Returns:

a CompletableFuture that completes, with this WebSocket, when the Close message has been sent
sendClose
(int
the status code
statusCode
,
String
the reason
reason
)

Initiates an orderly closure of this WebSocket's output by sending a Close message with the given status code and the reason.

public CompletableFuture<WebSocket>

Returns:

a CompletableFuture that completes, with this WebSocket, when the Ping message has been sent
sendPing
(ByteBuffer
the message
message
)

Sends a Ping message with bytes from the given buffer.

public CompletableFuture<WebSocket>

Returns:

a CompletableFuture that completes, with this WebSocket, when the Pong message has been sent
sendPong
(ByteBuffer
the message
message
)

Sends a Pong message with bytes from the given buffer.

public CompletableFuture<WebSocket>

Returns:

a CompletableFuture that completes, with this WebSocket, when the data has been sent
sendText
(CharSequence
the data
data
,
boolean
true if this invocation completes the message, false otherwise
last
)

Sends textual data with characters from the given character sequence.

Field Detail

NORMAL_CLOSUREback to summary
public static final int NORMAL_CLOSURE

The WebSocket Close message status code (), indicating normal closure, meaning that the purpose for which the connection was established has been fulfilled.

See Also
sendClose(int, String), Listener#onClose(WebSocket, int, String)

Method Detail

abortback to summary
public void abort()

Closes this WebSocket's input and output abruptly.

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.

getSubprotocolback to summary
public String getSubprotocol()

Returns the subprotocol used by this WebSocket.

Returns:String

the subprotocol, or an empty string if there's no subprotocol

isInputClosedback to summary
public boolean isInputClosed()

Tells whether this WebSocket's input is closed.

If this method returns true, subsequent invocations will also return true.

Returns:boolean

true if closed, false otherwise

isOutputClosedback to summary
public boolean isOutputClosed()

Tells whether this WebSocket's output is closed.

If this method returns true, subsequent invocations will also return true.

Returns:boolean

true if closed, false otherwise

requestback to summary
public void request(long n)

Increments the counter of invocations of receive methods.

This WebSocket will invoke onText, onBinary, onPing, onPong or onClose methods on the associated listener (i.e. receive methods) up to n more times.

API Note

The parameter of this method is the number of invocations being requested from this WebSocket to the associated listener, not the number of messages. Sometimes a message may be delivered to the listener in a single invocation, but not always. For example, Ping, Pong and Close messages are delivered in a single invocation of 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; } };
    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;
    }
};
Parameters
n:long

the number of invocations

Exceptions
IllegalArgumentException:
if n <= 0
sendBinaryback to summary
public 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 message
  • IOException - if an I/O error occurs, or if the output is closed
Parameters
data:ByteBuffer

the data

last:boolean

true if this invocation completes the message, false otherwise

Returns:CompletableFuture<WebSocket>

a CompletableFuture that completes, with this WebSocket, when the data has been sent

sendCloseback to summary
public 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:

Unless 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.

API Note

Use the provided integer constant 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();
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);
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);
Parameters
statusCode:int

the status code

reason:String

the reason

Returns:CompletableFuture<WebSocket>

a CompletableFuture that completes, with this WebSocket, when the Close message has been sent

sendPingback to summary
public 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:

Parameters
message:ByteBuffer

the message

Returns:CompletableFuture<WebSocket>

a CompletableFuture that completes, with this WebSocket, when the Ping message has been sent

sendPongback to summary
public 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:

Parameters
message:ByteBuffer

the message

Returns:CompletableFuture<WebSocket>

a CompletableFuture that completes, with this WebSocket, when the Pong message has been sent

sendTextback to summary
public 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 message
  • IOException - if an I/O error occurs, or if the output is closed

Implementation Note

If data is a malformed UTF-16 sequence, the operation will fail with IOException.

Parameters
data:CharSequence

the data

last:boolean

true if this invocation completes the message, false otherwise

Returns:CompletableFuture<WebSocket>

a CompletableFuture that completes, with this WebSocket, when the data has been sent

java.net.http back to summary

public Interface WebSocket.Builder

Known Direct Implementers
jdk.internal.net.http.websocket.BuilderImpl

A builder of WebSocket Clients.

Builders are created by invoking HttpClient.newWebSocketBuilder. The intermediate (setter-like) methods change the state of the builder and return the same builder they have been invoked on. If an intermediate method is not invoked, an appropriate default value (or behavior) will be assumed. A Builder is not safe for use by multiple threads without external synchronization.

Since
11

Method Summary

Modifier and TypeMethod and Description
public CompletableFuture<WebSocket>

Returns:

a CompletableFuture with the WebSocket
buildAsync
(URI
the WebSocket URI
uri
,
WebSocket.Listener
the listener
listener
)

Builds a WebSocket connected to the given URI and associated with the given Listener.

public WebSocket.Builder

Returns:

this builder
connectTimeout
(Duration
the timeout, non-negative, non-ZERO
timeout
)

Sets a timeout for establishing a WebSocket connection.

public WebSocket.Builder

Returns:

this builder
header
(String
the header name
name
,
String
the header value
value
)

Adds the given name-value pair to the list of additional HTTP headers sent during the opening handshake.

public WebSocket.Builder

Returns:

this builder
subprotocols
(String
the most preferred subprotocol
mostPreferred
,
String...
the lesser preferred subprotocols
lesserPreferred
)

Sets a request for the given subprotocols.

Method Detail

buildAsyncback to summary
public CompletableFuture<WebSocket> buildAsync(URI uri, WebSocket.Listener listener)

Builds a WebSocket connected to the given URI and associated with the given Listener.

Returns a CompletableFuture which will either complete normally with the resulting WebSocket or complete exceptionally with one of the following errors:

Parameters
uri:URI

the WebSocket URI

listener:WebSocket.Listener

the listener

Returns:CompletableFuture<WebSocket>

a CompletableFuture with the WebSocket

connectTimeoutback to summary
public WebSocket.Builder connectTimeout(Duration timeout)

Sets a timeout for establishing a WebSocket connection.

If the connection is not established within the specified duration then building of the WebSocket will fail with HttpTimeoutException. If this method is not invoked then the infinite timeout is assumed.

Parameters
timeout:Duration

the timeout, non-negative, non-ZERO

Returns:WebSocket.Builder

this builder

headerback to summary
public WebSocket.Builder header(String name, String value)

Adds the given name-value pair to the list of additional HTTP headers sent during the opening handshake.

Headers defined in the WebSocket Protocol are illegal. If this method is not invoked, no additional HTTP headers will be sent.

Parameters
name:String

the header name

value:String

the header value

Returns:WebSocket.Builder

this builder

subprotocolsback to summary
public WebSocket.Builder subprotocols(String mostPreferred, String... lesserPreferred)

Sets a request for the given subprotocols.

After the WebSocket has been built, the actual subprotocol can be queried through WebSocket.getSubprotocol().

Subprotocols are specified in the order of preference. The most preferred subprotocol is specified first. If there are any additional subprotocols they are enumerated from the most preferred to the least preferred.

Subprotocols not conforming to the syntax of subprotocol identifiers are illegal. If this method is not invoked then no subprotocols will be requested.

Parameters
mostPreferred:String

the most preferred subprotocol

lesserPreferred:String[]

the lesser preferred subprotocols

Returns:WebSocket.Builder

this builder

java.net.http back to summary

public Interface WebSocket.Listener


The receiving interface of WebSocket.

A WebSocket invokes methods of the associated listener passing itself as an argument. These methods are invoked in a thread-safe manner, such that the next invocation may start only after the previous one has finished.

When data has been received, the WebSocket invokes a receive method. Methods onText, onBinary, onPing and onPong must return a CompletionStage that completes once the message has been received by the listener. If a listener's method returns null rather than a CompletionStage, WebSocket will behave as if the listener returned a CompletionStage that is already completed normally.

An IOException raised in WebSocket will result in an invocation of onError with that exception (if the input is not closed). Unless otherwise stated if the listener's method throws an exception or a CompletionStage returned from a method completes exceptionally, the WebSocket will invoke onError with this exception.

API Note

The strict sequential order of invocations from WebSocket to Listener means, in particular, that the Listener's methods are treated as non-reentrant. This means that Listener implementations do not need to be concerned with possible recursion or the order in which they invoke WebSocket.request in relation to their processing logic.

Careful attention may be required if a listener is associated with more than a single WebSocket. In this case invocations related to different instances of WebSocket may not be ordered and may even happen concurrently.

CompletionStages returned from the receive methods have nothing to do with the counter of invocations. Namely, a CompletionStage does not have to be completed in order to receive more invocations of the listener's methods. Here is an example of a listener that requests invocations, one at a time, until a complete message has been accumulated, then processes the result, and completes the CompletionStage:

WebSocket.Listener listener = new WebSocket.Listener() { List<CharSequence> parts = new ArrayList<>(); CompletableFuture<?> accumulatedMessage = new CompletableFuture<>(); public CompletionStage<?> onText(WebSocket webSocket, CharSequence message, boolean last) { parts.add(message); webSocket.request(1); if (last) { processWholeText(parts); parts = new ArrayList<>(); accumulatedMessage.complete(null); CompletionStage<?> cf = accumulatedMessage; accumulatedMessage = new CompletableFuture<>(); return cf; } return accumulatedMessage; } };
    WebSocket.Listener listener = new WebSocket.Listener() {

    List<CharSequence> parts = new ArrayList<>();
    CompletableFuture<?> accumulatedMessage = new CompletableFuture<>();

    public CompletionStage<?> onText(WebSocket webSocket,
                                     CharSequence message,
                                     boolean last) {
        parts.add(message);
        webSocket.request(1);
        if (last) {
            processWholeText(parts);
            parts = new ArrayList<>();
            accumulatedMessage.complete(null);
            CompletionStage<?> cf = accumulatedMessage;
            accumulatedMessage = new CompletableFuture<>();
            return cf;
        }
        return accumulatedMessage;
    }
};
Since
11

Method Summary

Modifier and TypeMethod and Description
public default CompletionStage<?>

Returns:

a CompletionStage which completes when the ByteBuffer may be reclaimed; or null if it may be reclaimed immediately
onBinary
(WebSocket
the WebSocket on which the data has been received
webSocket
,
ByteBuffer
the data
data
,
boolean
whether this invocation completes the message
last
)

A binary data has been received.

public default CompletionStage<?>

Returns:

a CompletionStage which completes when the WebSocket may be closed; or null if it may be closed immediately
onClose
(WebSocket
the WebSocket on which the message has been received
webSocket
,
int
the status code
statusCode
,
String
the reason
reason
)

Receives a Close message indicating the WebSocket's input has been closed.

public default void
onError(WebSocket
the WebSocket on which the error has occurred
webSocket
,
Throwable
the error
error
)

An error has occurred.

public default void
onOpen(WebSocket
the WebSocket that has been connected
webSocket
)

A WebSocket has been connected.

public default CompletionStage<?>

Returns:

a CompletionStage which completes when the ByteBuffer may be reclaimed; or null if it may be reclaimed immediately
onPing
(WebSocket
the WebSocket on which the message has been received
webSocket
,
ByteBuffer
the message
message
)

A Ping message has been received.

public default CompletionStage<?>

Returns:

a CompletionStage which completes when the ByteBuffer may be reclaimed; or null if it may be reclaimed immediately
onPong
(WebSocket
the WebSocket on which the message has been received
webSocket
,
ByteBuffer
the message
message
)

A Pong message has been received.

public default CompletionStage<?>

Returns:

a CompletionStage which completes when the CharSequence may be reclaimed; or null if it may be reclaimed immediately
onText
(WebSocket
the WebSocket on which the data has been received
webSocket
,
CharSequence
the data
data
,
boolean
whether this invocation completes the message
last
)

A textual data has been received.

Method Detail

onBinaryback to summary
public default CompletionStage<?> onBinary(WebSocket webSocket, ByteBuffer data, boolean last)

A binary data has been received.

This data is located in bytes from the buffer's position to its limit.

Return a CompletionStage which will be used by the WebSocket as an indication it may reclaim the ByteBuffer. Do not access the ByteBuffer after this CompletionStage has completed.

Implementation Specification

The default implementation is equivalent to:

webSocket.request(1); return null;
webSocket.request(1);
return null;
Parameters
webSocket:WebSocket

the WebSocket on which the data has been received

data:ByteBuffer

the data

last:boolean

whether this invocation completes the message

Returns:CompletionStage<?>

a CompletionStage which completes when the ByteBuffer may be reclaimed; or null if it may be reclaimed immediately

onCloseback to summary
public default CompletionStage<?> onClose(WebSocket webSocket, int statusCode, String reason)

Receives a Close message indicating the WebSocket's input has been closed.

This is the last invocation from the specified WebSocket. By the time this invocation begins the WebSocket's input will have been closed.

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. The reason is a string which has a UTF-8 representation not longer than 123 bytes.

If the WebSocket's output is not already closed, the CompletionStage returned by this method will be used as an indication that the WebSocket's output may be closed. The WebSocket will close its output at the earliest of completion of the returned CompletionStage or invoking either of the sendClose or abort methods.

API Note

Returning a CompletionStage that never completes, effectively disables the reciprocating closure of the output.

To specify a custom closure code or reason code the sendClose method may be invoked from inside the onClose invocation:

public CompletionStage<?> onClose(WebSocket webSocket, int statusCode, String reason) { webSocket.sendClose(CUSTOM_STATUS_CODE, CUSTOM_REASON); return new CompletableFuture<Void>(); }
   public CompletionStage<?> onClose(WebSocket webSocket,
                        int statusCode,
                        String reason) {
    webSocket.sendClose(CUSTOM_STATUS_CODE, CUSTOM_REASON);
    return new CompletableFuture<Void>();
}

Implementation Specification

The default implementation of this method returns null, indicating that the output should be closed immediately.

Parameters
webSocket:WebSocket

the WebSocket on which the message has been received

statusCode:int

the status code

reason:String

the reason

Returns:CompletionStage<?>

a CompletionStage which completes when the WebSocket may be closed; or null if it may be closed immediately

onErrorback to summary
public default void onError(WebSocket webSocket, Throwable error)

An error has occurred.

This is the last invocation from the specified WebSocket. By the time this invocation begins both the WebSocket's input and output will have been closed. A WebSocket may invoke this method on the associated listener at any time after it has invoked onOpen, regardless of whether or not any invocations have been requested from the WebSocket.

If an exception is thrown from this method, resulting behavior is undefined.

Parameters
webSocket:WebSocket

the WebSocket on which the error has occurred

error:Throwable

the error

onOpenback to summary
public default void onOpen(WebSocket webSocket)

A WebSocket has been connected.

This is the initial invocation and it is made once. It is typically used to make a request for more invocations.

Implementation Specification

The default implementation is equivalent to:

webSocket.request(1);
webSocket.request(1);
Parameters
webSocket:WebSocket

the WebSocket that has been connected

onPingback to summary
public default CompletionStage<?> onPing(WebSocket webSocket, ByteBuffer message)

A Ping message has been received.

As guaranteed by the WebSocket Protocol, the message consists of not more than 125 bytes. These bytes are located from the buffer's position to its limit.

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 when a ping is received.

Return a CompletionStage which will be used by the WebSocket as a signal it may reclaim the ByteBuffer. Do not access the ByteBuffer after this CompletionStage has completed.

Implementation Specification

The default implementation is equivalent to:

webSocket.request(1); return null;
webSocket.request(1);
return null;
Parameters
webSocket:WebSocket

the WebSocket on which the message has been received

message:ByteBuffer

the message

Returns:CompletionStage<?>

a CompletionStage which completes when the ByteBuffer may be reclaimed; or null if it may be reclaimed immediately

onPongback to summary
public default CompletionStage<?> onPong(WebSocket webSocket, ByteBuffer message)

A Pong message has been received.

As guaranteed by the WebSocket Protocol, the message consists of not more than 125 bytes. These bytes are located from the buffer's position to its limit.

Return a CompletionStage which will be used by the WebSocket as a signal it may reclaim the ByteBuffer. Do not access the ByteBuffer after this CompletionStage has completed.

Implementation Specification

The default implementation is equivalent to:

webSocket.request(1); return null;
webSocket.request(1);
return null;
Parameters
webSocket:WebSocket

the WebSocket on which the message has been received

message:ByteBuffer

the message

Returns:CompletionStage<?>

a CompletionStage which completes when the ByteBuffer may be reclaimed; or null if it may be reclaimed immediately

onTextback to summary
public default CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last)

A textual data has been received.

Return a CompletionStage which will be used by the WebSocket as an indication it may reclaim the CharSequence. Do not access the CharSequence after this CompletionStage has completed.

Implementation Specification

The default implementation is equivalent to:

webSocket.request(1); return null;
webSocket.request(1);
return null;

Implementation Note

The data is always a legal UTF-16 sequence.

Parameters
webSocket:WebSocket

the WebSocket on which the data has been received

data:CharSequence

the data

last:boolean

whether this invocation completes the message

Returns:CompletionStage<?>

a CompletionStage which completes when the CharSequence may be reclaimed; or null if it may be reclaimed immediately