【问题标题】:SignalR Multiple Hub Connection .NET CoreSignalR 多集线器连接 .NET Core
【发布时间】:2020-10-26 12:05:31
【问题描述】:

我有 2 个 Hub 课程,

SystemNotificationHub.cs

public class SystemNotificationHub : Hub { }

QuotationChatHub.cs

public class QuotationChatHub: Hub { }

SystemNotificationHub_Layout.cshtml 中定义,因此用户不断连接到集线器, 当用户进入QuotationChat.cshtml页面时,我也希望同一个用户连接QuotationChatHub,所以以一种简单的方式,我希望用户同时连接多个集线器。

我不能让用户同时连接多个集线器。我怎样才能做到这一点?

启动端点配置

endpoints.MapHub<SystemNotificationHub>("/systemNotificationHub");

endpoints.MapHub<QuotationHub>("/quotationHub");

quotationChat.js

$(function () {
    if (connection === null) {
        connection = new signalR.HubConnectionBuilder()
            .withUrl("/quotationHub")
            .build();

        connection.start().then(function () {
            document.getElementById('sendButton').onclick = function () {
                connection.invoke("BroadcastFromClient")
                    .catch(function (err) {
                        return console.error(err.toString());
                    });
            };
        });
    }
});

notification.js


$(function () {
    if (connection === null) {
        connection = new signalR.HubConnectionBuilder()
            .withUrl("/systemNotificationHub")
            .build();

        connection.on("Notify", function (response) {

        });

        connection.on("HubError", function (response) {
            alert(response.error);
        });

        connection.start().then(function () {
            connection.invoke("NotificationMethod")
                .catch(function (err) {
                    return console.error(err.toString());
                });
        });
    }
});

【问题讨论】:

  • 好的 - 那么你的问题到底是什么? - 尝试用实际的问号将其作为一个单数问题,以便我们清楚地了解您的需求
  • 你在这里只描述了你的情况。请添加一个实际问题
  • 我已经编辑了问题,只是我不能让用户同时连接超过 1 个集线器。只有 1 个 Hub 是用户连接的第一个 Hub 正在工作,quotationHub 在用户进入该特定页面时不工作。
  • 其实我看到,在编辑之前,他只是忘记了一个问号,而另一方面他说他不能同时将用户连接到 2 个集线器的陈述是可以的。
  • 所以基本上,如果我理解正确,您希望能够在不同集线器上建立连接时断开连接?如果是这样,只需向集线器管理器触发一个事件,该事件将断开客户端与其他集线器的连接......

标签: asp.net asp.net-core asp.net-core-signalr


【解决方案1】:

据我所知,此问题与您代码中的 if 条件有关。

在创建连接构建器之前,您已检查连接是否为空。但是两个js都使用相同的连接模型。

为了解决这个问题,我建议你可以尝试为systemNotificationHub创建一个新的连接,例如connection1,然后你的代码就可以正常工作了。

更多细节,您可以参考以下代码:

quotationChat.js 没有改变。

notification.js:

//Define a new connection1 as the new js object as connection
$(function () {
    if (connection1 === null) {
        connection1 = new signalR.HubConnectionBuilder()
            .withUrl("/systemNotificationHub")
            .build();

        connection1.on("Notify", function (response) {

        });

        connection1.on("HubError", function (response) {
            alert(response.error);
        });

        connection1.start().then(function () {
            connection.invoke("NotificationMethod")
                .catch(function (err) {
                    return console.error(err.toString());
                });
        });
    }
});

【讨论】:

    猜你喜欢
    • 2019-10-07
    • 2020-08-17
    • 2020-08-24
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 2019-10-09
    相关资源
    最近更新 更多