2013-05-08 30 views
0

有沒有人有一個基於JQuery的客戶端在SignalR Hub上調用基於異步任務的方法的工作示例?有關服務器端異步任務的示例,請參閱SignalR Doco中的以下代碼。調用SignalR Async的JQuery示例任務

public Task<int> AsyncWork() 
    { 
     return Task.Factory.StartNew(() => 
     { 
      // Don't do this in the real world 
      Thread.Sleep(500); 
      return 10; 
     }); 
    } 
+0

請參閱http://www.asp。 net/signalr/overview/hubs-api/hubs-api-guide-server#asyncmethods – tdykstra 2013-05-08 21:00:52

回答

2

這裏是在服務器一側的例子 * *

public void AsyncWork() 
    { 

      // start working in a new thread 
      var task = Task.Factory.StartNew(() => dosomething()); 

    task.ContinueWith(t => 
            { 
             Caller.notifyResult(t.Result); 
          }); 

    } 


    private int dosomething() 
     { 
      int result =0; 
      return result; 
     } 

在客戶端:

<script type="text/javascript"> 
// init hub 
var proxy = $.connection.signals; 

// declare response handler functions, the server calls these 
proxy.notifyResult = function (result) { 
    alert("the result was: " + result); 
}; 

// start the connection 
$.connection.hub.start(); 

// Function for the client to call 
function AsyncWork() { 
    proxy.AsyncWork(); 
} 
</script> 
+0

'// Function f或者客戶端調用 函數AsyncWork(){ //需要camelCase proxy.asyncWork(); }' – 2017-03-23 17:37:44