From bf781c7cfea6e01b659425ec97a1c97aae5c6470 Mon Sep 17 00:00:00 2001 From: Justin Huntington Date: Thu, 23 Oct 2014 19:09:14 -0400 Subject: fix #181 configurable reconnectDelay and reconnectDelayMax for socket.io Add class SocketIORequest.Config whose instances may optionally be passed to SocketIORequest's constructor to configure reconnectDelay (initial delay) and reconnectDelayMax. The delay will double on each attempt as before, but never exceed reconnectDelayMax, if specified. If reconnectDelayMax is 0 (the default), there is no max and reconnect delay will grow forever (as it did before this change). --- .../async/http/socketio/SocketIOConnection.java | 11 +++++-- .../async/http/socketio/SocketIORequest.java | 35 +++++++++++++++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) (limited to 'AndroidAsync') diff --git a/AndroidAsync/src/com/koushikdutta/async/http/socketio/SocketIOConnection.java b/AndroidAsync/src/com/koushikdutta/async/http/socketio/SocketIOConnection.java index 9b03541..c8381cb 100644 --- a/AndroidAsync/src/com/koushikdutta/async/http/socketio/SocketIOConnection.java +++ b/AndroidAsync/src/com/koushikdutta/async/http/socketio/SocketIOConnection.java @@ -30,6 +30,7 @@ import java.util.Hashtable; class SocketIOConnection { AsyncHttpClient httpClient; int heartbeat; + long reconnectDelay; ArrayList clients = new ArrayList(); SocketIOTransport transport; SocketIORequest request; @@ -37,6 +38,7 @@ class SocketIOConnection { public SocketIOConnection(AsyncHttpClient httpClient, SocketIORequest request) { this.httpClient = httpClient; this.request = request; + this.reconnectDelay = this.request.config.reconnectDelay; } public boolean isConnected() { @@ -157,7 +159,7 @@ class SocketIOConnection { return; } - reconnectDelay = 1000L; + reconnectDelay = request.config.reconnectDelay; SocketIOConnection.this.transport = result; attach(); } @@ -216,10 +218,13 @@ class SocketIOConnection { reconnect(null); } }, reconnectDelay); - reconnectDelay *= 2; + + reconnectDelay = reconnectDelay * 2; + if (request.config.reconnectDelayMax > 0L) { + reconnectDelay = Math.min(reconnectDelay, request.config.reconnectDelayMax); + } } - long reconnectDelay = 1000L; private void reportDisconnect(final Exception ex) { if (ex != null) { request.loge("socket.io disconnected", ex); diff --git a/AndroidAsync/src/com/koushikdutta/async/http/socketio/SocketIORequest.java b/AndroidAsync/src/com/koushikdutta/async/http/socketio/SocketIORequest.java index 49ff2da..f98407f 100644 --- a/AndroidAsync/src/com/koushikdutta/async/http/socketio/SocketIORequest.java +++ b/AndroidAsync/src/com/koushikdutta/async/http/socketio/SocketIORequest.java @@ -1,7 +1,6 @@ package com.koushikdutta.async.http.socketio; import android.net.Uri; -import android.text.TextUtils; import com.koushikdutta.async.http.AsyncHttpPost; @@ -10,6 +9,11 @@ public class SocketIORequest extends AsyncHttpPost { this(uri, ""); } + Config config; + public Config getConfig() { + return config; + } + String endpoint; public String getEndpoint() { return endpoint; @@ -25,8 +29,37 @@ public class SocketIORequest extends AsyncHttpPost { } public SocketIORequest(String uri, String endpoint, String query) { + this(uri, endpoint, query, null); + } + + public SocketIORequest(String uri, String endpoint, String query, Config config) { super(Uri.parse(uri + (query == null ? "" : "?" + query)).buildUpon().encodedPath("/socket.io/1/").build().toString()); + this.config = (config != null) ? config : new Config(); this.endpoint = endpoint; this.query = query; } + + public static class Config { + long reconnectDelay = 1000L; + public void setReconnectDelay(long reconnectDelay) { + if (reconnectDelay < 0L) { + throw new IllegalArgumentException("reconnectDelay must be >= 0"); + } + this.reconnectDelay = reconnectDelay; + } + public long getReconnectDelay() { + return reconnectDelay; + } + + long reconnectDelayMax = 0L; + public void setReconnectDelayMax(long reconnectDelayMax) { + if (reconnectDelay < 0L) { + throw new IllegalArgumentException("reconnectDelayMax must be >= 0"); + } + this.reconnectDelayMax = reconnectDelayMax; + } + public long getReconnectDelayMax() { + return reconnectDelayMax; + } + } } -- cgit v1.2.3