跨域将无法与SignalR PersistentConnection工作 [英] Cross-domain will not work with a SignalR PersistentConnection

查看:1063
本文介绍了跨域将无法与SignalR PersistentConnection工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:别人问原来这个问题,但删除它之前,我可以张贴我的答案。由于这个问题涉及开发商试图使SignalR工作跨域时面临很多问题,我决定去复制它。另外,我已经写完了答案!

NOTE: Someone else originally asked this question but deleted it before I could post my answer. Since this question covers many issues that developers face when trying to make SignalR work cross-domain, I decided to replicate it. Plus, I had already finished writing the answer!

我运行在一个ASP.NET MVC的.NET Framework 4项目SignalR 1.0.1服务器。我有一个不同的域(不同的本地主机端口)尝试通过JavaScript客户端连接其他ASP.NET应用程序。我得到这个当我的应用程序试图连接:

I'm running a SignalR 1.0.1 server in an ASP.NET MVC .NET Framework 4 project. I have another ASP.NET application on a different domain (different localhost port) trying to connect via the JavaScript client. I get this when my application tries to connect:

XMLHttpRequest cannot load http://localhost:31865/api/negotiate?_=1363105027533.
Origin http://localhost:64296 is not allowed by Access-Control-Allow-Origin.

我已经执行了所有步骤,使跨域支持,SignalR? - 我缺少什么

I've followed all steps to enable cross-domain support with SignalR -- what am I missing?


  • jQuery.support.cors = TRUE;

  • $■连接(的http://本地主机:31865 / API','',虚假的,{JSONP:真的,xdomain:真});

  • RouteTable.Routes.MapHubs(新HubConfiguration {EnableCrossDomain =真});

  • RouteTable.Routes.MapConnection< ApiConnection>(/ API,API);

  • jQuery.support.cors = true;
  • $.connection('http://localhost:31865/api', '', false, { jsonp: true, xdomain: true });
  • RouteTable.Routes.MapHubs(new HubConfiguration { EnableCrossDomain = true });
  • RouteTable.Routes.MapConnection<ApiConnection>("/api", "api");

我还添加了API项目中的以下到Web.config中:

I also added the following to Web.config in the API project:

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

我使用的是PersistentConnection我SignalR服务器,而不是集线器。

I'm using a PersistentConnection for my SignalR server, not hubs.

任何想法?

推荐答案

MapHubs 将配置在/ signalr端点与您所有的集线器通信类。既然你不使用集线器,调用 MapHubs 是不必要的。到 MapHubs 呼叫也没有你/ API接口的配置没有任何影响。

MapHubs will configure an endpoint at /signalr for communicating with all your Hub classes. Since you are not using Hubs, the call to MapHubs is unnecessary. The call to MapHubs also does not have any effect on the configuration of your /api connection.

您到MapConnection调用应改为如下:

Your call to MapConnection should be changed to read as follows:

RouteTable.Routes.MapConnection<ApiConnection>("api", "api",
    new ConnectionConfiguration { EnableCrossDomain = true });

注意:的第二个参数 MapConnection 是URL。第一个参数是路由名。在 / 是不必要的,但并没有真正伤在任一情况。

NOTE: The second argument to MapConnection is the URL. The first argument is the route name. The / is unnecessary, but doesn't really hurt in either case.


  • 设置 jQuery.support.cors = TRUE; 应的完成为了让在做环境跨域请求不支持CORS尚未但是允许跨域XHR请求(Windows小工具,等等) [1] 。这不涉及到IE浏览器的任何版本或任何其他浏览器,我知道的。如果浏览器不支持CORS,SignalR就已经自动回落到JSONP 除非设置 jQuery.support.cors 为true。

  • Setting jQuery.support.cors = true; should ONLY be done "To enable cross-domain requests in environments that do not support cors yet but do allow cross-domain XHR requests (windows gadget, etc)" [1]. This does not pertain to any versions of IE or any other browser that I know of. If the browser does not support CORS, SignalR will already automatically fall back to JSONP unless you set jQuery.support.cors to true.

如果你只是设置为true盲目,SignalR将假定环境不支持跨域XHR请求和不可以自动退回到JSONP渲染SignalR无法建立跨域连接运行时在真正不支持CORS的浏览器。

If you just set this to true blindly, SignalR will assume that the environment does support cross-domain XHR requests and not automatically fall back to JSONP rendering SignalR unable to establish cross-domain connections while running in browsers that truly don't support CORS.

$■连接(的http://本地主机:31865 / API','',虚假的,{JSONP:真的,xdomain:真}); 不正确。您应该只需要

$.connection('http://localhost:31865/api', '', false, { jsonp: true, xdomain: true }); is incorrect. You should only need

var connection = $.connection('http://localhost:31865/api');

xdomain 不再为SignalR JS客户端的选项,如果你的真正的要指定 JSONP ,你应该这样做,当你启动像这样的连接:

xdomain is no longer an option for the SignalR JS client, and if you really want to specify jsonp, you should do it when you start the connection like so:

 connection.start({ jsonp: true}).done(function () { /* ... */ });

我要重申,SignalR会的自动的回落到JSONP如果环境不支持CORS,所以你的不该亲自指定此选项。 JSONP不需要访问控制允许来源头,但它确实迫使SignalR使用其最没有效率的运输:长轮询

I should reiterate that SignalR will automatically fall back to JSONP if the environment does not support CORS, so you should not specify this option yourself. JSONP does not require an Access-Control-Allow-Origin header, but it does force SignalR to use its most inefficient transport: long polling.

您不需要设置 customHeaders 在你的web.config。 SignalR将设置在SignalR响应访问控制允许来源头时会自动设置 EnableCrossDomain 在你真正的 ConnectionConfiguration

You do not need to setup customHeaders in your Web.config. SignalR will set the Access-Control-Allow-Origin header in SignalR responses automatically when you set EnableCrossDomain to true in you ConnectionConfiguration.

参考<一个href=\"https://github.com/SignalR/SignalR/wiki/QuickStart-Persistent-Connections\">https://github.com/SignalR/SignalR/wiki/QuickStart-Persistent-Connections对于紧靠使用更多的建议 PersistentConnections

Reference https://github.com/SignalR/SignalR/wiki/QuickStart-Persistent-Connections for more advice abut using PersistentConnections.

这篇关于跨域将无法与SignalR PersistentConnection工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆