node.js socket.io 发出回调合适吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20337832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:24:51  来源:igfitidea点击:

Is socket.io emit callback appropriate?

node.jscallbacksocket.ioemit

提问by antanas_sepikas

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

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

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.

一切正常,没有错误,但我很感兴趣,如果这样做是合适的,因为到目前为止我还没有在文档或任何示例中看到任何类似的内容。

采纳答案by robertklep

It's perfectly legal.

这是完全合法的。

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

这些回调称为“确认函数”,在 Wiki进行了简要介绍,并在NPM 页面(“获取确认”)上进行了更详细的描述。

EDIT: more recent documentation can be found here.

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