【问题标题】:SignalR console app start must be called before data can be sent必须先调用 SignalR 控制台应用程序启动,然后才能发送数据
【发布时间】:2012-12-02 16:33:32
【问题描述】:

我正在尝试创建一个客户端信号器应用程序。目前,我能够让我的 MVC 客户端向 Hub 发送消息和从 Hub 发送消息,但是,我的 .NET 客户端应用程序遇到了一些困难

服务器集线器代码:

namespace ServerHub
{
    public class ChatterBox : Hub
    {

        public void Send(string message)
        {
            Clients.All.addMessage(message);
        }
    }
}

Console App 代码(几乎直接取自 github)

using Microsoft.AspNet.SignalR.Client.Hubs;
using ServerHub;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
          Say("hello from console");
        }
        public static void Say(string message)
        {
            //var connection = new HubConnection("http://localhost/");

            //IHubProxy myHub = connection.CreateHubProxy("ChatterBox");

            var connection = new HubConnection("http://localhost/");
            //Make proxy to hub based on hub name on server
            var myHub = connection.CreateHubProxy("ChatterBox");
            //Start connection
            connection.Start().ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
                }
                else
                {
                    Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);
                    // Do more stuff here
                }
            });

            connection.Send("Hello").ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    Console.WriteLine("Send failed {0}", task.Exception.GetBaseException());
                }
                else
                {
                    Console.WriteLine("Success");
                }
            });
        }
    }
}

我在connection.Send()期间收到以下错误消息

必须先调用 Start 才能发送数据。

我哪里做错了?

编辑:

第二次尝试:

    var connection = new HubConnection("http://localhost/");
    //Make proxy to hub based on hub name on server
    var myHub = connection.CreateHubProxy("ChatterBox");
    //Start connection
    connection.Start().ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
            }
            else
            {
                Console.WriteLine("Success! Connected with client connection id {0}",
                                  connection.ConnectionId);

                // Do more stuff here

                connection.Send("Hello");
            }
        });


    myHub.Invoke("Send", "lol");

还是报错

编辑:第三次尝试

            var connection = new HubConnection("http://localhost:60610/");
            //Make proxy to hub based on hub name on server
            var myHub = connection.CreateHubProxy("ChatterBox");
            //Start connection
            connection.Start().ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
                }
                else
                {
                    //Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);

                    myHub.Invoke("Send", "lol");
                }
            });

似乎工作正常

我还需要设置端口号 - 例如,如果我像这样浏览我的 MVC 网站:http://localhost:60610/,这是我需要在控制台应用程序中设置的地址。

我可以说,在部署期间,它不会成为问题,因为它默认为端口 80?

【问题讨论】:

    标签: c# asp.net-mvc signalr


    【解决方案1】:

    阅读此http://msdn.microsoft.com/en-us/library/dd460717.aspx

    TL;DR Start 是异步的,这不是您编写顺序异步代码的方式:

        var connection = new HubConnection("http://localhost/");
        //Make proxy to hub based on hub name on server
        var myHub = connection.CreateHubProxy("ChatterBox");
        //Start connection
        connection.Start().ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
            }
            else
            {
                Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);
    
                // Do more stuff here
    
                connection.Send("Hello").ContinueWith(task =>
                {
                     .......
                });
            }
        });
    

    如果您可以使用 .NET 4.5,它会让您的生活更轻松:

    var connection = new HubConnection("http://localhost/");
    //Make proxy to hub based on hub name on server
    var myHub = connection.CreateHubProxy("ChatterBox");
    //Start connection
    try 
    {
        await connection.Start();
        Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);
    }
    catch(Exception ex) 
    {
        Console.WriteLine("Failed to start: {0}", ex.GetBaseException());
    }
    
    await connection.Send("Hello");
    

    更新:让我们再试一次。下面的代码将起作用,但您应该真正阅读该链接以了解如何编写异步代码。否则你只会一遍又一遍地遇到这些问题。

    var connection = new HubConnection("http://localhost/");
    //Make proxy to hub based on hub name on server
    var myHub = connection.CreateHubProxy("ChatterBox");
    //Start connection
    connection.Start().ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
            }
            else
            {
                Console.WriteLine("Success! Connected with client connection id {0}",
                                  connection.ConnectionId);
    
                myHub.Invoke("Send", "lol");
            }
        });
    

    【讨论】:

    • 在.Send("Hello")之后你通常会继续做什么?
    • 我试过你的代码(我没有使用 4.5,所以我使用的是你发布的第一个代码块),我做了 connection.Send("Hello"); 而不是 connection.Send("Hello").ContinueWith(task => { ....... }); 但它没有工作,也没有错误消息。没有任何东西发送到我的浏览器
    • 可能因为你需要写 myhub.Invoke("Send", "hello");深入了解 .NET 客户端的文档github.com/SignalR/SignalR/wiki/SignalR-Client-Hubs
    • 是的,我已经尝试过了。我收到与此线程标题中相同的错误消息。 “必须先调用 start 才能发送数据。”
    • 我的配置正确吗?我没有更改 Hub 名称,所以 localhost 应该可以正常工作吗?
    猜你喜欢
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    相关资源
    最近更新 更多