public interface WebSocket
OkHttpClient.
Upon normal operation each web socket progresses through a sequence of states:
Web sockets may fail due to HTTP upgrade problems, connectivity problems, or if either peer chooses to short-circuit the graceful shutdown process:
Note that the state progression is independent for each peer. Arriving at a gracefully-closed state indicates that a peer has sent all of its outgoing messages and received all of its incoming messages. But it does not guarantee that the other peer will successfully receive all of its incoming messages.
| 限定符和类型 | 接口和说明 |
|---|---|
static interface |
WebSocket.Factory |
| 限定符和类型 | 方法和说明 |
|---|---|
void |
cancel()
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.
|
long |
queueSize()
Returns the size in bytes of all messages enqueued to be transmitted to the server.
|
Request |
request()
Returns the original request that initiated this web socket.
|
boolean |
send(ByteString bytes)
Attempts to enqueue
bytes to be sent as a the data of a binary (type 0x2)
message. |
boolean |
send(String text)
Attempts to enqueue
text to be UTF-8 encoded and sent as a the data of a text (type
0x1) message. |
Request request()
long queueSize()
boolean send(String text)
text to be UTF-8 encoded and sent as a the data of 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.
This method returns immediately.
boolean send(ByteString bytes)
bytes to be sent as a the data of 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.
This method returns immediately.
boolean close(int code,
@Nullable
String reason)
send(java.lang.String) will
return false and their messages will not be enqueued.
This returns true if a graceful shutdown was initiated by this call. It returns false if a graceful shutdown was already underway or if the web socket is already closed or canceled.
code - Status code as defined by Section
7.4 of RFC 6455.reason - Reason for shutting down or null.IllegalArgumentException - if code is invalid.void cancel()
Copyright © 2022. All rights reserved.