首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >asp.net中的SignalR客户端

asp.net中的SignalR客户端
EN

Stack Overflow用户
提问于 2012-10-20 00:26:02
回答 1查看 747关注 0票数 0

我在asp.net应用程序中创建了一个服务器中心,如下所示

代码语言:javascript
复制
public class Calc : Hub
{
    public void CalculateSomething(int a, int b)
    {
        // start working in a new thread
        var task = Task.Factory.StartNew(() => DoCalculate(a, b));

        // attach a continuation task to notify
        // the client when the work is done
        task.ContinueWith(t =>
        {
            System.Threading.Thread.Sleep(2000);
            Clients.addMessage(t.Result);
            Caller.notifyCalculateResult(t.Result);
            System.Threading.Thread.Sleep(2000);
            Caller.notifyCalculateResult("Completed");
            Clients.addMessage("Completed");
        });
    }

    private int DoCalculate(int p1, int p2)
    {
        // do some slow work on the input,
        // e.g. call webservice or I/O.
        int result = p1 + p2;
        //int result = DoSlowWork(p1, p2);
        return result;
    }
}

现在,在另一个asp.net应用程序中,我使用SiganlR客户端创建了一个客户端。但是它不能正常工作。我希望从服务器获取数据,因为它推送到客户端

代码语言:javascript
复制
using System.Threading.Tasks;
using SignalR;
using SignalR.Client;
using SignalR.Client.Hubs;
namespace WebApplication2
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Connect to the service
            var hubConnection = new HubConnection("http://localhost:3119/");

            // Create a proxy to the chat service
            var chat = hubConnection.CreateProxy("Calc");

            // Print the message when it comes in
            chat.On("addMessage", message =>Print(message));

            // Start the connection
            hubConnection.Start().Wait();

            // Send a message to the server
            chat.Invoke("CalculateSomething", 1, 2).Wait();
        }

        private async void Print(object message)
        {
            Response.Write(message);
        }
    }
}

控制台客户端应用程序运行良好。主要问题在于asp.net信标,因为它无法处理来自服务器的回调。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-24 01:08:36

看起来你调用了错误的服务器端方法,试试这个

代码语言:javascript
复制
chat.Invoke("CalculateSomething", 1, 2).ContinueWith(task =>
{ 
  Console.WriteLine("Value from server {0}", task.Result);
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12978472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档