signalR核心打开连接两次

signalR core open connection twice

是否允许对同一个集线器创建两个连接?还是我应该使用相同的集线器连接?

var cnn1 = new signalR.HubConnection("/hub1");
cnn1.on("Method1", function(){});
cnn1.start().then(function() {cnn1.invoke("Something1");});

var cnn2 = new signalR.HubConnection("/hub1");
cnn2.on("Method2", function(){});
cnn2.start().then(function() {cnn2.invoke("Something2");});

您可以创建 2 个 HubConnection 实例。在这个简单的示例中,没有理由这样做,但我相信在某些情况下您会想要这样做。

旁注,您知道连接可以注册多个 .on() 方法吗? cnn1.on("Method1", function(){}); cnn1.on("Method2", function(){});

SignalR 2.0:多个集线器可以共享一个连接

是的,你可以做到。但要注意服务器(IIS 和浏览器)的限制

You can have multiple hubs sharing one connection on your site. SignalR 2.0 was updated to handle multiple hubs over one signlar connection with no lost in performance."

您在以下问题中找到了这个答案: Multiple signalR connections/hubs on your website

SignalR Core:不支持每个连接有多个集线器

The new version of SignalR does not support having more than one Hub per connection. This results in a simplified client API, and makes it easier to apply Authentication policies and other Middleware to Hub connections. In addition subscribing to hub methods before the connection starts is no longer required.

阅读更多关于 signalr 核心的信息: https://blogs.msdn.microsoft.com/webdev/2017/09/14/announcing-signalr-for-asp-net-core-2-0/

备注:

一旦我开始使用带有 signalr 2.0 的应用程序,我还计划在其中使用多个连接。但是由于浏览器的限制,我有一些奇怪的效果。浏览器确实限制了并行连接的数量。也许这不是您在同一个浏览器上打开多个浏览器实例的用例,但您应该知道这是背景。 如果您打开 10 个相同浏览器类型的实例,您可以对此进行测试。然后你可以看到并不是所有的实例都会创建连接。