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:

- https://github.com/socketio/socket.io-client-java/issues/491
- https://github.com/socketio/socket.io-client-java/issues/660
This commit is contained in:
Damien Arrachequesne
2022-07-03 22:35:00 +02:00
parent 9be533fdf3
commit fb531fab30
3 changed files with 13 additions and 14 deletions

View File

@@ -206,16 +206,10 @@ public class Socket extends Emitter {
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;
}
@@ -228,6 +222,15 @@ public class Socket extends Emitter {
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.
*

View File

@@ -2,7 +2,6 @@ package io.socket.engineio.client.transports;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@@ -18,7 +17,6 @@ import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
@@ -160,7 +158,7 @@ public class PollingXHR extends Polling {
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;
}

View File

@@ -7,7 +7,6 @@ import io.socket.engineio.parser.Parser;
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;
@@ -41,7 +40,6 @@ public class WebSocket extends Transport {
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()) {
@@ -49,7 +47,7 @@ public class WebSocket extends Transport {
}
}
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();