我有两个集线器类,
SystemNotificationHub.cs
public class SystemNotificationHub : Hub { }
QuotationChatHub.cs
public class QuotationChatHub: Hub { }
在CCD_ 2中定义了CCD_,当用户进入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());
});
};
});
}
});
通知.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());
});
});
}
});
据我所知,这个问题与代码中的if条件有关。
在创建连接生成器之前,您已检查连接是否为null。但所有两个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());
});
});
}
});