Normally, when we use HTTP requests, we have a specific set of methods/callbacks that would be called when the request succeeds/fails. Each connection request could have its own callback methods, which made structuring of code very easy.
So now that I am trying out Socket.io for a new project, I got all confused. For instance, you emit a message into the socket connection and you're done. no callbacks there. There is no easy way I can know if that actually succeeded or not I've managed to simulate a callback by writing this method:
public void emitRequest(final String event, JSONObject data, final emitResponseListener pListener)
{
mSocket.on(event+"_resp", new Listener() {
@Override
public void call(final Object... args) {
((Activity)c).runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
if (pListener!=null)
{
pListener.onResponseRecieved(data);
}
}
});
}
});
mSocket.emit(event,data);
}
This method listens for a event
_resp message from the server after emitting an event
. But thats as far as I've got. I have no clue as to how to handle errors for specific emits, so as to update the UI or inform the user .etc.
Sure, there are callback events for the Socket
but those are impossible to wire up into the apps flow.
So does anybody know a way to achieve this? I searched a lot but couldn't find anything. I'm using Java Client but generalized answers for any client API are welcome.