Skip to content

Commit

Permalink
fix: increase the readTimeout value of the default OkHttpClient
Browse files Browse the repository at this point in the history
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

Backported from fb531fa
  • Loading branch information
darrachequesne committed Jul 10, 2022
1 parent 319f2e2 commit 2d87497
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
19 changes: 11 additions & 8 deletions src/main/java/io/socket/engineio/client/Socket.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,10 @@ public Socket(Options opts) {
this.callFactory = opts.callFactory != null ? opts.callFactory : defaultCallFactory;
this.webSocketFactory = opts.webSocketFactory != null ? opts.webSocketFactory : defaultWebSocketFactory;
if (callFactory == null) {
if (defaultOkHttpClient == null) {
defaultOkHttpClient = new OkHttpClient();
}
callFactory = defaultOkHttpClient;
callFactory = getDefaultOkHttpClient();
}
if (webSocketFactory == null) {
if (defaultOkHttpClient == null) {
defaultOkHttpClient = new OkHttpClient();
}
webSocketFactory = defaultOkHttpClient;
webSocketFactory = getDefaultOkHttpClient();
}
}

Expand All @@ -232,6 +226,15 @@ public static void setDefaultOkHttpCallFactory(okhttp3.Call.Factory factory) {
defaultCallFactory = factory;
}

private static OkHttpClient getDefaultOkHttpClient() {
if (defaultOkHttpClient == null) {
defaultOkHttpClient = new OkHttpClient.Builder()
.readTimeout(1, TimeUnit.MINUTES) // defaults to 10 seconds
.build();
}
return defaultOkHttpClient;
}

/**
* Connects the client.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
Expand Down Expand Up @@ -172,7 +171,7 @@ public Request(Options opts) {
this.method = opts.method != null ? opts.method : "GET";
this.uri = opts.uri;
this.data = opts.data;
this.callFactory = opts.callFactory != null ? opts.callFactory : new OkHttpClient();
this.callFactory = opts.callFactory;
}

public void create() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import io.socket.thread.EventThread;
import io.socket.utf8.UTF8Exception;
import io.socket.yeast.Yeast;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocketListener;
Expand All @@ -39,15 +38,14 @@ protected void doOpen() {
this.emit(EVENT_REQUEST_HEADERS, headers);

final WebSocket self = this;
okhttp3.WebSocket.Factory factory = webSocketFactory != null ? webSocketFactory : new OkHttpClient();
Request.Builder builder = new Request.Builder().url(uri());
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
for (String v : entry.getValue()) {
builder.addHeader(entry.getKey(), v);
}
}
final Request request = builder.build();
ws = factory.newWebSocket(request, new WebSocketListener() {
ws = webSocketFactory.newWebSocket(request, new WebSocketListener() {
@Override
public void onOpen(okhttp3.WebSocket webSocket, Response response) {
final Map<String, List<String>> headers = response.headers().toMultimap();
Expand Down

0 comments on commit 2d87497

Please sign in to comment.