public final class WebSocket extends Object
Modifier and Type | Class and Description |
---|---|
static class |
WebSocket.PayloadType
The format of a message payload.
|
Modifier and Type | Method and Description |
---|---|
void |
close(int code,
String reason)
Send a close frame to the server.
|
Response |
connect(WebSocketListener listener)
Connects the web socket and blocks until the response can be processed.
|
boolean |
isClosed()
True if this web socket is closed and can no longer be written to.
|
okio.BufferedSink |
newMessageSink(WebSocket.PayloadType type)
Stream a message payload to the server of the specified {code type}.
|
static WebSocket |
newWebSocket(OkHttpClient client,
Request request)
Prepares the
request to create a web socket at some point in the future. |
Request |
request()
The HTTP request which initiated this web socket.
|
void |
sendMessage(WebSocket.PayloadType type,
okio.Buffer payload)
Send a message payload the server of the specified
type . |
public static WebSocket newWebSocket(OkHttpClient client, Request request)
request
to create a web socket at some point in the future.
TODO Move to OkHttpClient as non-static once web sockets are finalized!
public Request request()
public Response connect(WebSocketListener listener) throws IOException
listener
.
Note that transport-layer success (receiving a HTTP response code,
headers and body) does not necessarily indicate application-layer success:
response
may still indicate an unhappy HTTP response code like 404
or 500.
IOException
- if the request could not be executed due to
a connectivity problem or timeout. Because networks can
fail during an exchange, it is possible that the remote server
accepted the request before the failure.IllegalStateException
- when the web socket has already been connected.public okio.BufferedSink newMessageSink(WebSocket.PayloadType type)
You must call close()
to complete the message. Calls to
flush()
write a frame fragment. The message may be empty.
IllegalStateException
- if not connected, already closed, or another writer is active.public void sendMessage(WebSocket.PayloadType type, okio.Buffer payload) throws IOException
type
.IllegalStateException
- if not connected, already closed, or another writer is active.IOException
public void close(int code, String reason) throws IOException
The corresponding WebSocketListener
will continue to get messages until its
onClose()
method is called.
It is an error to call this method before calling close on an active writer. Calling this method more than once has no effect.
IOException
public boolean isClosed()
Note: Due to the asynchronous nature of a websocket, a true
value from method does not
guarantee that the connection will be open or even that you will be able to write in a
subsequent call.
Copyright © 2015. All rights reserved.