本文介绍了JQuery的例子调用SignalR异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有一个基于jQuery的客户端调用一个SignalR集线器异步任务为基础的方法工作的例子?见$ C $下面从SignalR DOCO c代表一个服务器侧异步任务的一个例子。

 公共任务< INT> AsyncWork()
    {
        返回Task.Factory.StartNew(()=>
        {
            //不要在现实世界中做到这一点
            Thread.sleep代码(500);
            返回10;
        });
    }


解决方案

下面是一个例子
*的在服务器端:的*

 公共无效AsyncWork()
    {          //开始一个新的线程工作
          变种任务= Task.Factory.StartNew(()=> DoSomething的());    task.ContinueWith(T =>
                                  {
                                      Caller.notifyResult(t.Result);
                          });    }
   私人诠释DoSomething的()
        {
            INT结果为0;
            返回结果;
        }

在客户端:

 <脚本类型=文/ JavaScript的>
//初始化枢纽
VAR代理= $ .connection.signals;//声明响应处理函数,服务器调用这些
proxy.notifyResult =功能(结果){
    警报(结果是:+结果);
};//启动连接
$ .connection.hub.start();//功能客户端调用
功能AsyncWork(){
    proxy.AsyncWork();
}
< / SCRIPT>

Does anyone have a working example of a JQuery based client calling an async task based method on a SignalR Hub? See the code below from the SignalR Doco for an example of a server side async task.

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

Here is an example*at server side:*

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;
        }

At client side:

<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>

这篇关于JQuery的例子调用SignalR异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 19:02