Handle callbacks in Socket.io android client v1.4

I can not find any document on how to properly handle Ack and events in the latest Socket.io (v1.4.3). All existing articles / question relate to older versions, especially to the IOCallback class. But this class is missing in the latest version.

All that I have managed to find out so far is:

To get callbacks for Socket Events :

 mSocket.connect(); mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override public void call(Object... args) { //What to do here } }) 
  • How to handle (Object... args) . An example with a small code will be great.
  • It seems like more than a dozen events, should I handle it all separately? Or what is a good minimal set of events that I can implement to get connection information?

To get callbacks for individual emit events :

 mSocket.emit("payload", jsObj.toString(), new Ack() { @Override public void call(Object... args) { //TODO process ACK } }); 
  • Again, how should I handle (Object... args) ?
+8
java android callback sockets
source share
2 answers

Well. I finally figured it out myself.

How to handle (Object... args) in an EVENT_CONNECT call listen method?

I have not figured this out yet. But I look.

What is a good minimal set of events that I can implement to get connection information

These three methods would be sufficient:

connect: successful connection activated.
connect_error: about a connection error.
connect_timeout: Fired with connection timeout.

Source: Socket.io Docs

How should I process (Object... args) when confirming an issue?

So, I rummaged through the docs and found this :

Server (app.js)

 var io = require('socket.io')(80); io.on('connection', function (socket) { socket.on('ferret', function (name, fn) { fn('woot'); }); }); 

Client

 socket.on('connect', function () { // TIP: you can avoid listening on `connect` and listen on events directly too! socket.emit('ferret', 'tobi', function (data) { console.log(data); // data will be 'woot' }); }); 

So args will be what the server sent as a parameter to the callback. So this is how you would get Java client code for the above server code:

 public void call(Object... args) { String response = (String)args[0]; //this will be woot } 

The parameter can also be JSON or any of the supported data types in socket.io:

we send the string, but you can also do JSON data with the org.json package and even support binary data!

+2
source share

No, it works like this in android

payload may be from JSONOBJECT / JSONArray

 import com.github.nkzawa.socketio.client.Ack socket.emit("EVENT_NAME", payload, Ack { val ackData = it[0] Logger.e(TAG, "ackData $ackData") }) 

server side

  socket.on('EVENT_NAME', (payload, callback) => { callback("success"); }); 
0
source share

All Articles