SignalR Server Hosting Localhost

2019-02-18 17:32发布

I'm running a server with a localhost http://*:52080. On the same computer, I'm run client and trying to connect to a local IP hub http://192.168.1.102:52080/signalr. Everything works well.

But if I run the client on another computer (from the same local network) and try to connect to http://192.168.1.102:52080/signalr, it does not connect. The client catches an exception ("System.AggregateException" in mscorlib.dll).

Port 52080 on the computer with the hub is open.

What could be the reason for the failure?

Server:

    using System;
    using Microsoft.Owin.Hosting;

    public class Program
    {
        static void Main(string[] args)
        {
            string url = "http://*:52080";
            using (WebApp.Start<Startup>(url))
            {
                Console.WriteLine("Server running at {0}\n", url);
                Console.ReadLine();
            }
        }
    }

Startup.cs

    using Owin;
    using Microsoft.Owin.Cors;

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCors(CorsOptions.AllowAll);
            app.MapSignalR();
        }
    }

SGHub.cs

    using System;
    using System.Threading.Tasks;
    using Microsoft.AspNet.SignalR;
    using System.Collections.Generic;

    public class SGHub : Hub
    {   
        public static List<string> Users = new List<string>();

        public override Task OnConnected()
        {
            Console.WriteLine("\nOnConnected {0}", Context.ConnectionId);
            Users.Add(Context.ConnectionId);

            Clients.Caller.broadcastMessage("Server:", "Successful connection");
            Clients.Others.broadcastMessage("Server:", "New connection");

            return (base.OnConnected());
        }
    }

Client:

using System; 
using Microsoft.AspNet.SignalR.Client; 
using Microsoft.AspNet.SignalR.Client.Hubs; 
using Newtonsoft.Json.Linq; 

class Program
{
    static void Main(string[] args)
    {
        string serverURL = "http://192.168.1.102:52080/signalr";

        Console.WriteLine("Connection to {0}\n", serverURL);
        HubConnection hubConnection = new HubConnection(serverURL);
        IHubProxy myHubProxy = hubConnection.CreateHubProxy("StartGameHub");

        myHubProxy.On<string, string>("Send", (name, message) => Console.Write("Recieved addMessage: " + name + ": " + message + "\n"));
        myHubProxy.On("heartbeat", () => Console.Write("Recieved heartbeat \n"));

        Subscription subscription = myHubProxy.Subscribe("broadcastMessage");
        subscription.Received += SubscriptionData;

        while (true)
        {
            string key = Console.ReadLine();

            if (key.ToUpper() == "A")
            {
                try
                {
                    Console.WriteLine("Start connect..");
                    hubConnection.Start().Wait();
                }
                catch (System.AggregateException e)
                {
                    Console.WriteLine("Connected fauld :(");
                }
            }
        }
    }

    private static void SubscriptionData(IList<JToken> obj)
    {
        Console.WriteLine(obj[1].ToString());
    }
}

0条回答
登录 后发表回答