在 .net 核心中使用 Signalr 向单个用户和管理员角色用户发送通知
Notification sending to single user and to admin role users using Signalr in .net core
基本上我做了一个集线器,它在存储库中发生某些操作时发送通知。我想将其更改为 signle User sending ,例如,如果该用户发生了操作,则发送通知或将所有通知发送给管理员角色用户。
现在我只有一个集线器,我使用
await _hubContext.Clients.All.SendAsync("displayNotification", "");
并在html侧
const connection = new signalR.HubConnectionBuilder()
.withUrl("/NotificationHub")
.configureLogging(signalR.LogLevel.Information)
.build();
connection.on('displayNotification', () => {
getNotification()
toastr.info("You hava a new Notification");
}
);
async function start() {
try {
await connection.start();
console.log("connected");
} catch (err) {
console.log(err);
setTimeout(() => start(), 5000);
}
};
connection.onclose(async () => {
await start();
});
start();
I want to change this to signle User sending , for example to send notificaiton if action happened on that user or to send all notifications to the admin role users.
当我们从集线器 class 外部调用集线器方法时,没有 caller
与调用关联。因此,我们不能使用 Clients.Caller.SendAsync("ReceiveMessage", user, message)
之类的代码片段向调用方返回消息。
要实现向 specific/signle 用户发送消息的要求,您可以创建单用户组(一个针对单用户的组),然后在您只想到达该用户时向该组发送消息特定用户。
并且您还可以将所有管理员用户添加到一个组中,这样您就可以通过向该组发送消息来向所有管理员用户推送通知。
await _hubContext.Clients.Group(groupName_Here).SendAsync("ReceiveMessage", $"{message_here}");
有关“SignalR 中的组”的更多信息,请查看:https://docs.microsoft.com/en-us/aspnet/core/signalr/groups?view=aspnetcore-5.0#groups-in-signalr
此外,为了达到同样的要求,如@Prolog 提到的,您还可以将用户映射到 ConnectionId(s),然后使用以下代码片段将消息发送到特定连接的客户端。
await _hubContext.Clients.Clients(connectionIds_here).SendAsync("ReceiveMessage", $"{message_here}");
基本上我做了一个集线器,它在存储库中发生某些操作时发送通知。我想将其更改为 signle User sending ,例如,如果该用户发生了操作,则发送通知或将所有通知发送给管理员角色用户。
现在我只有一个集线器,我使用
await _hubContext.Clients.All.SendAsync("displayNotification", "");
并在html侧
const connection = new signalR.HubConnectionBuilder()
.withUrl("/NotificationHub")
.configureLogging(signalR.LogLevel.Information)
.build();
connection.on('displayNotification', () => {
getNotification()
toastr.info("You hava a new Notification");
}
);
async function start() {
try {
await connection.start();
console.log("connected");
} catch (err) {
console.log(err);
setTimeout(() => start(), 5000);
}
};
connection.onclose(async () => {
await start();
});
start();
I want to change this to signle User sending , for example to send notificaiton if action happened on that user or to send all notifications to the admin role users.
当我们从集线器 class 外部调用集线器方法时,没有 caller
与调用关联。因此,我们不能使用 Clients.Caller.SendAsync("ReceiveMessage", user, message)
之类的代码片段向调用方返回消息。
要实现向 specific/signle 用户发送消息的要求,您可以创建单用户组(一个针对单用户的组),然后在您只想到达该用户时向该组发送消息特定用户。
并且您还可以将所有管理员用户添加到一个组中,这样您就可以通过向该组发送消息来向所有管理员用户推送通知。
await _hubContext.Clients.Group(groupName_Here).SendAsync("ReceiveMessage", $"{message_here}");
有关“SignalR 中的组”的更多信息,请查看:https://docs.microsoft.com/en-us/aspnet/core/signalr/groups?view=aspnetcore-5.0#groups-in-signalr
此外,为了达到同样的要求,如@Prolog 提到的,您还可以将用户映射到 ConnectionId(s),然后使用以下代码片段将消息发送到特定连接的客户端。
await _hubContext.Clients.Clients(connectionIds_here).SendAsync("ReceiveMessage", $"{message_here}");