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

Signalr 信号器:无法连接到http://localhost:8080 使用完整IP地址的自托管服务器,signalr,signalr-hub,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 stati

我正在使用以下代码创建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);
        }
    }
}
内部类配置
{
内部静态字符串serverurl=null;
内部静态Microsoft.AspNet.signal.HubConfiguration HubConfiguration=null;
内部静态信号Hub集线器{get;set;}
内部静态void StartServer()
{
服务器URL=”http://localhost:8080";
//在此方法中,启动类型为Startup的web应用程序在指定的URL处启动(http://localhost:8080). 
{
Microsoft.Owin.Hosting.WebApp.Start(serverurl);
Log.AddMessage(“运行在”+serverurl上的服务器”);
}
捕获(例外情况除外)
{
Log.AddMessage(“启动服务器时出错:”+ex);
}
}
}
类启动
{
//包含信号服务器配置的类
//(唯一的配置是调用UseCors),
//以及对MapSignalR的调用,它为项目中的任何集线器对象创建路由。
公共无效配置(IAppBuilder应用程序)
{
尝试
{
应用程序UseCors(CorsOptions.AllowAll);
//发生异常时启用详细错误
Config.hubconfiguration=新的hubconfiguration();
Config.hubconfiguration.enabledDetailedErrors=true;
app.MapSignalR(“/signalr”,Config.hubconfiguration);
}
捕获(例外情况除外)
{
Log.AddMessage(“服务器配置期间发生错误:”+ex.Message);
}
}
}
我还创建了一些客户端应用程序,通过集线器连接到此SignalR服务器,当我在本地计算机上进行测试时,一切正常

但是当我尝试使用域计算机IP地址或计算机名而不是
http://localhost:8080
(从域网络的另一台计算机,甚至从我的本地计算机),我收到一个错误,因为找不到服务器

您能帮助我使用IP地址而不是“本地主机”连接到我的信号服务器吗?

解决方案是:

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

  • 您可以使用此选项自动设置IP:

     <script src="http://<%=Request.ServerVariables("LOCAL_ADDR")%>:8080/signalr/hubs"></script>
    
     $.connection.hub.url = "http://<%=Request.ServerVariables("LOCAL_ADDR")%>:8080/signalr";
    
    
    $.connection.hub.url=“http://:8080/signal”;
    
    我通过为serverHub使用的特定端口添加入站和出站规则解决了相同的问题,例如在我的案例8080中,此端口需要打开,以便将流入我的计算机和流出到目标的流量(例如,serverHub向所有连接的客户端发送消息)。

    此端口可能被防火墙阻止。您是否尝试将
    serverurl
    切换到“http://{MyIPAddress}:8080”?@halter73是的,我试图将服务器URL设置为我的域IP地址(
    serverurl=)http://192.168.20.24:8080“;
    )和我的计算机名,但我收到以下错误:
    System.Reflection.TargetInvocationException:调用的目标已引发异常。-->System.Net.HttpListener异常:Microsoft.Owin.Host.HttpListener.OwinHttpListener.Start()的System.Net.HttpListener.AddAllPrefixes()的System.Net.HttpListener.AddAllPrefixes()访问被拒绝(HttpListener侦听器,Func
    2 appFunc,IList
    1地址,IDictionary
    2功能,Func
    2 loggerFactory)
    @halter73抱歉,当我使用管理员权限执行服务器时,具有IP地址的服务器url起作用。当我将服务器url设置为我的IP地址时,我现在可以从我的计算机连接信号器客户端,但不能从网络的其他计算机连接,其中我得到以下错误:
    System.Net.Http.HttpRequestException:发送请求时出错。-->System.Net.WebException:无法连接到远程服务器-->System.Net.Sockets.SocketException:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机没有响应192.168.20.24:8080
    。您好,请看我的问题。,哈,我真傻。这就是我需要提醒我的,在Xamarin应用程序中使用SignalR不适用于
    localhost
    。必须计算出本地IP地址。也许这会帮助其他犯同样愚蠢错误的人。嗨,你能解释一下为什么我不能只指定localhost吗?