java.lang.Object
dev.onvoid.webrtc.examples.web.client.WebSocketClient
A WebSocket client implementation using JDK internal WebSockets (java.net.http.WebSocket).
This client supports secure WebSocket connections (WSS), message sending/receiving,
and automatic heartbeat to keep the connection alive.
- Author:
- Alex Andres
-
Constructor Summary
ConstructorsConstructorDescriptionWebSocketClient(URI serverUri, String subprotocol) Creates a new WebSocket client with a single subprotocol.WebSocketClient(URI serverUri, List<String> subprotocols) Creates a new WebSocket client. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCloseListener(Runnable listener) Adds a listener for connection close events.voidaddErrorListener(Consumer<Throwable> listener) Adds a listener for connection errors.voidaddMessageListener(Consumer<String> listener) Adds a listener for incoming text messages.connect()Connects to the WebSocket server.disconnect(int statusCode, String reason) Closes the WebSocket connection.booleanChecks if the client is connected to the WebSocket server.voidremoveCloseListener(Runnable listener) Removes a close listener.voidremoveErrorListener(Consumer<Throwable> listener) Removes an error listener.voidremoveMessageListener(Consumer<String> listener) Removes a message listener.sendMessage(String message) Sends a text message to the WebSocket server.
-
Constructor Details
-
WebSocketClient
Creates a new WebSocket client.- Parameters:
serverUri- The URI of the WebSocket server (e.g., "wss://localhost:8443/ws").subprotocols- The WebSocket subprotocols to use (e.g., "ws-signaling").
-
WebSocketClient
Creates a new WebSocket client with a single subprotocol.- Parameters:
serverUri- The URI of the WebSocket server.subprotocol- The WebSocket subprotocol to use.
-
-
Method Details
-
connect
Connects to the WebSocket server.- Returns:
- A CompletableFuture that completes when the connection is established.
-
sendMessage
Sends a text message to the WebSocket server.- Parameters:
message- The message to send.- Returns:
- A CompletableFuture that completes when the message is sent.
-
disconnect
Closes the WebSocket connection.- Parameters:
statusCode- The WebSocket close status code.reason- The reason for closing.- Returns:
- A CompletableFuture that completes when the connection is closed.
-
addMessageListener
Adds a listener for incoming text messages.- Parameters:
listener- The message listener.
-
removeMessageListener
Removes a message listener.- Parameters:
listener- The message listener to remove.
-
addCloseListener
Adds a listener for connection close events.- Parameters:
listener- The close listener.
-
removeCloseListener
Removes a close listener.- Parameters:
listener- The close listener to remove.
-
addErrorListener
Adds a listener for connection errors.- Parameters:
listener- The error listener.
-
removeErrorListener
Removes an error listener.- Parameters:
listener- The error listener to remove.
-
isConnected
public boolean isConnected()Checks if the client is connected to the WebSocket server.- Returns:
- true if connected, false otherwise.
-