asp.net-mvc - SignalR:无法连接到本地或任何其他 IP 地址

标签 asp.net-mvc c#-4.0 server signalr signalr-hub

我正在尝试制作能够连接到“http://localhost:8080”或http://127.0.0.1:8080/的SignalR服务器和客户端架构但我无法连接我的本地 IP 地址,如“192.x.x.x”,这可能是什么原因? 请帮助我,我也将我的代码放在这里......

 public partial class WinFormsServer : Form
        {
            private IDisposable SignalR { get; set; }
            const string ServerURI = "http://localhost:8080";

     private void ButtonStart_Click(object sender, EventArgs e)
            {
                WriteToConsole("Starting server...");
                ButtonStart.Enabled = false;
                Task.Run(() => StartServer());
            }
     private void StartServer()
            {
                try
                {
                    SignalR = WebApp.Start(ServerURI);
                }
                catch (TargetInvocationException)
                {
                    WriteToConsole("Server failed to start. A server is already running on " + ServerURI);
                    //Re-enable button to let user try to start server again
                    this.Invoke((Action)(() => ButtonStart.Enabled = true));
                    return;
                }
                this.Invoke((Action)(() => ButtonStop.Enabled = true));
                WriteToConsole("Server started at " + ServerURI);
            }
     class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                app.UseCors(CorsOptions.AllowAll);
                app.MapSignalR();
            }
        }

    }

最佳答案

我尝试了不同的解决方案,但找不到正确的解决方案。

最后我发现了只与权限有关的问题。 以管理员身份运行您的 SignalR 服务器应用程序。它将开始在本地 IP 上运行,例如 192.168.X.X:9090,然后您的客户端应用程序可以使用此 IP 地址从任何其他 PC 连接此服务器。

class Program
{
    static void Main(string[] args)
    {
        var url = $"http://{GetLocalIPAddress()}:8080";
        using (WebApp.Start<Startup>(url))
        {                
            Console.WriteLine($"Server running at {{{url}}}");
            Console.ReadLine();
        }
    }

    public static string GetLocalIPAddress()
    {
        var host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                return ip.ToString();
            }
        }
        throw new Exception("Local IP Address Not Found!");
    }

}

关于asp.net-mvc - SignalR:无法连接到本地或任何其他 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38100618/

相关文章:

css - 如何让我的 scss 在网络服务器上更快地更新 css 文件?

css - ASP.NET MVC Bootstrap iPhone 导航栏 UI 问题

generics - .net 4 泛型问题

xml - 在 C# 中创建 MS Project XML 文件

git - 如何创建存储库并将代码从服务器/cpanel 推送到 bitbucket

c - 后台线程上的多线程 TCP 服务器 - 如何强制从主线程关闭

c# - 显示空类型的数据

c# - 构建后所有页面的初始加载速度变慢

asp.net-mvc - 使用 GoogleOAuth2AuthenticationOptions 出现 redirect_uri_mismatch 错误

wpf - MVVM设计模式