public final class WebSocket
- Object
- WebSocket
Client-side WebSocket. Connections are created via build and configured
with fluent handler setters before being started with connect:
WebSocket ws = WebSocket.build("wss://example.com/chat")
.onConnect(w -> w.send("hello"))
.onTextMessage((w, m) -> Log.p("recv " + m))
.onClose((w, c, r) -> Log.p("closed " + c + " / " + r))
.onError((w, e) -> Log.e(e))
.connect();
Each handler receives the WebSocket as its first argument so it can
send, query state, or close without capturing an external reference.
Handlers fire on a background thread. Use
Display.getInstance().callSerially(...) inside a handler if you need
to touch UI from it.
Use isSupported to check at runtime whether the current port supports
WebSocket – older ports return false and build will throw on them.
Nested types
interface WebSocket.ConnectHandler | Handler for the connection-established event. |
interface WebSocket.TextHandler | Handler for an incoming text frame. |
interface WebSocket.BinaryHandler | Handler for an incoming binary frame. |
interface WebSocket.CloseHandler | Handler for the close event. |
interface WebSocket.ErrorHandler | Handler for transport- or protocol-level errors. |
Methods
Inherited methods
Method details
isSupported
public static boolean isSupported()build
public static WebSocket build(String url)url. The URL must use the
ws:// or wss:// scheme. Call connect to start the handshake.Throws
RuntimeException- if the current port does not support WebSocket.
onConnect
public WebSocket onConnect(WebSocket.ConnectHandler handler)this for chaining.onTextMessage
public WebSocket onTextMessage(WebSocket.TextHandler handler)this for chaining.onBinaryMessage
public WebSocket onBinaryMessage(WebSocket.BinaryHandler handler)this for chaining.onClose
public WebSocket onClose(WebSocket.CloseHandler handler)this for chaining.onError
public WebSocket onError(WebSocket.ErrorHandler handler)this for chaining.subprotocols
public WebSocket subprotocols(String... protocols)Offer one or more subprotocols (RFC 6455 Sec-WebSocket-Protocol),
in preference order, to negotiate during the handshake. Must be
called before connect. After the connection opens,
getSelectedSubprotocol returns the one the server chose (or null).
Returns this for chaining.
WebSocket.build("wss://api.example.com/graphql")
.subprotocols("graphql-transport-ws")
.onConnect(w -> Log.p("using " + w.getSelectedSubprotocol()))
.connect();
header
public WebSocket header(String name, String value)Add a header to the opening handshake. Must be called before connect.
Passing a null value removes a previously set header. Returns this for
chaining.
Typically used to carry an authorization or attestation token, since a WebSocket has no other place to put one.
WebSocket.build("wss://api.example.com/stream")
.header("X-CN1-Attest", token)
.connect();
Not supported everywhere
Emitted on Android, desktop, Windows and Linux, which build the opening handshake themselves. Silently dropped on iOS and in the browser, which hand the handshake to a platform WebSocket that exposes no way to add headers to it.
Where headers are unavailable, obtain a short-lived ticket over an ordinary HTTPS request – which can be attested and pinned normally – and pass it in the URL query instead. That also avoids leaking a long-lived credential into a URL.
Headers the handshake sets itself – Host, Upgrade, Connection,
Sec-WebSocket-Key, Sec-WebSocket-Version, Sec-WebSocket-Protocol
– are reserved and are ignored if passed here. Use subprotocols for
the last of those.
getSelectedSubprotocol
public String getSelectedSubprotocol()ConnectHandler has fired.connect
public WebSocket connect()this for chaining; success is signalled asynchronously
via the registered ConnectHandler.connect
public WebSocket connect(int connectTimeoutMs)0 means “use platform default”.close
public void close()send
public void send(String text)IllegalStateException if the connection
is not WebSocketState.OPEN.send
public void send(byte[] binary)IllegalStateException if the connection
is not WebSocketState.OPEN.getReadyState
public WebSocketState getReadyState()getUrl
public String getUrl()