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

时间:2022-06-08 18:51:38

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?

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

For error callback , i used Exposed events doc and got it working

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

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?

我怎样才能掌握成功处理程序?

2 个解决方案

#1


58  

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);
                  }
                 );

#2


15  

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

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

Client emits 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-side Gets a call from the message emit from the client and emits the messageSuccess back to the client

服务器端从客户端获取来自消息emit的调用,并将messageSuccess发送回客户端

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.

您可能可以使模块不受此行为的影响,因此您可以为希望以这种方式处理的每条消息附加此模块。

#1


58  

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);
                  }
                 );

#2


15  

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

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

Client emits 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-side Gets a call from the message emit from the client and emits the messageSuccess back to the client

服务器端从客户端获取来自消息emit的调用,并将messageSuccess发送回客户端

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.

您可能可以使模块不受此行为的影响,因此您可以为希望以这种方式处理的每条消息附加此模块。

相关文章