From 2d874971c2428a7a444b3a33afe66aedcdce3a96 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Sun, 3 Jul 2022 22:35:00 +0200 Subject: [PATCH] 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 Backported from https://github.com/socketio/engine.io-client-java/commit/fb531fab30968a4b65a402c81f37e92dd5671f33 --- .../io/socket/engineio/client/Socket.java | 19 +++++++++++-------- .../client/transports/PollingXHR.java | 3 +-- .../engineio/client/transports/WebSocket.java | 4 +--- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/socket/engineio/client/Socket.java b/src/main/java/io/socket/engineio/client/Socket.java index 3f61f9ad..aa3aea95 100644 --- a/src/main/java/io/socket/engineio/client/Socket.java +++ b/src/main/java/io/socket/engineio/client/Socket.java @@ -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(); } } @@ -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. * diff --git a/src/main/java/io/socket/engineio/client/transports/PollingXHR.java b/src/main/java/io/socket/engineio/client/transports/PollingXHR.java index 827c24f6..82bcd9f7 100644 --- a/src/main/java/io/socket/engineio/client/transports/PollingXHR.java +++ b/src/main/java/io/socket/engineio/client/transports/PollingXHR.java @@ -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; @@ -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() { diff --git a/src/main/java/io/socket/engineio/client/transports/WebSocket.java b/src/main/java/io/socket/engineio/client/transports/WebSocket.java index d6e2f1a3..7f9654dc 100644 --- a/src/main/java/io/socket/engineio/client/transports/WebSocket.java +++ b/src/main/java/io/socket/engineio/client/transports/WebSocket.java @@ -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; @@ -39,7 +38,6 @@ 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> entry : headers.entrySet()) { for (String v : entry.getValue()) { @@ -47,7 +45,7 @@ protected void doOpen() { } } 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> headers = response.headers().toMultimap();