Update okhttp3

Reviewed By: bestander

Differential Revision: D5078004

fbshipit-source-id: 79c66cedeeb682d8bb4e67798b41115899fd1c81
This commit is contained in:
Emil Sjolander
2017-05-18 08:16:02 -07:00
committed by Facebook Github Bot
parent 658f632f59
commit 93a1d592d6
15 changed files with 124 additions and 216 deletions
@@ -21,24 +21,22 @@ import com.facebook.common.logging.FLog;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.ws.WebSocket;
import okhttp3.ws.WebSocketCall;
import okhttp3.ws.WebSocketListener;
import okio.Buffer;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;
/**
* A wrapper around WebSocketClient that reconnects automatically
*/
final public class ReconnectingWebSocket implements WebSocketListener {
final public class ReconnectingWebSocket extends WebSocketListener {
private static final String TAG = ReconnectingWebSocket.class.getSimpleName();
private static final int RECONNECT_DELAY_MS = 2000;
public interface MessageCallback {
void onMessage(ResponseBody message);
void onMessage(String text);
void onMessage(ByteString bytes);
}
public interface ConnectionCallback {
@@ -77,8 +75,7 @@ final public class ReconnectingWebSocket implements WebSocketListener {
.build();
Request request = new Request.Builder().url(mUrl).build();
WebSocketCall call = WebSocketCall.create(httpClient, request);
call.enqueue(this);
httpClient.newWebSocket(request, this);
}
private synchronized void delayedReconnect() {
@@ -122,7 +119,7 @@ final public class ReconnectingWebSocket implements WebSocketListener {
if (mWebSocket != null) {
try {
mWebSocket.close(1000, "End of session");
} catch (IOException e) {
} catch (Exception e) {
// swallow, no need to handle it here
}
mWebSocket = null;
@@ -145,9 +142,9 @@ final public class ReconnectingWebSocket implements WebSocketListener {
}
@Override
public synchronized void onFailure(IOException e, Response response) {
public synchronized void onFailure(WebSocket webSocket, Throwable t, Response response) {
if (mWebSocket != null) {
abort("Websocket exception", e);
abort("Websocket exception", t);
}
if (!mClosed) {
if (mConnectionCallback != null) {
@@ -158,17 +155,21 @@ final public class ReconnectingWebSocket implements WebSocketListener {
}
@Override
public synchronized void onMessage(ResponseBody message) {
public synchronized void onMessage(WebSocket webSocket, String text) {
if (mMessageCallback != null) {
mMessageCallback.onMessage(message);
mMessageCallback.onMessage(text);
}
}
@Override
public synchronized void onPong(Buffer payload) { }
public synchronized void onMessage(WebSocket webSocket, ByteString bytes) {
if (mMessageCallback != null) {
mMessageCallback.onMessage(bytes);
}
}
@Override
public synchronized void onClose(int code, String reason) {
public synchronized void onClosed(WebSocket webSocket, int code, String reason) {
mWebSocket = null;
if (!mClosed) {
if (mConnectionCallback != null) {
@@ -178,9 +179,17 @@ final public class ReconnectingWebSocket implements WebSocketListener {
}
}
public synchronized void sendMessage(RequestBody message) throws IOException {
public synchronized void sendMessage(String message) throws IOException {
if (mWebSocket != null) {
mWebSocket.sendMessage(message);
mWebSocket.send(message);
} else {
throw new ClosedChannelException();
}
}
public synchronized void sendMessage(ByteString message) throws IOException {
if (mWebSocket != null) {
mWebSocket.send(message);
} else {
throw new ClosedChannelException();
}