【问题标题】:pass relative url in signalR hub connection在 signalR 集线器连接中传递相对 url
【发布时间】:2019-04-03 09:14:17
【问题描述】:

我正在尝试在 angularJS 中实现 signalR, 我想将相对 url 传递给集线器连接,但它正在生成当前 url(我的 angular 应用程序在其上托管)

我的 API 基本网址:http://localhost:81/NrsService/api/TestSignal

我的 Angular 应用程序运行在

http://localhost:81

这是我的 signalR 设置:

$.connection.hub.url = "/NrsService/api/TestSignal";
//Getting the connection object
connection = $.hubConnection();

就像它在http://localhost:81/signalr/negotiate? 发送请求,但我希望它是http://localhost:81/NrsService/api/TestSignal/negotiate?

【问题讨论】:

  • 您找到解决方案了吗?我有同样的问题..

标签: angularjs asp.net-web-api signalr signalr-hub


【解决方案1】:

您必须在定义客户端代理的位置编辑生成的 JavaScript 代码。从 SignalR 2.4.0 开始,定义了一个 createHubProxies 函数,您应该在其中找到这行代码:

signalR.hub = $.hubConnection("/signalr", { useDefaultPath: false });

将其更改为以下内容以防止“/signalr”在您的请求中结束:

signalR.hub = $.hubConnection("", { useDefaultPath: false });

之后,您可以简单地更改应该以您在问题中提供的方式调用的 url,例如:

$.connection.hub.url = "/NrsService/api/TestSignal";

如果您还想动态更改此 Url,可以使用 document.location 属性。就我而言,我做了这样的事情:

var subPath = document.location.pathname.substr(0, document.location.pathname.lastIndexOf("/"));
$.connection.hub.url = subPath;      // subpath equals to "/NrsService/api"

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 2015-03-28
    • 2017-03-11
    • 1970-01-01
    相关资源
    最近更新 更多