如何使用 signalr 向 asp.net 核心中的特定组发送消息?
How to send message to specific group in asp.net core using signalr?
在我的 asp.net 核心 3.1 应用程序中,我使用信号器发送消息,angular 用于 UI。
所以现在每个人都可以看到消息,我想发送消息只让合适的组织可以看到。
例如:我有 2 个组织,org1 和 org2。在 org1 中有 4 个用户,在 org2 中有 5 个用户。
我想发送消息给 ex:org1 用户已登录,并且应该只通知 4 个用户。
我也有 getCurrentOrgId。
我的 NotificationHub Class 看起来像:
public class NotificationHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
我正在使用 HubContext 发送消息:
private readonly IHubContext<NotificationHub> _hubContext;
await _hubContext.Clients.All.SendAsync("ReceiveMessage",$"{notificationToAdd.ActionMessage}", cancellationToken: cancellationToken);
// 我想要一些 organizationGroup 并仅发送消息 loggedinuser orgId == getCurrentOrgId。只有相应组织的用户才能看到通知 orgId2 用户不应看到 orgId1 用户通知。
这是带有群组的集线器的示例:
public class NotificationHub : Hub
{
private readonly UserManager<ApplicationUser> userManager;
public NotificationHub(UserManager<ApplicationUser> userManager)
{
this.userManager = userManager;
}
public async override Task OnConnectedAsync()
{
var user = await userManager.FindByNameAsync(Context.User.Identity.Name);
if (user != null)
{
if (user.UserType == UserType.Administrator)
{
await AddToGroup("Administrators");
}
else if (user.UserType == UserType.Employee)
{
await AddToGroup("Employees");
}
}
else
{
await Clients.Caller.SendAsync("ReceiveNotification", "Connection Error", 0);
}
}
public async Task SendNotification(string group, string message, int messageType)
{
await Clients.Group(group).SendAsync("ReceiveNotification", message, messageType);
}
public async Task AddToGroup(string groupName)
{
await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
}
public async Task RemoveFromGroup(string groupName)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);
}
}
我在 ApplicationUser
中有一个名为 UserType
的额外 enum
,我用它来将用户添加到连接组。
在我的 asp.net 核心 3.1 应用程序中,我使用信号器发送消息,angular 用于 UI。 所以现在每个人都可以看到消息,我想发送消息只让合适的组织可以看到。 例如:我有 2 个组织,org1 和 org2。在 org1 中有 4 个用户,在 org2 中有 5 个用户。 我想发送消息给 ex:org1 用户已登录,并且应该只通知 4 个用户。 我也有 getCurrentOrgId。
我的 NotificationHub Class 看起来像:
public class NotificationHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
我正在使用 HubContext 发送消息:
private readonly IHubContext<NotificationHub> _hubContext;
await _hubContext.Clients.All.SendAsync("ReceiveMessage",$"{notificationToAdd.ActionMessage}", cancellationToken: cancellationToken);
// 我想要一些 organizationGroup 并仅发送消息 loggedinuser orgId == getCurrentOrgId。只有相应组织的用户才能看到通知 orgId2 用户不应看到 orgId1 用户通知。
这是带有群组的集线器的示例:
public class NotificationHub : Hub
{
private readonly UserManager<ApplicationUser> userManager;
public NotificationHub(UserManager<ApplicationUser> userManager)
{
this.userManager = userManager;
}
public async override Task OnConnectedAsync()
{
var user = await userManager.FindByNameAsync(Context.User.Identity.Name);
if (user != null)
{
if (user.UserType == UserType.Administrator)
{
await AddToGroup("Administrators");
}
else if (user.UserType == UserType.Employee)
{
await AddToGroup("Employees");
}
}
else
{
await Clients.Caller.SendAsync("ReceiveNotification", "Connection Error", 0);
}
}
public async Task SendNotification(string group, string message, int messageType)
{
await Clients.Group(group).SendAsync("ReceiveNotification", message, messageType);
}
public async Task AddToGroup(string groupName)
{
await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
}
public async Task RemoveFromGroup(string groupName)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);
}
}
我在 ApplicationUser
中有一个名为 UserType
的额外 enum
,我用它来将用户添加到连接组。