javascript - SignalR 客户端连接到不同的服务器

标签 javascript c# node.js angular signalr

我在我的 ASP.Net MVC 应用程序中使用 SignalR 版本 2.x,并且在我的 Angular 客户端应用程序中使用相同版本的 Signalr。
托管在 http://localhost:42080 中的 Asp.net MVC 应用程序和托管在 http://localhost:4200 中的 Angular 应用程序。 我已经安装了 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>

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

最佳答案

你需要像这样配置你的 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);
            });
        }
    }

注意

Don't set jQuery.support.cors to true in your code.

Don't set jQuery.support.cors to true

SignalR handles the use of CORS. Setting jQuery.support.cors to true disables JSONP because it causes SignalR to assume the browser supports CORS.

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

JavaScript

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

注意

Normally you register event handlers before calling the start method to establish the connection.

详情可见here

关于javascript - SignalR 客户端连接到不同的服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56196661/

相关文章:

node.js - 用另一个函数替换一个函数进行测试

javascript - Typescript + Angular 公开服务方法

c# - Clickonce + .NET client profile 4 framework + offline

c# - 组合属性 setter "init and private"(C#9)

c# - 有没有办法简化这个开关盒?

javascript - .replace 在 js 中不起作用

javascript - 如何像单选按钮一样在一组 div 之间只选择一个 div?

javascript - 音位间隙的 Style.Transform

javascript - jquery.form.js 中的 .ajaxSubmit 不使用数据选项

node.js - 如何解决 npm lib 的问题?