I'm facing a weird problem with SocketIO, on the server-side i emit() this :
$s.sockets.emit(scope, {some: datas},
function(feedback) {
console.log('received callback');
}
);
And on the client-side I do this :
this.Socket.on(handler, function (datas, callback) {
callback('test');
}
I'm waiting for the client to send a callback('test')
the server will receive. Then it would output a simple console.log('received callback')
The client effectively receive the server request but on the client-side the callback type is null
(object) instead of function
... If I remove the function()
on the server side and don't wait for a callback the type on the client-side will be undefined
which means that when the server expect a callback the function
is somehow transmitted but transformed in an object (?)
Is it a bug ? Am I doing something wrong here ? Really weird ...
One more detail : i'm currently using SocketIO 0.9.16
Thanks people ;)
Ok I didn't find any solution on getting a callback through emit()
functionality. So I made a "trick" which is a parallel working solution :
When I emit()
something that will involve a callback()
on the server-side, I generate a key through a function and send the datas adding a { callback : key }
at the end of my datas object.
I also generate a socket.on(key, function(datas) { callback(datas); socket.removeListener(key); }
which will be an equivalent of a classic callback in the system at the end.
This will be understood by the front which will emit(key, datas...)
in response. The server will receive everything, do the stuff, and remove the listener.
Works like a charm but a bit complicated to make at first. Would've been nice if it worked directly with SocketIO ...
If someone got stuck, I hope this helps :)