简体   繁体   English

套接字io内部的回调

[英]callback inside of socket io

I am having a problem with socket when the executing data takes a long time. 当执行数据需要很长时间时,我遇到套接字问题。 For example: 例如:

socket.on('start', function(input){
    var output = foo(input); //taking a very long time to get data, networking etc.
    console.log("this is ending", output);
    socket.emit('end',output);
});

It seems that if it takes a long time for foo(input) to execute, nodejs would actually emit output first while it is still null. 似乎如果执行foo(输入)需要很长时间,nodejs实际上会在它仍为空时首先发出输出。

How can I make sure to finish executing first before emit? 如何在发射之前确保先完成执行?

I modified like this 我修改过这样的

    socket.on('several',function(meterIDArray){
    console.log("I have receiever meterIDArray",meterIDArray);
    meterIDs = meterIDArray.meterIDs;
    foo(meterIDs,function(Datasets){
        console.log("This is datasets",Datasets);
        socket.emit('Datasets',Datasets);
    });

});

And I modified my foo like this 我像这样修改了我的foo

function foo(meterIDs,callback){
//this is what I meant by a long call. 
var Datasets = [];
    for (var i = meterIDs.length - 1; i >= 0; i--) {
        datapoint.setMeterID(meterIDs[i],function(err, results){
            datapoint.doRequest(null, function(err, results){
                var dataAfter = [];
                var step = 1;
                for(var i = 0; i < results.timeseries.length; i = i + step) {
                    var item = results.timeseries[i];
                    for(date in item) {
                        dataAfter.push({x: date, y: item[date]})
                    }
                }
                console.log("This is data After",dataAfter);
                Datasets.push(dataAfter);
            });
        });
    }
callback(Datasets);
}

But it is still the same effect: socket is still emitting null. 但它仍然是同样的效果:socket仍然是空的。 Sorry if it is a naive bug. 对不起,如果这是一个天真的bug。 Thanks! 谢谢!

If you can modify foo all you need is this: 如果你可以修改foo你只需要:

socket.on('start', function(input){
  foo(input, function (output) {
    console.log("this is ending", output)
    socket.emit('end', output)
  ))
})

You need to fire the callback when your server request returns. 您需要在服务器请求返回时触发回调。

UPDATE: Can't use the for loop i to keep track of how many requests have been made, have to keep track once the request returns. 更新:不能使用for循环i来跟踪已经发出了多少请求,必须在请求返回后跟踪。

function foo(meterIDs, callback) {
  var Datasets = [];
  var numberOfRequestsToMake = meterIDs.length;
  for (var i = meterIDs.length - 1; i >= 0; i--) {
    datapoint.setMeterID(meterIDs[i],function(err, results){
      datapoint.doRequest(null, function(err, results){
        var dataAfter = [];
        var step = 1;
        for(var i = 0; i < results.timeseries.length; i = i + step) {
          var item = results.timeseries[i];
          for(var date in item) {
            dataAfter.push({x: date, y: item[date]})
          }
        }
        console.log("This is data After",dataAfter);
        Datasets.push(dataAfter);

        numberOfRequestsToMake--;

        // if this is the final return from the server
        if (numberOfRequestsToMake === 0) {
          callback(Datasets)
        }
      });
    });
  }
}

Promises are awesome though 承诺很棒但是很棒

You can redefine your foo function by adding a callback to guaranty that the output will be emited after executing what foo is doing. 您可以通过添加回调来重新定义foo函数,以保证在执行foo执行操作后输出将被激活。

For example 例如

    socket.on('start', function(input){
        foo(input, function(output){
            // This code will be executed after foo performs the main job
            console.log("this is ending", output);
            socket.emit('end',output);
        }); //taking a very long time to get data, networking etc.
    });

Learn more about callbacks 了解有关回调的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM