java.lang.Object
org.miaixz.bus.http.socket.RealWebSocket
- All Implemented Interfaces:
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 ClassesModifier and TypeClassDescriptionstatic classA 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
ConstructorsConstructorDescriptionRealWebSocket(Request request, WebSocketListener listener, Random random, long pingIntervalMillis) Constructs a new RealWebSocket. -
Method Summary
Modifier and TypeMethodDescriptionvoidcancel()Immediately and violently release resources held by this web socket, discarding any enqueued messages.booleanAttempts to initiate a graceful shutdown of this web socket.voidInitiates the WebSocket connection.voidfailWebSocket(Exception e, Response response) Notifies the listener of a failure and closes the connection.voidinitReaderAndWriter(String name, RealWebSocket.Streams streams) Initializes the frame reader, writer, and ping executor.voidStarts the reader loop to process incoming frames.voidonReadClose(int code, String reason) Invoked when a close frame is received.voidonReadMessage(String text) Invoked when a text message is received.voidonReadMessage(org.miaixz.bus.core.io.ByteString bytes) Invoked when a binary message is received.voidonReadPing(org.miaixz.bus.core.io.ByteString payload) Invoked when a ping frame is received.voidonReadPong(org.miaixz.bus.core.io.ByteString buffer) Invoked when a pong frame is received.longReturns the number of bytes of application data that have been enqueued to be transmitted to the server.request()booleanAttempts to enqueuetextto be transmitted as a text (type0x1) message.booleansend(org.miaixz.bus.core.io.ByteString bytes) Attempts to enqueuebytesto be transmitted as a binary (type0x2) message.
-
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
-
queueSize
public long queueSize()Description copied from interface:WebSocketReturns 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. -
cancel
public void cancel()Description copied from interface:WebSocketImmediately 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. -
connect
Initiates the WebSocket connection.- Parameters:
client- The HTTP client to use for the connection.
-
initReaderAndWriter
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
Starts the reader loop to process incoming frames.- Throws:
IOException- if a read error occurs.
-
onReadMessage
Description copied from interface:WebSocketReader.FrameCallbackInvoked when a text message is received.- Specified by:
onReadMessagein interfaceWebSocketReader.FrameCallback- Parameters:
text- The text content.
-
onReadMessage
public void onReadMessage(org.miaixz.bus.core.io.ByteString bytes) Description copied from interface:WebSocketReader.FrameCallbackInvoked when a binary message is received.- Specified by:
onReadMessagein interfaceWebSocketReader.FrameCallback- Parameters:
bytes- The binary content.
-
onReadPing
public void onReadPing(org.miaixz.bus.core.io.ByteString payload) Description copied from interface:WebSocketReader.FrameCallbackInvoked when a ping frame is received.- Specified by:
onReadPingin interfaceWebSocketReader.FrameCallback- Parameters:
payload- The ping payload.
-
onReadPong
public void onReadPong(org.miaixz.bus.core.io.ByteString buffer) Description copied from interface:WebSocketReader.FrameCallbackInvoked when a pong frame is received.- Specified by:
onReadPongin interfaceWebSocketReader.FrameCallback- Parameters:
buffer- The pong payload.
-
onReadClose
Description copied from interface:WebSocketReader.FrameCallbackInvoked when a close frame is received.- Specified by:
onReadClosein interfaceWebSocketReader.FrameCallback- Parameters:
code- The close code.reason- The close reason.
-
send
Description copied from interface:WebSocketAttempts to enqueuetextto be transmitted as a text (type0x1) 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.
-
send
public boolean send(org.miaixz.bus.core.io.ByteString bytes) Description copied from interface:WebSocketAttempts to enqueuebytesto be transmitted as a binary (type0x2) 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.
-
close
Description copied from interface:WebSocketAttempts 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 toWebSocket.send(java.lang.String)will return false and their messages will not be enqueued.- Specified by:
closein interfaceWebSocket- Parameters:
code- A status code as defined by Section 7.4 of RFC 6455.reason- A descriptive reason for the close, ornull.- Returns:
- true if the close message was successfully enqueued.
-
failWebSocket
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.
-