public static interface WebSocket.Builder
WebSocket
instances.
To build a WebSocket
, create a builder, configure it as required by
calling intermediate methods (the ones that return the builder itself),
then finally call buildAsync()
to get a CompletableFuture
with resulting WebSocket
.
If an intermediate method has not been called, an appropriate default value (or behavior) will be used. Unless otherwise noted, a repeated call to an intermediate method overwrites the previous value (or overrides the previous behaviour).
Instances of Builder
are not safe for use by multiple threads
without external synchronization.
Modifier and Type | Method | Description |
---|---|---|
CompletableFuture<WebSocket> |
buildAsync() |
Builds a
WebSocket . |
WebSocket.Builder |
connectTimeout(Duration timeout) |
Sets a timeout for the opening handshake.
|
WebSocket.Builder |
header(String name,
String value) |
Adds the given name-value pair to the list of additional headers for
the opening handshake.
|
WebSocket.Builder |
subprotocols(String mostPreferred,
String... lesserPreferred) |
Includes a request for the given subprotocols during the opening
handshake.
|
WebSocket.Builder header(String name, String value)
Headers defined in WebSocket Protocol are not allowed to be added.
name
- the header namevalue
- the header valueWebSocket.Builder subprotocols(String mostPreferred, String... lesserPreferred)
Among the requested subprotocols at most one will be chosen by
the server. This subprotocol will be available from WebSocket.getSubprotocol()
. Subprotocols are specified in the order of
preference.
Each of the given subprotocols must conform to the relevant rules defined in the WebSocket Protocol.
If this method is not invoked then no subprotocols are requested.
mostPreferred
- the most preferred subprotocollesserPreferred
- the lesser preferred subprotocols, with the least preferred
at the endWebSocket.Builder connectTimeout(Duration timeout)
If the opening handshake does not complete within the specified
duration then the CompletableFuture
returned from buildAsync()
completes exceptionally with a HttpTimeoutException
.
If this method is not invoked then the timeout is deemed infinite.
CompletableFuture<WebSocket> buildAsync()
WebSocket
.
Returns a CompletableFuture<WebSocket>
which completes
normally with the WebSocket
when it is connected or completes
exceptionally if an error occurs.
CompletableFuture
may complete exceptionally with the
following errors:
IOException
-
if an I/O error occurs
WebSocketHandshakeException
-
if the opening handshake fails
HttpTimeoutException
-
if the opening handshake does not complete within
the specified duration
InterruptedException
-
if the operation was interrupted
SecurityException
-
if a security manager is set, and the caller does not
have a URLPermission
for the WebSocket URI
IllegalArgumentException
-
if any of the additional headers
are illegal;
or if any of the WebSocket Protocol rules relevant to subprotocols
are violated;
or if the connect timeout
is invalid;
CompletableFuture
with the WebSocket
Submit a bug or feature
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 2015, 2017, Oracle and/or its affiliates. 500 Oracle Parkway
Redwood Shores, CA 94065 USA. All rights reserved.
DRAFT 9-Ubuntu+0-9b158-1