Skip to content

Commit 93a1d59

Browse files
Emil Sjolanderfacebook-github-bot
authored andcommitted
Update okhttp3
Reviewed By: bestander Differential Revision: D5078004 fbshipit-source-id: 79c66cedeeb682d8bb4e67798b41115899fd1c81
1 parent 658f632 commit 93a1d59

File tree

15 files changed

+121
-213
lines changed

15 files changed

+121
-213
lines changed

ReactAndroid/build.gradle

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,9 @@ dependencies {
283283
compile 'com.facebook.fresco:imagepipeline-okhttp3:1.0.1'
284284
compile 'com.facebook.soloader:soloader:0.1.0'
285285
compile 'com.google.code.findbugs:jsr305:3.0.0'
286-
compile 'com.squareup.okhttp3:okhttp:3.4.1'
287-
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.4.1'
288-
compile 'com.squareup.okhttp3:okhttp-ws:3.4.1'
289-
compile 'com.squareup.okio:okio:1.9.0'
286+
compile 'com.squareup.okhttp3:okhttp:3.8.0'
287+
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.8.0'
288+
compile 'com.squareup.okio:okio:1.13.0'
290289
compile 'org.webkit:android-jsc:r174650'
291290

292291
testCompile "junit:junit:${JUNIT_VERSION}"

ReactAndroid/src/main/java/com/facebook/react/devsupport/BUCK

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ android_library(
1212
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
1313
react_native_dep("third-party/java/jsr-305:jsr-305"),
1414
react_native_dep("third-party/java/okhttp:okhttp3"),
15-
react_native_dep("third-party/java/okhttp:okhttp3-ws"),
1615
react_native_dep("third-party/java/okio:okio"),
1716
react_native_target("java/com/facebook/react/bridge:bridge"),
1817
react_native_target("java/com/facebook/react/common:common"),

ReactAndroid/src/main/java/com/facebook/react/devsupport/InspectorPackagerConnection.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@
1919

2020
import okhttp3.OkHttpClient;
2121
import okhttp3.Request;
22-
import okhttp3.RequestBody;
2322
import okhttp3.Response;
24-
import okhttp3.ResponseBody;
25-
import okhttp3.ws.WebSocket;
26-
import okhttp3.ws.WebSocketCall;
27-
import okhttp3.ws.WebSocketListener;
28-
import okio.Buffer;
23+
import okhttp3.WebSocket;
24+
import okhttp3.WebSocketListener;
2925
import org.json.JSONArray;
3026
import org.json.JSONException;
3127
import org.json.JSONObject;
@@ -175,7 +171,7 @@ private JSONObject makePageIdPayload(String pageId) throws JSONException {
175171
return payload;
176172
}
177173

178-
private class Connection implements WebSocketListener {
174+
private class Connection extends WebSocketListener {
179175
private static final int RECONNECT_DELAY_MS = 2000;
180176

181177
private final String mUrl;
@@ -196,32 +192,26 @@ public void onOpen(WebSocket webSocket, Response response) {
196192
}
197193

198194
@Override
199-
public void onFailure(IOException e, Response response) {
195+
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
200196
if (mWebSocket != null) {
201-
abort("Websocket exception", e);
197+
abort("Websocket exception", t);
202198
}
203199
if (!mClosed) {
204200
reconnect();
205201
}
206202
}
207203

208204
@Override
209-
public void onMessage(ResponseBody message) throws IOException {
205+
public void onMessage(WebSocket webSocket, String text) {
210206
try {
211-
handleProxyMessage(new JSONObject(message.string()));
212-
} catch (JSONException e) {
213-
throw new IOException(e);
214-
} finally {
215-
message.close();
207+
handleProxyMessage(new JSONObject(text));
208+
} catch (Exception e) {
209+
throw new RuntimeException(e);
216210
}
217211
}
218212

219213
@Override
220-
public void onPong(Buffer payload) {
221-
}
222-
223-
@Override
224-
public void onClose(int code, String reason) {
214+
public void onClosed(WebSocket webSocket, int code, String reason) {
225215
mWebSocket = null;
226216
closeAllConnections();
227217
if (!mClosed) {
@@ -240,8 +230,7 @@ public void connect() {
240230
.build();
241231

242232
Request request = new Request.Builder().url(mUrl).build();
243-
WebSocketCall call = WebSocketCall.create(httpClient, request);
244-
call.enqueue(this);
233+
httpClient.newWebSocket(request, this);
245234
}
246235

247236
private void reconnect() {
@@ -270,7 +259,7 @@ public void close() {
270259
if (mWebSocket != null) {
271260
try {
272261
mWebSocket.close(1000, "End of session");
273-
} catch (IOException e) {
262+
} catch (Exception e) {
274263
// swallow, no need to handle it here
275264
}
276265
mWebSocket = null;
@@ -285,8 +274,8 @@ protected Void doInBackground(WebSocket... sockets) {
285274
return null;
286275
}
287276
try {
288-
sockets[0].sendMessage(RequestBody.create(WebSocket.TEXT, object.toString()));
289-
} catch (IOException e) {
277+
sockets[0].send(object.toString());
278+
} catch (Exception e) {
290279
FLog.w(TAG, "Couldn't send event to packager", e);
291280
}
292281
return null;
@@ -304,7 +293,7 @@ private void closeWebSocketQuietly() {
304293
if (mWebSocket != null) {
305294
try {
306295
mWebSocket.close(1000, "End of session");
307-
} catch (IOException e) {
296+
} catch (Exception e) {
308297
// swallow, no need to handle it here
309298
}
310299
mWebSocket = null;

ReactAndroid/src/main/java/com/facebook/react/devsupport/JSDebuggerWebSocketClient.java

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.facebook.react.common.JavascriptException;
1919

2020
import java.io.IOException;
21+
import java.io.StringReader;
2122
import java.io.StringWriter;
2223
import java.util.HashMap;
2324
import java.util.concurrent.ConcurrentHashMap;
@@ -28,18 +29,14 @@
2829

2930
import okhttp3.OkHttpClient;
3031
import okhttp3.Request;
31-
import okhttp3.RequestBody;
3232
import okhttp3.Response;
33-
import okhttp3.ResponseBody;
34-
import okhttp3.ws.WebSocket;
35-
import okhttp3.ws.WebSocketCall;
36-
import okhttp3.ws.WebSocketListener;
37-
import okio.Buffer;
33+
import okhttp3.WebSocket;
34+
import okhttp3.WebSocketListener;
3835

3936
/**
4037
* A wrapper around WebSocketClient that recognizes RN debugging message format.
4138
*/
42-
public class JSDebuggerWebSocketClient implements WebSocketListener {
39+
public class JSDebuggerWebSocketClient extends WebSocketListener {
4340

4441
private static final String TAG = "JSDebuggerWebSocketClient";
4542

@@ -67,8 +64,7 @@ public void connect(String url, JSDebuggerCallback callback) {
6764
.build();
6865

6966
Request request = new Request.Builder().url(url).build();
70-
WebSocketCall call = WebSocketCall.create(mHttpClient, request);
71-
call.enqueue(this);
67+
mHttpClient.newWebSocket(request, this);
7268
}
7369

7470
public void prepareJSRuntime(JSDebuggerCallback callback) {
@@ -142,7 +138,7 @@ public void closeQuietly() {
142138
if (mWebSocket != null) {
143139
try {
144140
mWebSocket.close(1000, "End of session");
145-
} catch (IOException e) {
141+
} catch (Exception e) {
146142
// swallow, no need to handle it here
147143
}
148144
mWebSocket = null;
@@ -157,8 +153,8 @@ private void sendMessage(int requestID, String message) {
157153
return;
158154
}
159155
try {
160-
mWebSocket.sendMessage(RequestBody.create(WebSocket.TEXT, message));
161-
} catch (IOException e) {
156+
mWebSocket.send(message);
157+
} catch (Exception e) {
162158
triggerRequestFailure(requestID, e);
163159
}
164160
}
@@ -180,16 +176,11 @@ private void triggerRequestSuccess(int requestID, @Nullable String response) {
180176
}
181177

182178
@Override
183-
public void onMessage(ResponseBody response) throws IOException {
184-
if (response.contentType() != WebSocket.TEXT) {
185-
FLog.w(TAG, "Websocket received unexpected message with payload of type " + response.contentType());
186-
return;
187-
}
188-
179+
public void onMessage(WebSocket webSocket, String text) {
189180
Integer replyID = null;
190181

191182
try {
192-
JsonReader reader = new JsonReader(response.charStream());
183+
JsonReader reader = new JsonReader(new StringReader(text));
193184
String result = null;
194185
reader.beginObject();
195186
while (reader.hasNext()) {
@@ -218,14 +209,12 @@ public void onMessage(ResponseBody response) throws IOException {
218209
} else {
219210
abort("Parsing response message from websocket failed", e);
220211
}
221-
} finally {
222-
response.close();
223212
}
224213
}
225214

226215
@Override
227-
public void onFailure(IOException e, Response response) {
228-
abort("Websocket exception", e);
216+
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
217+
abort("Websocket exception", t);
229218
}
230219

231220
@Override
@@ -236,15 +225,10 @@ public void onOpen(WebSocket webSocket, Response response) {
236225
}
237226

238227
@Override
239-
public void onClose(int code, String reason) {
228+
public void onClosed(WebSocket webSocket, int code, String reason) {
240229
mWebSocket = null;
241230
}
242231

243-
@Override
244-
public void onPong(Buffer payload) {
245-
// ignore
246-
}
247-
248232
private void abort(String message, Throwable cause) {
249233
FLog.e(TAG, "Error occurred, shutting down websocket connection: " + message, cause);
250234
closeQuietly();

ReactAndroid/src/main/java/com/facebook/react/modules/websocket/BUCK

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ android_library(
1111
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
1212
react_native_dep("third-party/java/jsr-305:jsr-305"),
1313
react_native_dep("third-party/java/okhttp:okhttp3"),
14-
react_native_dep("third-party/java/okhttp:okhttp3-ws"),
1514
react_native_dep("third-party/java/okio:okio"),
1615
react_native_target("java/com/facebook/react/bridge:bridge"),
1716
react_native_target("java/com/facebook/react/common:common"),

ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99

1010
package com.facebook.react.modules.websocket;
1111

12-
import android.util.Base64;
12+
import javax.annotation.Nullable;
1313

1414
import java.io.IOException;
15-
import java.lang.IllegalStateException;
16-
import javax.annotation.Nullable;
15+
import java.net.URI;
16+
import java.net.URISyntaxException;
17+
import java.util.HashMap;
18+
import java.util.List;
19+
import java.util.Map;
20+
import java.util.concurrent.TimeUnit;
1721

1822
import com.facebook.common.logging.FLog;
1923
import com.facebook.react.bridge.Arguments;
@@ -33,21 +37,9 @@
3337

3438
import okhttp3.OkHttpClient;
3539
import okhttp3.Request;
36-
import okhttp3.RequestBody;
3740
import okhttp3.Response;
38-
import okhttp3.ResponseBody;
39-
import okhttp3.ws.WebSocket;
40-
import okhttp3.ws.WebSocketCall;
41-
import okhttp3.ws.WebSocketListener;
42-
43-
import java.net.URISyntaxException;
44-
import java.net.URI;
45-
import java.util.HashMap;
46-
import java.util.Map;
47-
import java.util.List;
48-
import java.util.concurrent.TimeUnit;
49-
50-
import okio.Buffer;
41+
import okhttp3.WebSocket;
42+
import okhttp3.WebSocketListener;
5143
import okio.ByteString;
5244

5345
@ReactModule(name = "WebSocketModule", hasConstants = false)
@@ -132,7 +124,7 @@ public void connect(
132124
}
133125
}
134126

135-
WebSocketCall.create(client, builder.build()).enqueue(new WebSocketListener() {
127+
client.newWebSocket(builder.build(), new WebSocketListener() {
136128

137129
@Override
138130
public void onOpen(WebSocket webSocket, Response response) {
@@ -143,7 +135,7 @@ public void onOpen(WebSocket webSocket, Response response) {
143135
}
144136

145137
@Override
146-
public void onClose(int code, String reason) {
138+
public void onClosed(WebSocket webSocket, int code, String reason) {
147139
WritableMap params = Arguments.createMap();
148140
params.putInt("id", id);
149141
params.putInt("code", code);
@@ -152,40 +144,26 @@ public void onClose(int code, String reason) {
152144
}
153145

154146
@Override
155-
public void onFailure(IOException e, Response response) {
156-
notifyWebSocketFailed(id, e.getMessage());
147+
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
148+
notifyWebSocketFailed(id, t.getMessage());
157149
}
158150

159151
@Override
160-
public void onPong(Buffer buffer) {
152+
public void onMessage(WebSocket webSocket, String text) {
153+
WritableMap params = Arguments.createMap();
154+
params.putInt("id", id);
155+
params.putString("data", text);
156+
params.putString("type", "text");
157+
sendEvent("websocketMessage", params);
161158
}
162159

163160
@Override
164-
public void onMessage(ResponseBody response) throws IOException {
165-
String message;
166-
try {
167-
if (response.contentType() == WebSocket.BINARY) {
168-
message = Base64.encodeToString(response.source().readByteArray(), Base64.NO_WRAP);
169-
} else {
170-
message = response.source().readUtf8();
171-
}
172-
} catch (IOException e) {
173-
notifyWebSocketFailed(id, e.getMessage());
174-
return;
175-
}
176-
try {
177-
response.source().close();
178-
} catch (IOException e) {
179-
FLog.e(
180-
ReactConstants.TAG,
181-
"Could not close BufferedSource for WebSocket id " + id,
182-
e);
183-
}
184-
161+
public void onMessage(WebSocket webSocket, ByteString bytes) {
162+
String text = bytes.utf8();
185163
WritableMap params = Arguments.createMap();
186164
params.putInt("id", id);
187-
params.putString("data", message);
188-
params.putString("type", response.contentType() == WebSocket.BINARY ? "binary" : "text");
165+
params.putString("data", text);
166+
params.putString("type", "binary");
189167
sendEvent("websocketMessage", params);
190168
}
191169
});
@@ -221,8 +199,8 @@ public void send(String message, int id) {
221199
throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
222200
}
223201
try {
224-
client.sendMessage(RequestBody.create(WebSocket.TEXT, message));
225-
} catch (IOException | IllegalStateException e) {
202+
client.send(message);
203+
} catch (Exception e) {
226204
notifyWebSocketFailed(id, e.getMessage());
227205
}
228206
}
@@ -235,9 +213,8 @@ public void sendBinary(String base64String, int id) {
235213
throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
236214
}
237215
try {
238-
client.sendMessage(
239-
RequestBody.create(WebSocket.BINARY, ByteString.decodeBase64(base64String)));
240-
} catch (IOException | IllegalStateException e) {
216+
client.send(ByteString.decodeBase64(base64String));
217+
} catch (Exception e) {
241218
notifyWebSocketFailed(id, e.getMessage());
242219
}
243220
}
@@ -250,9 +227,8 @@ public void ping(int id) {
250227
throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
251228
}
252229
try {
253-
Buffer buffer = new Buffer();
254-
client.sendPing(buffer);
255-
} catch (IOException | IllegalStateException e) {
230+
client.send(ByteString.EMPTY);
231+
} catch (Exception e) {
256232
notifyWebSocketFailed(id, e.getMessage());
257233
}
258234
}

0 commit comments

Comments
 (0)