【问题标题】:JQuery example for calling SignalR Async Task调用 SignalR 异步任务的 JQuery 示例
【发布时间】:2013-05-10 11:11:16
【问题描述】:

有没有人有一个基于 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;
        });
    }

【问题讨论】:

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


【解决方案1】:

这是一个例子 *在服务器端:*

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>

【讨论】:

  • // Function for the client to call function AsyncWork() { //Needs to be camelCase proxy.asyncWork(); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多