接口 WebSocketTransport
public interface WebSocketTransport
WebSocket client interface.
Similar to HTTPClient design:
- Client instance is stateless and reusable
- Each connect() creates a new connection
- Connection configuration is passed via WebSocketRequest
- Supports generic type parameter for message format (String or byte[])
Implementations:
JdkWebSocketTransport- Based on JDK 11+ HttpClientOkHttpWebSocketTransport- Based on OkHttp
Usage example (text protocol):
// Create client (reusable)
WebSocketTransport client = JdkWebSocketTransport.create();
// Create connection request
WebSocketRequest request = WebSocketRequest.builder("wss://api.openai.com/v1/realtime")
.header("Authorization", "Bearer " + apiKey)
.build();
// Establish connection (specify String type)
client.connect(request, String.class)
.flatMapMany(connection -> {
// Send JSON configuration
connection.send(sessionConfig).subscribe();
// Receive JSON messages
return connection.receive();
})
.subscribe(json -> handleMessage(json));
Usage example (binary protocol):
// Establish connection (specify byte[] type)
client.connect(request, byte[].class)
.flatMapMany(connection -> {
// Send binary data
connection.send(binaryFrame).subscribe();
// Receive binary data
return connection.receive();
})
.subscribe(data -> handleBinaryMessage(data));
-
方法概要
修饰符和类型方法说明<T> reactor.core.publisher.Mono<WebSocketConnection<T>> connect(WebSocketRequest request, Class<T> messageType) Establish a WebSocket connection.voidshutdown()Shutdown the client and release resources.
-
方法详细资料
-
connect
<T> reactor.core.publisher.Mono<WebSocketConnection<T>> connect(WebSocketRequest request, Class<T> messageType) Establish a WebSocket connection.- 类型参数:
T- Message type: String for text protocol, byte[] for binary protocol- 参数:
request- Connection request configurationmessageType- Class object for message type (String.class or byte[].class)- 返回:
- Mono that emits WebSocketConnection on successful connection
-
shutdown
void shutdown()Shutdown the client and release resources.After shutdown, this client should not be used anymore.
-