Skip to main content
 首页 » 编程设计

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

2024年08月06日39lyj

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

 // 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. 

请您参考如下方法:

我有一个类似的问题,并想在此处记录解决方案,以防其他人因 SignalR 连接问题而烦恼。失败有两个不同的奇怪原因:

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

我最近将我的系统时钟调慢了一个小时,比同一时区内的时钟要慢一小时。当我更正日期和时间时,SignalR 开始在我本地机器上完美地处理所有协议(protocol)。我在任何地方都找不到这方面的文档,但我知道 Azure 表、队列和 Blob 需要在 15 分钟内同步时钟。

Azure SignalR 背板服务总线问题。 我正在使用 Azure 服务总线背板在多个 Web 前端实例之间同步消息传递。我实际上有一个服务总线损坏(我不知道为什么并且在单独的帖子中问过这个问题)。我知道它是服务总线,因为当我在我的 OWIN 启动中注释掉 GlobalHost.DependencyResolver.UseServiceBus 方法调用时,一切正常。当我将连接字符串切换到不同的服务总线时,它运行良好。但是 ONE BUS 会导致间歇性故障。