【发布时间】: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