我正在使用以下代码创建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地址而不是“ localhost”连接到SignalR服务器吗?
解决方案是:
http://{MyIPAddress}:8080
,而不是http://localhost:8080
您可以使用它来自动设置IP:
<script src="http://<%=Request.ServerVariables("LOCAL_ADDR")%>:8080/signalr/hubs"></script>
$.connection.hub.url = "http://<%=Request.ServerVariables("LOCAL_ADDR")%>:8080/signalr";
同一问题,我通过添加serverHub正在使用的特定端口的入站和出站规则来解决,例如,在我的情况下8080,此端口需要打开,以使流入内部流向我的计算机并向外流向目的地的流量(例如serverHub发送消息)所有连接的客户端。