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

查看:130
本文介绍了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.

推荐答案

这是完全合法的.

这些回调称为确认功能",并在Wiki中中被简要提及并在 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').

编辑:可以在此处找到最新的文档.

EDIT: more recent documentation can be found here.

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

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