簡體   English   中英

Signalr無法連接遠程服務器

[英]Signalr unable to connect the remote server

因此,我安裝了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();
   }

我最近幫助了另一個在Web上遵循相似或相同示例的用戶,之所以這樣說,是因為代碼非常相似且方法幾乎相同。

我發現在遠程部署服務器時嘗試連接時出現了一個問題。 就是這樣

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

只是更改為

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

難題的下一部分可能是端口,但是基於以下事實:當您瀏覽到該地址時,會收到未知的傳輸錯誤,在這種情況下這很好,因此端口處於打開狀態並且通訊正常。

另一個常見問題是實際的集線器名稱,一些人弄錯了大小寫,但是對於我們進行檢查,您需要向我們提供集線器實現,或者您可以嘗試閱讀有關名稱的方式以及在某些情況下的信息。方法需要在Signalr客戶端中更改大小寫。

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

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM