signalr异步响应javascript回调

时间:2017-08-22 14:01:49

标签: javascript jquery signalr.client

请你告诉我最好的方法将我的响应从一个signalR集线器传递给一个有回调的函数调用..我已经尝试了一个全局变量来存储回调并在接收数据时调用它但没有运气。这就是我喜欢做的事情;

school.lib.setmytime = function(_time){

 console.log('mytime is :' + _time);
}

school.lib.gettime = data.getservertime(school.lib.setmytime);
var myhub;

data.connection = function(){
    $connection.hub.url = 'http://myserver.com:65442';
    $connection.hub.start();

    myhub = $.connection.adminHub;

    myhub.client.getServerTime = function (_time){
         //How do i access the callback 
         //function in data.getservertime so as to return time to the 
        //school.lib.gettime?
    }

}

data.getservertime = function(callbak){

    myhub.server.getServerTime();

}

1 个答案:

答案 0 :(得分:0)

鉴于您当前的设计,您似乎应该将回调传递给getServerTime方法:

myhub.client.getServerTime = function (callback){
     //How do i access the callback 
     //function in data.getservertime so as to return time to the 
    //school.lib.gettime?

    // after you have retrieved the time
    if (typeof callback === 'function') {
       callback(school.lib.gettime);
    }
}

...

data.getservertime = function(callback){
   myhub.server.getServerTime(callback);
}