信号器无法连接远程服务器

时间:2014-08-11 23:21:13

标签: c# exception console signalr

所以,我安装了信号器库,一切都很好,除了远程连接。 我的客户端很容易在本地连接到服务器,但当我尝试远程连接时,我收到下一个错误:无法连接远程服务器。

  

关闭防火墙

StartUp.cs

[assembly: OwinStartup(typeof(PushNotifier.StartUp))]
namespace PushNotifier
{
 public class StartUp
 {
  public void Configuration(IAppBuilder appBuilder)
  {      
   appBuilder.Map("/signalr", map =>
    {
     var hubConfiguration = new HubConfiguration
      {
       EnableDetailedErrors = true,
      };
     map.UseCors(CorsOptions.AllowAll);
     map.RunSignalR(hubConfiguration);
    });
  }
 }
}

Program.cs的

  public static void Main(string[] args)
  {
   try
   {
    using (WebApp.Start("http://*:8734"))
    {
     while (true)
     {
      var pressedKey = Console.ReadKey(true).Key;

      switch (pressedKey)
      {
       case ConsoleKey.P:
        {
         var hubEntity = new HubEntity();
         hubEntity.SendNotification("hidden", JsonConvert.DeserializeObject<VersionEntity>(FileHelper.OpenFile(filePath)).Version);           
        }
        break;

       case ConsoleKey.Escape:
        return;
      }
     }
    }
   }
   catch (Exception ex)
   {
    MessageBox.Show(ex.Message + "|" + ex.StackTrace);
   }
  }

Client.cs

 var connection = new HubConnection("http://10.0.0.18:8734/signalr");

   var hubProxy = connection.CreateHubProxy("HubEntity");

   hubProxy.On<string, string>("addMessage", (message, version) =>
    {
     try
     {
      Console.WriteLine("Connected");
     }
     catch (Exception ex)
     {
      MessageBox.Show(ex.Message);
     }
    });

   try
   {
    await connection.Start();
   }
   catch (Exception ex)
   {
    MessageBox.Show(ex.Message, string.Empty, MessageBoxButton.OK, MessageBoxImage.Error);
    Application.Current.Shutdown();
   }

1 个答案:

答案 0 :(得分:3)

我最近帮助了另一位在网络上关注类似或相同示例的用户,我之所以这么说,是因为代码非常相似且方法几乎相同。

我发现在远程部署服务器时尝试连接时出现的一个问题。只是这个,

var connection = new HubConnection("http://10.0.0.18:8734/signalr");

只是改为

var connection = new HubConnection("http://10.0.0.18:8734/");

拼图的下一部分可能是端口,但是基于以下事实:当您浏览到地址时,您会收到未知的传输错误,这在这种情况下很好,因此端口是打开的,通信正在工作。< / p>

另一个常见问题是实际的集线器名称,有些人认为是错误的,但是对于我们来检查这一点,您需要向我们提供集线器实现,或者您可以简单地尝试阅读有关名称和在某些情况下,方法需要在信号器客户端中更改大小写。

使用时的另一个问题     await connection.Start();

我看不到包含此代码的函数,但如果它没有标记为异步,则上述调用将同步运行并会产生一些问题,这只有在客户端和服务器位于不同的计算机上时才真正可见,当延迟开始发挥作用。为了消除这种情况,我建议尝试

hubConnection.Start().Wait();

进一步提供帮助,很难确定你接下来要做什么,但我会假设你没有超过连接点,这就是你没有放置其余代码的原因。

我只是想参考我要知道的代码是针对一个类似的例子,代码是针对该示例的控制台版本。

{
    static void Main(string[] args)
    {
        Console.WriteLine("Starting client  http://10.0.0.18:8734/");

        var hubConnection = new HubConnection("http://10.0.0.18:8734/");
        //hubConnection.TraceLevel = TraceLevels.All;
        //hubConnection.TraceWriter = Console.Out;
        IHubProxy myHubProxy = hubConnection.CreateHubProxy("MyHub");

        myHubProxy.On<string, string>("addMessage", (name, message) => Console.Write("Recieved addMessage: " + name + ": " + message + "\n"));
        myHubProxy.On("heartbeat", () => Console.Write("Recieved heartbeat \n"));
        myHubProxy.On<HelloModel>("sendHelloObject", hello => Console.Write("Recieved sendHelloObject {0}, {1} \n", hello.Molly, hello.Age));

        hubConnection.Start().Wait();

        while (true)
        {
            string key = Console.ReadLine();
            if (key.ToUpper() == "W")
            {
                myHubProxy.Invoke("addMessage", "client message", " sent from console client").ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {
                        Console.WriteLine("!!! There was an error opening the connection:{0} \n", task.Exception.GetBaseException());
                    }

                }).Wait();
                Console.WriteLine("Client Sending addMessage to server\n");
            }
            if (key.ToUpper() == "E")
            {
                myHubProxy.Invoke("Heartbeat").ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {
                        Console.WriteLine("There was an error opening the connection:{0}", task.Exception.GetBaseException());
                    }

                }).Wait();
                Console.WriteLine("client heartbeat sent to server\n");
            }
            if (key.ToUpper() == "R")
            {
                HelloModel hello = new HelloModel { Age = 10, Molly = "clientMessage" };
                myHubProxy.Invoke<HelloModel>("SendHelloObject", hello).ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {
                        Console.WriteLine("There was an error opening the connection:{0}", task.Exception.GetBaseException());
                    }

                }).Wait();
                Console.WriteLine("client sendHelloObject sent to server\n");
            }
            if (key.ToUpper() == "C")
            {
                break;
            }
        }

    }
}
相关问题