Skip to content

Commit fb531fa

Browse files
fix: increase the readTimeout value of the default OkHttpClient
With the previous value (10 seconds by default), a connection established with HTTP long-polling was closed if the server did not send any packet for 10 seconds (the HTTP request would timeout from the client side). It will now default to 1 minute, which is above the `pingInterval + pingTimeout` value from the server. Note: the connectTimeout and writeTimeout options are left as is (10 seconds), but you may need to increase them depending on your use case: ``` OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(20, TimeUnit.SECONDS) .readTimeout(1, TimeUnit.MINUTES) .writeTimeout(1, TimeUnit.MINUTES) .build(); ``` Related: - socketio/socket.io-client-java#491 - socketio/socket.io-client-java#660
1 parent 9be533f commit fb531fa

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

Diff for: src/main/java/io/socket/engineio/client/Socket.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,10 @@ public Socket(Options opts) {
206206
this.callFactory = opts.callFactory != null ? opts.callFactory : defaultCallFactory;
207207
this.webSocketFactory = opts.webSocketFactory != null ? opts.webSocketFactory : defaultWebSocketFactory;
208208
if (callFactory == null) {
209-
if (defaultOkHttpClient == null) {
210-
defaultOkHttpClient = new OkHttpClient();
211-
}
212-
callFactory = defaultOkHttpClient;
209+
callFactory = getDefaultOkHttpClient();
213210
}
214211
if (webSocketFactory == null) {
215-
if (defaultOkHttpClient == null) {
216-
defaultOkHttpClient = new OkHttpClient();
217-
}
218-
webSocketFactory = defaultOkHttpClient;
212+
webSocketFactory = getDefaultOkHttpClient();
219213
}
220214
this.extraHeaders = opts.extraHeaders;
221215
}
@@ -228,6 +222,15 @@ public static void setDefaultOkHttpCallFactory(okhttp3.Call.Factory factory) {
228222
defaultCallFactory = factory;
229223
}
230224

225+
private static OkHttpClient getDefaultOkHttpClient() {
226+
if (defaultOkHttpClient == null) {
227+
defaultOkHttpClient = new OkHttpClient.Builder()
228+
.readTimeout(1, TimeUnit.MINUTES) // defaults to 10 seconds
229+
.build();
230+
}
231+
return defaultOkHttpClient;
232+
}
233+
231234
/**
232235
* Connects the client.
233236
*

Diff for: src/main/java/io/socket/engineio/client/transports/PollingXHR.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44
import java.io.IOException;
5-
import java.util.Arrays;
65
import java.util.Collections;
76
import java.util.LinkedList;
87
import java.util.List;
@@ -18,7 +17,6 @@
1817
import okhttp3.Callback;
1918
import okhttp3.HttpUrl;
2019
import okhttp3.MediaType;
21-
import okhttp3.OkHttpClient;
2220
import okhttp3.RequestBody;
2321
import okhttp3.Response;
2422
import okhttp3.ResponseBody;
@@ -160,7 +158,7 @@ public Request(Options opts) {
160158
this.method = opts.method != null ? opts.method : "GET";
161159
this.uri = opts.uri;
162160
this.data = opts.data;
163-
this.callFactory = opts.callFactory != null ? opts.callFactory : new OkHttpClient();
161+
this.callFactory = opts.callFactory;
164162
this.extraHeaders = opts.extraHeaders;
165163
}
166164

Diff for: src/main/java/io/socket/engineio/client/transports/WebSocket.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import io.socket.parseqs.ParseQS;
88
import io.socket.thread.EventThread;
99
import io.socket.yeast.Yeast;
10-
import okhttp3.OkHttpClient;
1110
import okhttp3.Request;
1211
import okhttp3.Response;
1312
import okhttp3.WebSocketListener;
@@ -41,15 +40,14 @@ protected void doOpen() {
4140
this.emit(EVENT_REQUEST_HEADERS, headers);
4241

4342
final WebSocket self = this;
44-
okhttp3.WebSocket.Factory factory = webSocketFactory != null ? webSocketFactory : new OkHttpClient();
4543
Request.Builder builder = new Request.Builder().url(uri());
4644
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
4745
for (String v : entry.getValue()) {
4846
builder.addHeader(entry.getKey(), v);
4947
}
5048
}
5149
final Request request = builder.build();
52-
ws = factory.newWebSocket(request, new WebSocketListener() {
50+
ws = webSocketFactory.newWebSocket(request, new WebSocketListener() {
5351
@Override
5452
public void onOpen(okhttp3.WebSocket webSocket, Response response) {
5553
final Map<String, List<String>> headers = response.headers().toMultimap();

0 commit comments

Comments
 (0)