c# – 如何从其他主机连接到我的SignalR集线器?
我有一个运行
SignalR 2.2.2的ASP.NET MVC 5应用程序此应用程序正在hub.domain.com上运行.在另一个基于Asp.Net MVC-5的应用程序(即localhost:15371)上,我想与集线器进行交互.
在我的localhost:15371应用程序中,我添加了以下代码 <script> var myHubHost = 'http://hub.domain.com/'; </script> <script src="http://hub.domain.com/Scripts/jquery.signalR-2.2.2.min.js"></script> <script src="http://hub.domain.com/signalr/hubs"></script> <script src="http://hub.domain.com/signalr/custom_code.js"></script> 但是,我在尝试连接到app.domain.com上的集线器时收到以下错误,但是当我直接从hub.domain.com运行它时工作正常
通过将以下内容添加到我的< system.webServer>< /system.webServeru0026gt;来启用我的集线器应用程序上的CORS; Web.config文件中的部分. <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> </customHeaders> </httpProtocol> 我也尝试启用JSONP以及我的集线器上的详细错误 public void Configuration(IAppBuilder app) { ConfigureAuth(app); var hubConfiguration = new HubConfiguration(); hubConfiguration.EnableDetailedErrors = true; hubConfiguration.EnableJSONP = true; app.MapSignalR(hubConfiguration); } 可能导致此错误的原因是什么?从另一个应用程序连接到我的集线器还需要做些什么? 用于连接到我的集线器的代码如下所示,可在custom_code.js文件中找到. $(document).ready(function () { // Reference the auto-generated proxy for the hub. var app = $.connection.myHubName; // The getApiUrl() method return http://hub.domain.com/signalr // as the myHubHost variable is set to http://hub.domain.com/ $.connection.hub.url = getApiUrl('signalr'); $.connection.hub.error(function (error) { console.log(error) }); // Create a function that the hub can call back get the new events app.client.updatePanel = function (message) { // Do stuff } // Start the connection. $.connection.hub.start(); // This allows me to set a variable to control the base-url host when including this file on a different app. function getApiUrl(uri) { var link = "/"; if (typeof window.myHubHost !== typeof someUndefinedVariableName) { link = window.myHubHost; } return link + uri; } }); 更新 我按照这样的方式启用了日志记录 $.connection.hub.logging = true; 我还安装了Microsoft.Owin.Cors包以根据documentation启用cors.这是我当前的配置 app.Map("/signalr",map => { map.UseCors(CorsOptions.AllowAll); var hubConfiguration = new HubConfiguration { EnableDetailedErrors = true }; map.RunSignalR(hubConfiguration); }); 这是我在控制台中获得的日志.如您所见,协商失败. SignalR: Auto detected cross domain url. SignalR: Client subscribed to hub 'myHubName'. SignalR: Negotiating with 'http://hub.domain.com/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%5D'. SignalR error: Error: Error during negotiation request. SignalR: Stopping connection. 解决方法
我终于弄明白了我的问题.
我的布局中有html基本标记导致问题. 我删除了 我的问题解决了! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |