SignalR:尝试连接时webSockets超时。可能的CORS问题

时间:2015-01-29 21:25:59

标签: c# angularjs asp.net-web-api signalr cors

我正在尝试编写一个移动应用程序(角度),该应用程序连接到具有信号中心的现有网站。我在网络服务器上配置了信号器:

 // Branch the pipeline here for requests that start with "/signalr"
            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.
                    // JSONP requests are insecure but some older browsers (and some
                    // versions of IE) require JSONP to work cross domain
                    // EnableJSONP = true
                     EnableDetailedErrors = true
                };
                // Run the SignalR pipeline. We're not using MapSignalR
                // since this branch already runs under the "/signalr"
                // path.
                map.RunSignalR(hubConfiguration);
            });

服务器正在网站上运行:http://localhost:62115/

我在http://localhost:4434/上运行了另一个客户端应用程序,我正试图从http://localhost:62115/使用信号器中心:

$.connection.hub.url = "http://localhost:62115/signalr/hubs";

        var queueProxy = $.connection.queueHub;

        $.connection.hub.logging = true;

        $.connection.hub.start()
            .done(function () {
                alert('yes');
            })
            .fail(function (e) {
                alert(e);
            });

但是信号器无法连接。我收到错误无法成功初始化传输。以下是我的日志:

[16:19:58 GMT-0500 (Eastern Standard Time)] SignalR: Auto detected cross domain url.
jquery.signalR-2.1.1.js:81 [16:19:58 GMT-0500 (Eastern Standard Time)] SignalR: No hubs have been subscribed to.  The client will not receive data from hubs.  To fix, declare at least one client side function prior to connection start for each hub you wish to subscribe to.
jquery.signalR-2.1.1.js:81 [16:19:58 GMT-0500 (Eastern Standard Time)] SignalR: Negotiating with 'http://localhost:62115/signalr/hubs/negotiate?clientProtocol=1.4&connectionData=%5B%5D'.
jquery.signalR-2.1.1.js:81 [16:19:59 GMT-0500 (Eastern Standard Time)] SignalR: Connecting to websocket endpoint 'ws://localhost:62115/signalr/hubs/connect?transport=webSockets&clientProtocol=1.4&connectionToken=yNATl6%2FEQt710oI6ZGOV9%2F61Ry2SRjKQOgPDxKuXGSCYagKpbQKY5DOzRWdk1ggv6XaIm%2FJ2TUkkbmEkgCJyKHAKwy3ZweftP%2FYzRglLGe492Z4RDYY%2Btj8Aah3IhHIo&connectionData=%5B%5D&tid=2'.
jquery.signalR-2.1.1.js:81 [16:19:59 GMT-0500 (Eastern Standard Time)] SignalR: Websocket opened.
jquery.signalR-2.1.1.js:81 [16:20:04 GMT-0500 (Eastern Standard Time)] SignalR: webSockets timed out when trying to connect.
jquery.signalR-2.1.1.js:81 [16:20:04 GMT-0500 (Eastern Standard Time)] SignalR: Closing the Websocket.
jquery.signalR-2.1.1.js:81 [16:20:04 GMT-0500 (Eastern Standard Time)] SignalR: Attempting to connect to SSE endpoint 'http://localhost:62115/signalr/hubs/connect?transport=serverSentEvents&clie…AKwy3ZweftP%2FYzRglLGe492Z4RDYY%2Btj8Aah3IhHIo&connectionData=%5B%5D&tid=5'.
jquery.signalR-2.1.1.js:81 [16:20:04 GMT-0500 (Eastern Standard Time)] SignalR: EventSource connected.
jquery.signalR-2.1.1.js:81 [16:20:09 GMT-0500 (Eastern Standard Time)] SignalR: serverSentEvents timed out when trying to connect.
jquery.signalR-2.1.1.js:81 [16:20:09 GMT-0500 (Eastern Standard Time)] SignalR: EventSource calling close().
jquery.signalR-2.1.1.js:81 [16:20:09 GMT-0500 (Eastern Standard Time)] SignalR: Opening long polling request to 'http://localhost:62115/signalr/hubs/connect?transport=longPolling&clientPro…AKwy3ZweftP%2FYzRglLGe492Z4RDYY%2Btj8Aah3IhHIo&connectionData=%5B%5D&tid=6'.
jquery.signalR-2.1.1.js:81 [16:20:14 GMT-0500 (Eastern Standard Time)] SignalR: longPolling timed out when trying to connect.
jquery.signalR-2.1.1.js:81 [16:20:14 GMT-0500 (Eastern Standard Time)] SignalR: Aborted xhr request.
jquery.signalR-2.1.1.js:81 [16:20:24 GMT-0500 (Eastern Standard Time)] SignalR: Stopping connection.
jquery.signalR-2.1.1.js:81 [16:20:24 GMT-0500 (Eastern Standard Time)] SignalR: Fired ajax abort async = true.

3 个答案:

答案 0 :(得分:2)

我遇到了类似的问题,并希望在此处记录解决方案,以防其他人因为SignalR连接问题而解决问题。失败有两个独立的奇怪原因:

时钟同步问题。 我正在发布Azure,SignalR在暂存和生产环境中完美地工作。它突然不再在我的开发环境中连接了。

我最近将系统时钟设置为在同一时区内应该超过一小时。当我更正日期和时间时,SignalR开始完全适用于我本地计算机上的所有协议。我无法在任何地方找到相关文档,但我确实知道Azure表,队列和Blob需要在15分钟内完成同步时钟。

Azure SignalR背板服务总线问题。 我使用Azure Service Bus Backplane来同步多个Web前端实例的消息传递。我实际上有一个服务总线腐败(我不知道为什么,并在一个单独的帖子中提出这个问题)。我知道它是服务总线,因为当我在OWIN启动时注释掉GlobalHost.DependencyResolver.UseServiceBus方法时,事情就完美了。当我将连接字符串切换到不同的服务总线时,它完美地工作。但ONE BUS导致间歇性故障。

答案 1 :(得分:1)

我有类似的问题,一段时间以来,当我将我的nuget软件包升级到有关跨域的最新版本时,这是一场艰苦的战斗。您是否可以尝试以下方式并告诉我这是否解决了您的问题?

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.Map("/signalr", map =>
        {
            map.UseCors(CorsOptions.AllowAll);
            map.RunSignalR(new HubConfiguration { EnableJSONP = true });
        });
    }
}

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // your client localhost
        config.EnableCors(new EnableCorsAttribute(origins: "http://localhost:4434", headers: "*", methods: "*"));
    }
}

// wack "/hubs"
$.connection.hub.url = "http://localhost:62115/signalr/

$.connection.hub.start({ xdomain: true })
    .done(function () { 
        console.log('Connected. connectionId : ' + $.connection.hub.id); })
    .fail(function () {
        console.log('Could not connect!');
});

最后一定要在客户端应用的<head>中引用该脚本。请注意,您明确引用了服务器正在运行的本地主机。

<script src="//localhost:62115/signalr/hubs"></script>

答案 2 :(得分:0)

我有同样的问题。协商步骤将列出所有传输方法,但随后逐个1将超时(对我来说为5秒)。即使长时间的民意调查也被打破了。

就我而言,我们使用Redis作为SignalR的缓存。我的本地副本工作正常,因为我们有一个条件,只使用Redis缓存prod。所以我认为这是有道理的。

更新所有相关软件包为我解决了这个问题:

  • 所有Microsoft.AspNet.SignalR软件包,包括客户端lib
  • 所有Microsoft.Owin软件包
  • StackExchange.Redis
  • 所有Autofac软件包(因为AutoFac处理了正在注入的Redis依赖项)