socket.io + using 'emit' with a callback?

时间:2022-03-19 19:43:11

I'm having an issue with my sockets where my server side code is executing before my socket.emit finishes. Here's a snippet:

我的套接字有问题,我的服务器端代码在我的socket.emit完成之前执行。这是一个片段:

admin.getTh(function(th){
   socket.emit('GPS', {gpsResults: data, thresholds: th, PEMSID: PEMSID, count: count});
   count++;
   toolbox.drawPaths(function(ta){
        console.log(ta);
   });
});

So what I need to have happen is the code that gets executed on the client via socket.emit('GPS', ...) to completely finish before toolbox.drawPaths happens. I tried to toss a callback on the emit like you would socket.on(..., function(){...}) but that doesn't seem to be part of the API. Any suggestions?

所以我需要发生的是通过socket.emit('GPS',...)在客户端上执行的代码,以便在toolbox.drawPaths发生之前完全完成。我试图像对socket.on(...,function(){...})那样在emit上抛回一个回调,但这似乎不是API的一部分。有什么建议么?

1 个解决方案

#1


4  

One thing you might want to do is create an event on the server that gets emitted when your GPS is completed.

您可能想要做的一件事是在服务器上创建一个在GPS完成时发出的事件。

For example:

例如:

Server

服务器

socket.on ('GPS', function (data) {
  // Do work

  // Done doing work
  socket.emit ('messageSuccess', data);
});

Client

客户

socket.emit('GPS', {gpsResults: data, thresholds: th, PEMSID: PEMSID, count: count});
socket.on ('messageSuccess', function (data) {
  //do stuff here
  toolbox.drawPaths(function(ta){
    console.log(ta);
  });
});

#1


4  

One thing you might want to do is create an event on the server that gets emitted when your GPS is completed.

您可能想要做的一件事是在服务器上创建一个在GPS完成时发出的事件。

For example:

例如:

Server

服务器

socket.on ('GPS', function (data) {
  // Do work

  // Done doing work
  socket.emit ('messageSuccess', data);
});

Client

客户

socket.emit('GPS', {gpsResults: data, thresholds: th, PEMSID: PEMSID, count: count});
socket.on ('messageSuccess', function (data) {
  //do stuff here
  toolbox.drawPaths(function(ta){
    console.log(ta);
  });
});