Skip to content

Commit b6db559

Browse files
committed
Report any socket.io usage exceptions asynchronously.
1 parent 80c4b1d commit b6db559

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.koushikdutta.async.http.socketio;
2+
3+
/**
4+
* Created by koush on 8/1/13.
5+
*/
6+
public interface ExceptionCallback {
7+
public void onException(Exception e);
8+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.text.TextUtils;
66

77
import com.koushikdutta.async.AsyncServer;
8+
import com.koushikdutta.async.callback.CompletedCallback;
89
import com.koushikdutta.async.future.Future;
910
import com.koushikdutta.async.future.SimpleFuture;
1011
import com.koushikdutta.async.http.AsyncHttpClient;
@@ -110,6 +111,15 @@ public void onConnectCompleted(Exception ex, SocketIOClient client) {
110111
return ret;
111112
}
112113

114+
ExceptionCallback exceptionCallback;
115+
public void setExceptionCallback(ExceptionCallback exceptionCallback) {
116+
this.exceptionCallback = exceptionCallback;
117+
}
118+
119+
public ExceptionCallback getExceptionCallback() {
120+
return exceptionCallback;
121+
}
122+
113123
ErrorCallback errorCallback;
114124
public ErrorCallback getErrorCallback() {
115125
return errorCallback;

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public void onSelect(SocketIOClient client) {
302302
});
303303
}
304304

305-
private Acknowledge acknowledge(final String _messageId) {
305+
private Acknowledge acknowledge(final String _messageId, final String endpoint) {
306306
if (TextUtils.isEmpty(_messageId))
307307
return null;
308308

@@ -314,6 +314,19 @@ public void acknowledge(JSONArray arguments) {
314314
String data = "";
315315
if (arguments != null)
316316
data += "+" + arguments.toString();
317+
WebSocket webSocket = SocketIOConnection.this.webSocket;
318+
if (webSocket == null) {
319+
final Exception e = new SocketIOException("websocket is not connected");
320+
select(endpoint, new SelectCallback() {
321+
@Override
322+
public void onSelect(SocketIOClient client) {
323+
ExceptionCallback callback = client.exceptionCallback;
324+
if (callback != null)
325+
callback.onException(e);
326+
}
327+
});
328+
return;
329+
}
317330
webSocket.send(String.format("6:::%s%s", messageId, data));
318331
}
319332
};
@@ -354,22 +367,22 @@ public void onStringAvailable(String message) {
354367
break;
355368
case 3: {
356369
// message
357-
reportString(parts[2], parts[3], acknowledge(parts[1]));
370+
reportString(parts[2], parts[3], acknowledge(parts[1], parts[2]));
358371
break;
359372
}
360373
case 4: {
361374
//json message
362375
final String dataString = parts[3];
363376
final JSONObject jsonMessage = new JSONObject(dataString);
364-
reportJson(parts[2], jsonMessage, acknowledge(parts[1]));
377+
reportJson(parts[2], jsonMessage, acknowledge(parts[1], parts[2]));
365378
break;
366379
}
367380
case 5: {
368381
final String dataString = parts[3];
369382
final JSONObject data = new JSONObject(dataString);
370383
final String event = data.getString("name");
371384
final JSONArray args = data.optJSONArray("args");
372-
reportEvent(parts[2], event, args, acknowledge(parts[1]));
385+
reportEvent(parts[2], event, args, acknowledge(parts[1], parts[2]));
373386
break;
374387
}
375388
case 6:
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.koushikdutta.async.http.socketio;
2+
3+
/**
4+
* Created by koush on 8/1/13.
5+
*/
6+
public class SocketIOException extends Exception {
7+
public SocketIOException(String error) {
8+
super(error);
9+
}
10+
}

0 commit comments

Comments
 (0)