Module bus.http

Class RealWebSocket

java.lang.Object
org.miaixz.bus.http.socket.RealWebSocket
All Implemented Interfaces:
WebSocket, WebSocketReader.FrameCallback

public final class RealWebSocket extends Object implements WebSocket, WebSocketReader.FrameCallback
An implementation of the WebSocket protocol (RFC 6455). This class manages the lifecycle of a WebSocket connection, including message queuing, sending and receiving frames, handling ping/pong, and performing a graceful shutdown. It uses a listener to notify of events.
Since:
Java 17+
Author:
Kimi Liu
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    A holder for the sink and source of a WebSocket connection.

    Nested classes/interfaces inherited from interface org.miaixz.bus.http.socket.WebSocket

    WebSocket.Factory
  • Constructor Summary

    Constructors
    Constructor
    Description
    RealWebSocket(Request request, WebSocketListener listener, Random random, long pingIntervalMillis)
    Constructs a new RealWebSocket.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Immediately and violently release resources held by this web socket, discarding any enqueued messages.
    boolean
    close(int code, String reason)
    Attempts to initiate a graceful shutdown of this web socket.
    void
    connect(Httpd client)
    Initiates the WebSocket connection.
    void
    Notifies the listener of a failure and closes the connection.
    void
    Initializes the frame reader, writer, and ping executor.
    void
    Starts the reader loop to process incoming frames.
    void
    onReadClose(int code, String reason)
    Invoked when a close frame is received.
    void
    Invoked when a text message is received.
    void
    onReadMessage(org.miaixz.bus.core.io.ByteString bytes)
    Invoked when a binary message is received.
    void
    onReadPing(org.miaixz.bus.core.io.ByteString payload)
    Invoked when a ping frame is received.
    void
    onReadPong(org.miaixz.bus.core.io.ByteString buffer)
    Invoked when a pong frame is received.
    long
    Returns the number of bytes of application data that have been enqueued to be transmitted to the server.
     
    boolean
    send(String text)
    Attempts to enqueue text to be transmitted as a text (type 0x1) message.
    boolean
    send(org.miaixz.bus.core.io.ByteString bytes)
    Attempts to enqueue bytes to be transmitted as a binary (type 0x2) message.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RealWebSocket

      public RealWebSocket(Request request, WebSocketListener listener, Random random, long pingIntervalMillis)
      Constructs a new RealWebSocket.
      Parameters:
      request - The original HTTP GET request.
      listener - The listener for WebSocket events.
      random - A source of randomness.
      pingIntervalMillis - The interval for sending pings in milliseconds, or 0 for no pings.
      Throws:
      IllegalArgumentException - if the request method is not GET.
  • Method Details

    • request

      public Request request()
      Specified by:
      request in interface WebSocket
      Returns:
      The original request that initiated this WebSocket.
    • queueSize

      public long queueSize()
      Description copied from interface: WebSocket
      Returns the number of bytes of application data that have been enqueued to be transmitted to the server. This doesn't include framing overhead. It also doesn't include any bytes that have been buffered by the operating system or network intermediaries. This method returns 0 if no messages are waiting in the queue. It may return a non-zero value after the web socket has been canceled; this indicates that enqueued messages were not transmitted.
      Specified by:
      queueSize in interface WebSocket
      Returns:
      The size of the outgoing message queue in bytes.
    • cancel

      public void cancel()
      Description copied from interface: WebSocket
      Immediately and violently release resources held by this web socket, discarding any enqueued messages. This does nothing if the web socket has already been closed or canceled.
      Specified by:
      cancel in interface WebSocket
    • connect

      public void connect(Httpd client)
      Initiates the WebSocket connection.
      Parameters:
      client - The HTTP client to use for the connection.
    • initReaderAndWriter

      public void initReaderAndWriter(String name, RealWebSocket.Streams streams)
      Initializes the frame reader, writer, and ping executor.
      Parameters:
      name - A descriptive name for the threads.
      streams - The sink and source for the connection.
    • loopReader

      public void loopReader() throws IOException
      Starts the reader loop to process incoming frames.
      Throws:
      IOException - if a read error occurs.
    • onReadMessage

      public void onReadMessage(String text)
      Description copied from interface: WebSocketReader.FrameCallback
      Invoked when a text message is received.
      Specified by:
      onReadMessage in interface WebSocketReader.FrameCallback
      Parameters:
      text - The text content.
    • onReadMessage

      public void onReadMessage(org.miaixz.bus.core.io.ByteString bytes)
      Description copied from interface: WebSocketReader.FrameCallback
      Invoked when a binary message is received.
      Specified by:
      onReadMessage in interface WebSocketReader.FrameCallback
      Parameters:
      bytes - The binary content.
    • onReadPing

      public void onReadPing(org.miaixz.bus.core.io.ByteString payload)
      Description copied from interface: WebSocketReader.FrameCallback
      Invoked when a ping frame is received.
      Specified by:
      onReadPing in interface WebSocketReader.FrameCallback
      Parameters:
      payload - The ping payload.
    • onReadPong

      public void onReadPong(org.miaixz.bus.core.io.ByteString buffer)
      Description copied from interface: WebSocketReader.FrameCallback
      Invoked when a pong frame is received.
      Specified by:
      onReadPong in interface WebSocketReader.FrameCallback
      Parameters:
      buffer - The pong payload.
    • onReadClose

      public void onReadClose(int code, String reason)
      Description copied from interface: WebSocketReader.FrameCallback
      Invoked when a close frame is received.
      Specified by:
      onReadClose in interface WebSocketReader.FrameCallback
      Parameters:
      code - The close code.
      reason - The close reason.
    • send

      public boolean send(String text)
      Description copied from interface: WebSocket
      Attempts to enqueue text to be transmitted as a text (type 0x1) message.

      This method returns true if the message was enqueued. Messages that would overflow the outgoing message buffer will be rejected and trigger a graceful shutdown of this web socket. This method returns false in that case, and in any other case where this web socket is closing, closed, or canceled.

      Specified by:
      send in interface WebSocket
      Parameters:
      text - The text message to send.
      Returns:
      true if the message was successfully enqueued for sending.
    • send

      public boolean send(org.miaixz.bus.core.io.ByteString bytes)
      Description copied from interface: WebSocket
      Attempts to enqueue bytes to be transmitted as a binary (type 0x2) message.

      This method returns true if the message was enqueued. Messages that would overflow the outgoing message buffer (16 MiB) will be rejected and trigger a graceful shutdown of this web socket. This method returns false in that case, and in any other case where this web socket is closing, closed, or canceled.

      Specified by:
      send in interface WebSocket
      Parameters:
      bytes - The binary message to send.
      Returns:
      true if the message was successfully enqueued for sending.
    • close

      public boolean close(int code, String reason)
      Description copied from interface: WebSocket
      Attempts to initiate a graceful shutdown of this web socket. Any already-enqueued messages will be transmitted before the close message is sent but subsequent calls to WebSocket.send(java.lang.String) will return false and their messages will not be enqueued.
      Specified by:
      close in interface WebSocket
      Parameters:
      code - A status code as defined by Section 7.4 of RFC 6455.
      reason - A descriptive reason for the close, or null.
      Returns:
      true if the close message was successfully enqueued.
    • failWebSocket

      public void failWebSocket(Exception e, Response response)
      Notifies the listener of a failure and closes the connection.
      Parameters:
      e - The exception that caused the failure.
      response - The response received before the failure, or null.