Kernighan
Kernighan

Reputation: 31

Signalr unable to connect the remote server

So, I installed signalr library and everything work wonderful except remote connection. My client easily has connected to the server locally but when i try to connect remotely i get the next error: unable to connect the remote server.

Firewall if off

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

Upvotes: 2

Views: 5937

Answers (1)

Louis Lewis
Louis Lewis

Reputation: 1298

I have recently helped another user who followed a similar or the same example on the web, the reason I say that, is because the code is very similar and the methods are pretty much the same.

One problem that I found that came up when attempting the connection when the server is deployed remotely. Was simply this,

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

was simply changed to

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

the next part of the puzzle may be the ports, however based on the fact that when you browse to the address you get the unknown transport error, which is good in this case, so ports are open and communication is working.

Another common problem is the actual hub name, a few people get the case wrong, however for us to check this, you would need to provide us with the hub implementation, or you can simply try read up about how the name and in some instances methods require case changing in signalr clients.

another problem lies when using await connection.Start();

I cannot see your function that is containing this code, but if it is not marked async, the above call will run synchronously and will create a few problems, this only really becomes visible when the client and server are on separate machines, when the a delay starts coming into play. To eliminate this, I suggest trying

hubConnection.Start().Wait();

further to help, it is hard to determine what you are doing next, but I will assume that you have not gone past the connection point, which is why you did not place the rest of your code.

I am just for reference going to place code that I know to be working against a similar example, the code is for a console version of the example.

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

    }
}

Upvotes: 3

Related Questions