Is socket.io emit callback appropriate?

試著忘記壹切 提交于 2019-12-03 03:01:12

问题


Recently I have been messing around with socket.io and found this interesting thing, that I can have emit function callback like this.

I start emitting on client side like this:

client.emit('eventToEmit', dataToEmit, function(error, message){
    console.log(error);
    console.log(message);
});

Then I can fire a callback from server-side like this:

client.on('eventToEmit', function(data, callback){
    console.log(data);
    callback('error', 'message');
});

Everything works fine with no errors, but I am interested if doing something like this is appropriate since I have not seen anything similar in the documentation or any example so far.


回答1:


It's perfectly legal.

Those callbacks are called 'acknowledgement functions' and are summarily mentioned in the Wiki and described a bit more in detail on the NPM page ('Getting acknowledgements').

EDIT: more recent documentation can be found here.



来源:https://stackoverflow.com/questions/20337832/is-socket-io-emit-callback-appropriate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!