接口 WebSocketConnection<T>

类型参数:
T - Message type: String for text protocols, byte[] for binary protocols
所有已知实现类:
JdkWebSocketConnection, OkHttpWebSocketConnection

public interface WebSocketConnection<T>
WebSocket connection interface.

Represents an active WebSocket connection with send/receive capabilities. Uses generic type parameter to support different message formats:

  • WebSocketConnection<String> - Text protocol (JSON over WebSocket)
  • WebSocketConnection<byte[]> - Binary protocol

Error handling: All methods return Mono/Flux that propagate errors through Reactor's error channel. Errors are wrapped in WebSocketTransportException with connection context.

Logging: Implementations should log at appropriate levels:

  • INFO: Connection established/closed
  • DEBUG: Message send/receive operations
  • TRACE: Detailed message content (size, preview)
  • ERROR: Connection errors, send/receive failures

Usage example (text protocol):


 WebSocketTransport client = JdkWebSocketTransport.create();
 client.connect(request, String.class)
     .flatMapMany(connection -> {
         // Send JSON message
         connection.send("{\"type\":\"config\"}").subscribe();

         // Receive JSON messages
         return connection.receive();
     })
     .subscribe(
         json -> handleMessage(json),
         error -> handleError(error)  // WebSocketTransportException with context
     );
 

Usage example (binary protocol):


 client.connect(request, byte[].class)
     .flatMapMany(connection -> {
         // Send binary message
         connection.send(binaryData).subscribe();

         // Receive binary messages
         return connection.receive();
     })
     .subscribe(
         data -> handleBinaryMessage(data),
         error -> handleError(error)
     );
 
  • 方法概要

    修饰符和类型
    方法
    说明
    reactor.core.publisher.Mono<Void>
    Close the connection.
    Get close information (if closed).
    boolean
    Check if connection is open.
    reactor.core.publisher.Flux<T>
    Receive message stream.
    reactor.core.publisher.Mono<Void>
    send(T data)
    Send a message.
  • 方法详细资料

    • send

      reactor.core.publisher.Mono<Void> send(T data)
      Send a message.

      Implementation should:

      • Log at DEBUG level before sending
      • Log at TRACE level with message size
      • Wrap errors in WebSocketTransportException using onErrorMap
      • Log errors at ERROR level with full context
      参数:
      data - Message data (String or byte[])
      返回:
      Mono that completes when send is done, or emits WebSocketTransportException on error
    • receive

      reactor.core.publisher.Flux<T> receive()
      Receive message stream.

      The returned Flux:

      • Completes when connection is closed normally
      • Emits error (WebSocketTransportException) on connection failure
      • Logs each received message at TRACE level

      Implementation should:

      • Log received messages at TRACE level with size
      • Wrap errors in WebSocketTransportException using onErrorMap
      • Log errors at ERROR level with connection context
      返回:
      Message stream (String or byte[])
    • close

      reactor.core.publisher.Mono<Void> close()
      Close the connection.

      Implementation should:

      • Log at INFO level with close code and reason
      • Send WebSocket close frame with code 1000 (normal closure)
      • Clean up resources
      返回:
      Mono that completes when connection is closed
    • isOpen

      boolean isOpen()
      Check if connection is open.
      返回:
      true if connection is open
    • getCloseInfo

      CloseInfo getCloseInfo()
      Get close information (if closed).
      返回:
      Close info, or null if not closed