aboutsummaryrefslogtreecommitdiffstats
path: root/AndroidAsync
diff options
context:
space:
mode:
authorJustin Huntington <justinhuntington@gmail.com>2014-10-23 19:09:14 -0400
committerJustin Huntington <justinhuntington@gmail.com>2014-10-24 18:43:01 -0400
commitbf781c7cfea6e01b659425ec97a1c97aae5c6470 (patch)
tree2ecc139b9c40ddce7f48baa934991ea9743fa8b0 /AndroidAsync
parent3f03c7aa3d071a2276c5f97cb5fdea734cdc0538 (diff)
downloadAndroidAsync-bf781c7cfea6e01b659425ec97a1c97aae5c6470.tar.gz
AndroidAsync-bf781c7cfea6e01b659425ec97a1c97aae5c6470.tar.bz2
AndroidAsync-bf781c7cfea6e01b659425ec97a1c97aae5c6470.zip
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).
Diffstat (limited to 'AndroidAsync')
-rw-r--r--AndroidAsync/src/com/koushikdutta/async/http/socketio/SocketIOConnection.java11
-rw-r--r--AndroidAsync/src/com/koushikdutta/async/http/socketio/SocketIORequest.java35
2 files changed, 42 insertions, 4 deletions
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<SocketIOClient> clients = new ArrayList<SocketIOClient>();
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;
+ }
+ }
}