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
  • Loading branch information
darrachequesne committed Jul 3, 2022
1 parent 9be533f commit fb531fa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 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 @@ -206,16 +206,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();
}
this.extraHeaders = opts.extraHeaders;
}
Expand All @@ -228,6 +222,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 @@ -2,7 +2,6 @@


import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -18,7 +17,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 @@ -160,7 +158,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;
this.extraHeaders = opts.extraHeaders;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.socket.parseqs.ParseQS;
import io.socket.thread.EventThread;
import io.socket.yeast.Yeast;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocketListener;
Expand Down Expand Up @@ -41,15 +40,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 fb531fa

Please sign in to comment.