Spec-Zone.ru › OpenJDK 25

Interface WebSocket.Builder

Enclosing interface:
WebSocket
public static interface WebSocket.Builder
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 Type Method Description
CompletableFuture<WebSocket> buildAsync(URI uri, WebSocket.Listener listener)
Builds a WebSocket connected to the given URI and associated with the given Listener.
WebSocket.Builder connectTimeout(Duration timeout)
Sets a timeout for establishing a WebSocket connection.
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.
WebSocket.Builder subprotocols(String mostPreferred, String... lesserPreferred)
Sets a request for the given subprotocols.

Method Details

header

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 - the header name
value - the header value
Returns:
this builder

connectTimeout

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 - the timeout, non-negative, non-ZERO
Returns:
this builder

subprotocols

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 - the most preferred subprotocol
lesserPreferred - the lesser preferred subprotocols
Returns:
this builder

buildAsync

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:

  • IOException - if an I/O error occurs
  • WebSocketHandshakeException - if the opening handshake fails
  • HttpTimeoutException - if the opening handshake does not complete within the timeout
  • InterruptedException - if the operation is interrupted
  • IllegalArgumentException - if any of the arguments of this builder's methods are illegal
Parameters:
uri - the WebSocket URI
listener - the listener
Returns:
a CompletableFuture with the WebSocket

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2025, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/25/docs/api/java.net.http/java/net/http/WebSocket.Builder.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API