【问题标题】:SignalR cross-domain javascript client hub start method fails only when client methods are subscribedSignalR 跨域 javascript 客户端集线器启动方法仅在订阅客户端方法时失败
【发布时间】:2016-03-15 17:26:00
【问题描述】:

SignalR 跨域 javascript 客户端 集线器启动方法 仅在订阅客户端方法时才会失败。如果客户端方法没有订阅,hub start 调用成功server 方法调用成功

$.connection.hub.url = 'http://my-cross-domain.com:876/signalr';
$.connection.hub.logging = true;
var chatHub = $.connection.chatHub;

chatHub.client.displayMessage = function (windowID, message) {
    alert("test");
};

$.connection.hub.start().done(function () {
    alert("Connected");
});

如果 chatHub.client.displayMessage 没有订阅。它启动和连接良好。

但是如果客户端方法被订阅,它不会连接并抛出

no-access-control-allow-origin-header-is-present-on-the-requested-resource 一种错误。

失败:订阅客户端方法时

不订阅客户端方法时成功

更新:

忘了提到我正在使用 CORS for SignalR,正如 Jay 在下面指出的那样。它也是一个带有 WebAPI 控制器的 MVC 应用程序

请帮忙!

【问题讨论】:

  • 您是否手动将jQuery.support.cors 设置为true?如果您手动将 jQuery 设置为接受 cors,则会导致 SignalR 失败
  • 不,我没有这样做。谢谢。
  • 您能加入start().fail() 承诺和console.log(arguments) 吗?
  • 如果您可以访问服务器端调试,可能值得覆盖服务器上的onConnected 函数并检查HttpContext 对象,以查看服务器是否真的发回了正确的标头
  • Jay,感谢 cmets。 start().fail() 给出 Error: Error during start request。停止连接。 顺便说一句,希望您已经看到我添加的最新控制台屏幕截图。如果我发现服务器没有发送正确的标头,我能做些什么吗?

标签: javascript signalr


【解决方案1】:

响应状态 500(内部服务器错误)是关于集线器的 OnConnected 方法中的空引用。能够使用 Fiddler 解决这个问题。

【讨论】:

    【解决方案2】:

    确保您在前端设置 cors 支持

    jQuery.support.cors = true //Do not put this
    

    在服务器上,在您的启动类中,通过添加以下内容确保允许 cors:

      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
                };
                // Run the SignalR pipeline. We're not using MapSignalR
                // since this branch already runs under the "/signalr"
                // path.
                map.RunSignalR(hubConfiguration);
            });
    

    有关 Cors/JSONP 支持的信息可以在以下位置找到:asp net

    【讨论】:

    • 在服务器端,我已经通过启用 JSON 之类的附加行做到了这一点: var hubConfiguration = new HubConfiguration { // 您可以通过取消注释下面的行来启用 JSONP。 // JSONP 请求不安全,但一些旧浏览器(和一些 // IE 版本)要求 JSONP 跨域工作 EnableJSONP = true };
    • 您是否在您的网络配置文件中启用了它?并在注册路线时将其设置为允许? asp.net/web-api/overview/security/…
    • 我建议先使用 AJAX 来测试 cors 在您的 signalR 实现之外是否有效,然后您可以缩小问题所在
    • 杰伊,感谢您的宝贵时间。正如我之前提到的,只要我不尝试订阅客户端方法,我就能很好地连接和调用服务器方法。
    • Jay,我已删除 EnableJSONP = true
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多