socket.io 发出回调合适吗? [英] Is socket.io emit callback appropriate?

查看:21
本文介绍了socket.io 发出回调合适吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在玩socket.io,发现一个有趣的事情,我可以像这样发出回调函数.

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.

推荐答案

这是完全合法的.

那些回调被称为确认函数",并在在维基中被简要提及并在 NPM 页面(获得确认")上进行了更详细的描述.

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').

编辑:可以在此处找到更多最新文档一>.

这篇关于socket.io 发出回调合适吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆