客户端无法连接到SignalR Hub(CORS不起作用)

时间:2015-04-29 08:02:30

标签: asp.net signalr

服务器中的My StartUp.cs(使用Owin.UseCors实现跨域请求):

public void Configuration(IAppBuilder app)
{
    app.Map("/signalr", map =>
    {
        // Setup the CORS middleware to run before SignalR.
        // By default this will allow all origins. You can 
        // configure the set of origins and/or http verbs by
        // providing a cors options with a different policy.
        map.UseCors(CorsOptions.AllowAll);
        var hubConfiguration = new HubConfiguration
        {
            // You can enable JSONP by uncommenting line below.
            // EnableJSONP = true
        };
        // Run the SignalR pipeline. We're not using MapSignalR
        // since this branch already runs under the "/signalr"
        // path.
        map.RunSignalR(hubConfiguration);
    });
}

我的客户端(控制台项目):

static void Main(string[] args)
{
    IHubProxy _hub;
    string url = @"http://111.111.111.111:13098/signalr/hubs";
    var connection = new HubConnection(url, querystringData);
    _hub = connection.CreateHubProxy("TestHub");
    connection.Start().Wait();
    _hub.On("Broadcast", x => Console.WriteLine(x));
}

只有当我在服务器上运行客户端(console.exe)(信号器项目托管在IIS中)时,它才能连接到集线器。

如果我在我的电脑上运行客户端,它就无法连接。

为什么?

在Chrome上输入http://111.111.111.111:13098/signalr/hubs

enter image description here enter image description here

2 个答案:

答案 0 :(得分:0)

你能启用JSONP吗?

var hubConfiguration = new HubConfiguration { EnableJSONP = true };

答案 1 :(得分:0)

解决。

我使用了错误的端口......防火墙阻止了它。

相关问题