用于调用SignalR Async Task的JQuery示例

时间:2013-05-08 01:08:20

标签: jquery signalr async-await c#-5.0

有没有人有一个基于JQuery的客户端在SignalR Hub上调用基于异步任务的方法的工作示例?有关服务器端异步任务的示例,请参阅SignalR Doco中的以下代码。

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

1 个答案:

答案 0 :(得分: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>