I am not able to find any doc on how to properly handle Ack's and Events in the latest Socket.io
(v1.4.3). All existing articles/question refer to older versions, especially the IOCallback
class. But that class is not present on the latest version.
All I managed to find out till now is this :
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
}
})
(Object... args)
. A little code example would be great.To get callbacks for individual emit
events :
mSocket.emit("payload", jsObj.toString(), new Ack() {
@Override
public void call(Object... args) {
//TODO process ACK
}
});
(Object... args)
?Well. I finally figured that out on my own.
How do I handle the (Object... args)
on the EVENT_CONNECT listener's call
method?
I haven't yet figured that out. But I'm lookin.
What is a good minimum set of events that I can implement to be informed about the connection
These three methods would be sufficient :
connect : Fired upon a successful connection.
connect_error : Fired upon a connection error.
connect_timeout : Fired upon a connection timeout.
Source : Socket.io Docs
how am I supposed to process the (Object... args)
on an emit acknowledgement?
So I was digging 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 the args will be whatever the server sent as parameter into the callback. So this is how you would write Java
client code for the above server code :
public void call(Object... args) {
String response = (String)args[0]; //this will be woot
}
The param can also be JSON, or any of the supported datatypes in socket.io :
we send a string but you can do JSON data too with the org.json package, and even binary data is supported as well!
No In Android its works like this
payload can be of 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");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With