SignalR:无法使用完整的IP地址连接到http:// localhost:8080自托管服务器

时间:2014-08-19 12:11:44

标签: signalr signalr-hub

我正在使用以下代码创建Signal R自托管服务器:

internal class Config
{
    internal static string serverurl = null;
    internal static Microsoft.AspNet.SignalR.HubConfiguration hubconfiguration = null;
    internal static SignalRHub Hub { get; set; }
    internal static void StartServer()
    {
        serverurl = "http://localhost:8080";
        // In this method, a web application of type Startup is started at the specified URL (http://localhost:8080). 
        {
            Microsoft.Owin.Hosting.WebApp.Start<Startup>(serverurl);
            Log.AddMessage("Server running on " + serverurl); 
        }
        catch(Exception ex)
        {
            Log.AddMessage("An error occurred when starting Server: " + ex);
        }
    }
}

class Startup
{
    // the class containing the configuration for the SignalR server 
    // (the only configuration is the call to UseCors), 
    // and the call to MapSignalR, which creates routes for any Hub objects in the project.
    public void Configuration(IAppBuilder app)
    {
        try
        {
            app.UseCors(CorsOptions.AllowAll);
            // Enable detailed errors when an exception occures
            Config.hubconfiguration = new HubConfiguration();
            Config.hubconfiguration.EnableDetailedErrors = true;

            app.MapSignalR("/signalr", Config.hubconfiguration);
        }
        catch(Exception ex)
        {
            Log.AddMessage("An error occurred during server configuration: " + ex.Message);
        }
    }
}

我还创建了一些通过集线器连接到SignalR服务器的客户端应用程序,当我在本地计算机上进行测试时,一切正常。

但是当我尝试使用域计算机IP地址或计算机名称而不是http://localhost:8080(来自域网络的另一台计算机,甚至是我本地计算机)连接到服务器时,我就是这样做的。因无法找到服务器而收到错误。

请帮助我使用IP地址连接到我的SignalR服务器,而不是&#34; localhost&#34;?

3 个答案:

答案 0 :(得分:5)

解决方案是:

  1. 将服务器网址设置为http://{MyIPAddress}:8080而不是http://localhost:8080
  2. 通过在Windows防火墙中添加新的TCP入站规则来打开端口8080

答案 1 :(得分:0)

您可以使用此功能自动设置您的IP:

 <script src="http://<%=Request.ServerVariables("LOCAL_ADDR")%>:8080/signalr/hubs"></script>

 $.connection.hub.url = "http://<%=Request.ServerVariables("LOCAL_ADDR")%>:8080/signalr";

答案 2 :(得分:0)

我通过为serverHub使用的特定端口添加入站规则和Oubound规则解决了同一问题,例如在我的情况8080中,需要打开此端口,以使流量流入我的计算机并流出到目的地,例如serverHub发送按摩所有连接的客户。

相关问题