public final class RealWebSocket extends java.lang.Object implements WebSocket, WebSocketReader.FrameCallback
| Modifier and Type | Class and Description |
|---|---|
(package private) class |
RealWebSocket.CancelRunnable |
(package private) static class |
RealWebSocket.Close |
(package private) static class |
RealWebSocket.Message |
private class |
RealWebSocket.PingRunnable |
static class |
RealWebSocket.Streams |
WebSocket.Factory| Modifier and Type | Field and Description |
|---|---|
private boolean |
awaitingPong
True if we have sent a ping that is still awaiting a reply.
|
private Call |
call
Non-null for client web sockets.
|
private static long |
CANCEL_AFTER_CLOSE_MILLIS
The maximum amount of time after the client calls
close(int, java.lang.String) to wait for a graceful
shutdown. |
private java.util.concurrent.ScheduledFuture<?> |
cancelFuture
When executed this will cancel this websocket.
|
private boolean |
enqueuedClose
True if we've enqueued a close frame.
|
private java.util.concurrent.ScheduledExecutorService |
executor
Null until this web socket is connected.
|
private boolean |
failed
True if this web socket failed and the listener has been notified.
|
private java.lang.String |
key |
(package private) WebSocketListener |
listener |
private static long |
MAX_QUEUE_SIZE
The maximum number of bytes to enqueue.
|
private java.util.ArrayDeque<java.lang.Object> |
messageAndCloseQueue
Outgoing messages and close frames in the order they should be written.
|
private static java.util.List<Protocol> |
ONLY_HTTP1 |
private Request |
originalRequest
The application's original request unadulterated by web socket headers.
|
private long |
pingIntervalMillis |
private java.util.ArrayDeque<okio.ByteString> |
pongQueue
Outgoing pongs in the order they should be written.
|
private long |
queueSize
The total size in bytes of enqueued but not yet transmitted messages.
|
private java.util.Random |
random |
private WebSocketReader |
reader
Null until this web socket is connected.
|
private int |
receivedCloseCode
The close code from the peer, or -1 if this web socket has not yet read a close frame.
|
private java.lang.String |
receivedCloseReason
The close reason from the peer, or null if this web socket has not yet read a close frame.
|
private int |
receivedPingCount
Total number of pings received by this web socket.
|
private int |
receivedPongCount
Total number of pongs received by this web socket.
|
private int |
sentPingCount
Total number of pings sent by this web socket.
|
private RealWebSocket.Streams |
streams
The streams held by this web socket.
|
private WebSocketWriter |
writer
Null until this web socket is connected.
|
private java.lang.Runnable |
writerRunnable
This runnable processes the outgoing queues.
|
| Constructor and Description |
|---|
RealWebSocket(Request request,
WebSocketListener listener,
java.util.Random random,
long pingIntervalMillis) |
| Modifier and Type | Method and Description |
|---|---|
(package private) void |
awaitTermination(int timeout,
java.util.concurrent.TimeUnit timeUnit)
For testing: wait until the web socket's executor has terminated.
|
void |
cancel()
Immediately and violently release resources held by this web socket, discarding any enqueued
messages.
|
(package private) void |
checkResponse(Response response) |
boolean |
close(int code,
java.lang.String reason)
Attempts to initiate a graceful shutdown of this web socket.
|
(package private) boolean |
close(int code,
java.lang.String reason,
long cancelAfterCloseMillis) |
void |
connect(OkHttpClient client) |
void |
failWebSocket(java.lang.Exception e,
Response response) |
void |
initReaderAndWriter(java.lang.String name,
RealWebSocket.Streams streams) |
void |
loopReader()
Receive frames until there are no more.
|
void |
onReadClose(int code,
java.lang.String reason) |
void |
onReadMessage(okio.ByteString bytes) |
void |
onReadMessage(java.lang.String text) |
void |
onReadPing(okio.ByteString payload) |
void |
onReadPong(okio.ByteString buffer) |
(package private) boolean |
pong(okio.ByteString payload) |
(package private) boolean |
processNextFrame()
For testing: receive a single frame and return true if there are more frames to read.
|
long |
queueSize()
Returns the size in bytes of all messages enqueued to be transmitted to the server.
|
(package private) int |
receivedPingCount() |
(package private) int |
receivedPongCount() |
Request |
request()
Returns the original request that initiated this web socket.
|
private void |
runWriter() |
boolean |
send(okio.ByteString bytes)
Attempts to enqueue
bytes to be sent as a the data of a binary (type 0x2)
message. |
private boolean |
send(okio.ByteString data,
int formatOpcode) |
boolean |
send(java.lang.String text)
Attempts to enqueue
text to be UTF-8 encoded and sent as a the data of a text (type
0x1) message. |
(package private) int |
sentPingCount() |
(package private) void |
tearDown()
For testing: force this web socket to release its threads.
|
(package private) boolean |
writeOneFrame()
Attempts to remove a single frame from a queue and send it.
|
(package private) void |
writePingFrame() |
private static final java.util.List<Protocol> ONLY_HTTP1
private static final long MAX_QUEUE_SIZE
private static final long CANCEL_AFTER_CLOSE_MILLIS
close(int, java.lang.String) to wait for a graceful
shutdown. If the server doesn't respond the websocket will be canceled.private final Request originalRequest
final WebSocketListener listener
private final java.util.Random random
private final long pingIntervalMillis
private final java.lang.String key
private Call call
private final java.lang.Runnable writerRunnable
runWriter() to after enqueueing.private WebSocketReader reader
private WebSocketWriter writer
private java.util.concurrent.ScheduledExecutorService executor
private RealWebSocket.Streams streams
private final java.util.ArrayDeque<okio.ByteString> pongQueue
private final java.util.ArrayDeque<java.lang.Object> messageAndCloseQueue
private long queueSize
private boolean enqueuedClose
private java.util.concurrent.ScheduledFuture<?> cancelFuture
private int receivedCloseCode
private java.lang.String receivedCloseReason
private boolean failed
private int sentPingCount
private int receivedPingCount
private int receivedPongCount
private boolean awaitingPong
public RealWebSocket(Request request, WebSocketListener listener, java.util.Random random, long pingIntervalMillis)
public Request request()
WebSocketpublic long queueSize()
WebSocketpublic void cancel()
WebSocketpublic void connect(OkHttpClient client)
void checkResponse(Response response) throws java.net.ProtocolException
java.net.ProtocolExceptionpublic void initReaderAndWriter(java.lang.String name,
RealWebSocket.Streams streams)
throws java.io.IOException
java.io.IOExceptionpublic void loopReader()
throws java.io.IOException
java.io.IOExceptionboolean processNextFrame()
throws java.io.IOException
java.io.IOExceptionvoid awaitTermination(int timeout,
java.util.concurrent.TimeUnit timeUnit)
throws java.lang.InterruptedException
java.lang.InterruptedExceptionvoid tearDown()
throws java.lang.InterruptedException
java.lang.InterruptedExceptionint sentPingCount()
int receivedPingCount()
int receivedPongCount()
public void onReadMessage(java.lang.String text)
throws java.io.IOException
onReadMessage in interface WebSocketReader.FrameCallbackjava.io.IOExceptionpublic void onReadMessage(okio.ByteString bytes)
throws java.io.IOException
onReadMessage in interface WebSocketReader.FrameCallbackjava.io.IOExceptionpublic void onReadPing(okio.ByteString payload)
onReadPing in interface WebSocketReader.FrameCallbackpublic void onReadPong(okio.ByteString buffer)
onReadPong in interface WebSocketReader.FrameCallbackpublic void onReadClose(int code,
java.lang.String reason)
onReadClose in interface WebSocketReader.FrameCallbackpublic boolean send(java.lang.String text)
WebSockettext 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.
public boolean send(okio.ByteString bytes)
WebSocketbytes 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.
private boolean send(okio.ByteString data,
int formatOpcode)
boolean pong(okio.ByteString payload)
public boolean close(int code,
java.lang.String reason)
WebSocketWebSocket.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 and if a graceful shutdown was already underway or if the web socket is already closed or canceled.
close in interface WebSocketcode - Status code as defined by Section 7.4 of RFC 6455.reason - Reason for shutting down or null.boolean close(int code,
java.lang.String reason,
long cancelAfterCloseMillis)
private void runWriter()
boolean writeOneFrame()
throws java.io.IOException
If a frame cannot be sent - because there are none enqueued or because the web socket is not connected - this does nothing and returns false. Otherwise this returns true and the caller should immediately invoke this method again until it returns false.
This method may only be invoked by the writer thread. There may be only thread invoking this method at a time.
java.io.IOExceptionvoid writePingFrame()
public void failWebSocket(java.lang.Exception e,
@Nullable
Response response)