-
- Enclosing interface:
- WebSocket
public static interface WebSocket.BuilderA builder for creatingWebSocketinstances.
Incubating Feature. Will be removed in a future release.To build a
WebSocket, create a builder, configure it as required by calling intermediate methods (the ones that return the builder itself), then finally callbuildAsync()to get aCompletableFuturewith resultingWebSocket.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
Builderare not safe for use by multiple threads without external synchronization.- Since:
- 9
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description CompletableFuture<WebSocket>buildAsync()Builds aWebSocket.WebSocket.BuilderconnectTimeout(Duration timeout)Sets a timeout for the opening handshake.WebSocket.Builderheader(String name, String value)Adds the given name-value pair to the list of additional headers for the opening handshake.WebSocket.Buildersubprotocols(String mostPreferred, String... lesserPreferred)Includes a request for the given subprotocols during the opening handshake.
-
-
-
Method Detail
-
header
WebSocket.Builder header(String name, String value)
Adds the given name-value pair to the list of additional headers for the opening handshake.Headers defined in WebSocket Protocol are not allowed to be added.
- Parameters:
name- the header namevalue- the header value- Returns:
- this builder
-
subprotocols
WebSocket.Builder subprotocols(String mostPreferred, String... lesserPreferred)
Includes a request for the given subprotocols during the opening handshake.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.
- Parameters:
mostPreferred- the most preferred subprotocollesserPreferred- the lesser preferred subprotocols, with the least preferred at the end- Returns:
- this builder
-
connectTimeout
WebSocket.Builder connectTimeout(Duration timeout)
Sets a timeout for the opening handshake.If the opening handshake does not complete within the specified duration then the
CompletableFuturereturned frombuildAsync()completes exceptionally with aHttpTimeoutException.If this method is not invoked then the timeout is deemed infinite.
-
buildAsync
CompletableFuture<WebSocket> buildAsync()
Builds aWebSocket.Returns a
CompletableFuture<WebSocket>which completes normally with theWebSocketwhen it is connected or completes exceptionally if an error occurs.CompletableFuturemay 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 aURLPermissionfor the WebSocket URI -
IllegalArgumentException- if any of the additionalheadersare illegal; or if any of the WebSocket Protocol rules relevant tosubprotocolsare violated; or if theconnect timeoutis invalid;
- Returns:
- a
CompletableFuturewith theWebSocket
-
-
-