Node.JS:如何在全局函数中转换嵌套回调以便以后使用?

时间:2013-08-16 17:53:36

标签: javascript node.js callback scope

我有这个tls客户端,在这个客户端内我有一个服务器,它提供从ServerX到PHP客户端的数据......疯了,胡?好的..一切都工作得非常快......但是我想尽快回答PHP中的问题,但是它不会同步。请阅读:

var ServerX = tls.connect(port,host,options,function() {
    console.log(' Logging to ServerX...');
    var phpServer = dnode({
        fromPHP: function (variables,correlationalCallbackId,callBackToPHP) { // Shared PHP/NODE function
            othermodule.serialize(variable, function(buffer){ 
    HOW TO MAKE THIS-> global.phpCallBacks[correlationalCallbackId] = callBackToPHP; //I want to stores CallbackToPHP to use later when response from ServerX arrive
                ServerX.write(buffer,function(){ // write buffer to ServerX
                    //console.log("buffer sent");
                                    //callBackToPHP("Response from ServerX");//<--It works, but response dont exists
                });
                global.phpCallBacks[correlationalId]('Hi PHP! :)'); //Dont work even here
            });
        }// toNode END
    });
    //Starts DNODE server to integrate with PHP scripts
    phpServer.listen(PHPServerPort);    

});

我希望在收到带有相关ID的特定响应时调用callbackToPHP

ServerX.on('data', function(data) {
    othemodule.decode(data,function(message){ // message is an object with messageCorrelationId and other informations
        if(global.phpCallBacks[message.messageCorrelationId]) {
HOW TO MAKE IT WORK HERE-> global.phpCallBacks[message.messageCorrelationId](message.informations); // never fire :((
        }
    });
});

希望它能得到很好的解释......如果使用数百万个范围嵌套,如何使CallBack功能在全球范围内工作?!?!?

0 个答案:

没有答案