我有一个运行
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运行它时工作正常
Error: Error during negotiation request.
通过将以下内容添加到我的< system.webServer>< /system.webServer\u0026gt;来启用我的集线器应用程序上的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.
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。