okhttp / okhttp3 / WebSocket

WebSocket

interface WebSocket

A non-blocking interface to a web socket. Use the factory to create instances; usually this is OkHttpClient.

Web Socket Lifecycle

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.

Types

Factory

interface Factory

Functions

cancel

abstract fun cancel(): Unit

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.

close

abstract fun close(code: Int, reason: String?): Boolean

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 send will return false and their messages will not be enqueued.

queueSize

abstract fun queueSize(): Long

Returns the size in bytes of all messages enqueued to be transmitted to the server. This doesn't include framing overhead. It also doesn't include any bytes buffered by the operating system or network intermediaries. This method returns 0 if no messages are waiting in the queue. If may return a nonzero value after the web socket has been canceled; this indicates that enqueued messages were not transmitted.

request

abstract fun request(): Request

Returns the original request that initiated this web socket.

send

abstract fun send(text: String): Boolean

Attempts to enqueue text to be UTF-8 encoded and sent as a the data of a text (type 0x1) message.

abstract fun send(bytes: ByteString): Boolean

Attempts to enqueue bytes to be sent as a the data of a binary (type 0x2) message.