Skip to content

Commit bf781c7

Browse files
committed
fix koush#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).
1 parent 3f03c7a commit bf781c7

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

AndroidAsync/src/com/koushikdutta/async/http/socketio/SocketIOConnection.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
class SocketIOConnection {
3131
AsyncHttpClient httpClient;
3232
int heartbeat;
33+
long reconnectDelay;
3334
ArrayList<SocketIOClient> clients = new ArrayList<SocketIOClient>();
3435
SocketIOTransport transport;
3536
SocketIORequest request;
3637

3738
public SocketIOConnection(AsyncHttpClient httpClient, SocketIORequest request) {
3839
this.httpClient = httpClient;
3940
this.request = request;
41+
this.reconnectDelay = this.request.config.reconnectDelay;
4042
}
4143

4244
public boolean isConnected() {
@@ -157,7 +159,7 @@ public void onCompleted(Exception e, SocketIOTransport result) {
157159
return;
158160
}
159161

160-
reconnectDelay = 1000L;
162+
reconnectDelay = request.config.reconnectDelay;
161163
SocketIOConnection.this.transport = result;
162164
attach();
163165
}
@@ -216,10 +218,13 @@ public void run() {
216218
reconnect(null);
217219
}
218220
}, reconnectDelay);
219-
reconnectDelay *= 2;
221+
222+
reconnectDelay = reconnectDelay * 2;
223+
if (request.config.reconnectDelayMax > 0L) {
224+
reconnectDelay = Math.min(reconnectDelay, request.config.reconnectDelayMax);
225+
}
220226
}
221227

222-
long reconnectDelay = 1000L;
223228
private void reportDisconnect(final Exception ex) {
224229
if (ex != null) {
225230
request.loge("socket.io disconnected", ex);

AndroidAsync/src/com/koushikdutta/async/http/socketio/SocketIORequest.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.koushikdutta.async.http.socketio;
22

33
import android.net.Uri;
4-
import android.text.TextUtils;
54

65
import com.koushikdutta.async.http.AsyncHttpPost;
76

@@ -10,6 +9,11 @@ public SocketIORequest(String uri) {
109
this(uri, "");
1110
}
1211

12+
Config config;
13+
public Config getConfig() {
14+
return config;
15+
}
16+
1317
String endpoint;
1418
public String getEndpoint() {
1519
return endpoint;
@@ -25,8 +29,37 @@ public SocketIORequest(String uri, String endpoint) {
2529
}
2630

2731
public SocketIORequest(String uri, String endpoint, String query) {
32+
this(uri, endpoint, query, null);
33+
}
34+
35+
public SocketIORequest(String uri, String endpoint, String query, Config config) {
2836
super(Uri.parse(uri + (query == null ? "" : "?" + query)).buildUpon().encodedPath("/socket.io/1/").build().toString());
37+
this.config = (config != null) ? config : new Config();
2938
this.endpoint = endpoint;
3039
this.query = query;
3140
}
41+
42+
public static class Config {
43+
long reconnectDelay = 1000L;
44+
public void setReconnectDelay(long reconnectDelay) {
45+
if (reconnectDelay < 0L) {
46+
throw new IllegalArgumentException("reconnectDelay must be >= 0");
47+
}
48+
this.reconnectDelay = reconnectDelay;
49+
}
50+
public long getReconnectDelay() {
51+
return reconnectDelay;
52+
}
53+
54+
long reconnectDelayMax = 0L;
55+
public void setReconnectDelayMax(long reconnectDelayMax) {
56+
if (reconnectDelay < 0L) {
57+
throw new IllegalArgumentException("reconnectDelayMax must be >= 0");
58+
}
59+
this.reconnectDelayMax = reconnectDelayMax;
60+
}
61+
public long getReconnectDelayMax() {
62+
return reconnectDelayMax;
63+
}
64+
}
3265
}

0 commit comments

Comments
 (0)