node.js socket.io中emit方法的成功回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13549734/
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:42:51  来源:igfitidea点击:

Success Callback for emit method in socket.io

node.jssocket.io

提问by Prasanna

Im trying to emit a custom message from my client. I need to perform some actions on its success and failure. Now, how can i attach the success callback to emit method?

我试图从我的客户端发出自定义消息。我需要对其成功和失败执行一些操作。现在,我如何将成功回调附加到发出方法?

For error callback , i used Exposed eventsdoc and got it working

对于错误回调,我使用了Exposed eventsdoc 并使其正常工作

socket.on('error', () -> console.log("Error Occured"))

For success, i tried

为了成功,我试过了

socket.emit('my custom method', {content: json},() -> console.log("Emitted"))

This callback is never been triggered irrespective whether its a success or failure.

无论成功还是失败,都不会触发此回调。

How can i get hold of success handler?

我怎样才能得到成功处理程序?

回答by user3363398

If you look at the docs, it shows you an example of passing a call back function -2nd last example: http://socket.io/docs/#Sending-and-getting-data-acknowledgements

如果您查看文档,它会向您展示一个传递回调函数的示例-最后一个示例:http: //socket.io/docs/#Sending-and-getting-data-acknowledgements

Ex server:

前服务器:

    socket.on('formData', 
              function(data, fn){
                      // data is your form data from the client side
                      // we are here so we got it successfully so call client callback
                      // incidentally(not needed in this case) send back data value true 
                      fn(true);
              }
             );

client:

客户:

      socket.emit('formData', 
                  data, 
                  function(confirmation){
                          // send data
                          // know we got it once the server calls this callback      
                          // note -in this ex we dont need to send back any data 
                          // - could just have called fn() at server side
                          console.log(confirmation);
                  }
                 );

回答by toxicate20

The reason why your second code is not doing anything is because exposed events in socketIO are just defined for socket.onmethods. Therefore you need to add another emit in your server app.js to accomplish this

你的第二个代码没有做任何事情的原因是因为 socketIO 中暴露的事件只是为socket.on方法定义的。因此,您需要在服务器 app.js 中添加另一个发射来完成此操作

Clientemits the custom message and sends JSON data to the socket via socket.emit, also he gets an update function that handles the success callback

客户端发出自定义消息并通过 socket.emit 将 JSON 数据发送到套接字,同时他还获得一个处理成功回调的更新函数

socket.emit ('message', {hello: 'world'});
socket.on ('messageSuccess', function (data) {
 //do stuff here
});

Server-sideGets a call from the message emit from the client and emits the messageSuccess back to the client

服务端从客户端发出的消息中获取调用,并将消息成功发送回客户端

socket.on ('message', function (data) {
 io.sockets.emit ('messageSuccess', data);
});

You could probably make a module out of this behavior so you can attach this for every message that you want to be handled that way.

您可能可以根据这种行为制作一个模块,以便您可以将其附加到您希望以这种方式处理的每条消息中。