- All Known Implementing Classes:
RealWebSocket
public interface WebSocket
A non-blocking interface to a WebSocket. In normal operation, a WebSocket will be processed through a sequence of
states: open, message, and close. Use a
WebSocket.Factory (typically the Httpd client) to create
instances.- Since:
- Java 17+
- Author:
- Kimi Liu
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceA factory for creating WebSockets. -
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.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.
-
Method Details
-
request
Request request()- Returns:
- The original request that initiated this WebSocket.
-
queueSize
long queueSize()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.- Returns:
- The size of the outgoing message queue in bytes.
-
send
Attempts 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.
- Parameters:
text- The text message to send.- Returns:
- true if the message was successfully enqueued for sending.
-
send
boolean send(org.miaixz.bus.core.io.ByteString bytes) Attempts 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.
- Parameters:
bytes- The binary message to send.- Returns:
- true if the message was successfully enqueued for sending.
-
close
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 tosend(java.lang.String)will return false and their messages will not be enqueued.- 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.
- Throws:
IllegalArgumentException- if the code is invalid.
-
cancel
void cancel()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.
-