Reputation: 3268
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)
?Upvotes: 9
Views: 7178
Reputation: 983
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");
});
Upvotes: 2
Reputation: 3268
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!
Upvotes: 3