【问题标题】:SignalR client connect to different serverSignalR 客户端连接到不同的服务器
【发布时间】:2019-05-18 06:51:26
【问题描述】:

我在我的 ASP.Net MVC 应用程序中使用 SignalR 版本 2.x,并且在我的 Angular 客户端应用程序中使用相同版本的信号器。
Asp.net MVC 应用程序托管在 http://localhost:42080 和 Angular 应用程序托管在 http://localhost:4200。 我已经安装了Microsoft.AspNet.SignalR 并在 mvc 应用程序中启用了 cors。

[HubName("msg")]
public class MessageHub : Hub
{
    public void Send(string user, string message)
    {
        Clients.User(user).Send(user, message);
    }
}

我想从我的 Angular 应用程序连接到信号器服务器,但不能。

const connection = $.hubConnection(this.config.AdminUrl); // http://localhost:42080
const chat = connection.createHubProxy('msg'); // chat.server or chat.client are undefined

我也试过了:

$.connection.hub.url = 'http://localhost:42080/signalr';
var hub = $.connection.msg; // hub = undefined
$.connection.hub.start() // this will result Error loading hubs. Ensure your hubs reference is correct, e.g. <script src='/signalr/js'></script>

如何连接到托管在不同服务器上的信号器服务器?

【问题讨论】:

    标签: javascript c# node.js angular signalr


    【解决方案1】:

    你需要像这样配置你的 Startup.cs

    public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                // Branch the pipeline here for requests that start with "/signalr"
                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);
                });
            }
        }
    

    注意

    不要在代码中将 jQuery.support.cors 设置为 true。

    不要将 jQuery.support.cors 设置为 true

    SignalR 处理 CORS 的使用。将 jQuery.support.cors 设置为 true 禁用 JSONP,因为它导致 SignalR 假定浏览器 支持 CORS。

    如果您连接到不同的服务器,请在调用 start 方法之前指定 URL,如下例所示:

    JavaScript

    $.connection.hub.url = '<yourbackendurl>;
    

    注意

    通常在调用 start 方法之前注册事件处理程序 建立连接。

    详情可见here

    【讨论】:

    • 我按照您在回答中的建议进行了更改,但不幸的是问题仍然存在。
    • 确保您可以加载信号器脚本。你的控制台有错误吗?
    • 我保存了 http://backend.com/signalr/hubs 并将其导入到我的 Angular 应用程序中,现在一切正常。
    • 如果我的回答对你有用,请投票。谢谢@mhesabi
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    相关资源
    最近更新 更多