基于角色的通知,以及在 MVC 5 中使用 Signalr 的组

Role Based Notification, Along with Groups using Signalr in MVC 5

我是 signalR 的初学者,需要制作一个应用程序,其中有很多用户有很多角色,并且应该有三个向客户端发送通知的渠道 1. Public(全部使用) 2. Private(发送通知给单身人士) 3. 组和子组(向属于某个组或子组的成员发送通知)

问题是我无法理解 SignalR 中的用户差异,也没有理解组的概念。

谁能指导一下

首先,您可以开始阅读 Microsoft documentation about the groups. Then you can read the authorization documentation,这样您就可以为每个角色创建组和管理用户。

您可以做的是,当客户端连接到 Hub 时,假设您知道用户角色(使用上下文和授权),您将把他们添加到这 3 个组中。

然后向这些组发送消息就很容易了,在 Whosebug 和互联网上到处都有这样的例子。

希望对您有所帮助。

代码示例:

/// <summary>
/// Called when a new connection is established with the hub.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public override async Task OnConnectedAsync()
{
    // 1. Add the use to the public group

    await this.Groups.AddToGroupAsync( this.Context.ConnectionId, "PublicGroup");

    // 2. Add user to the private channel, single person

    await this.Groups.AddToGroupAsync(this.Context.ConnectionId, this.Context.User.Identity.Name);

    if (this.Context.User.IsInRole("Admin"))
    {
        // 3. Add the user to the Admin group

        await this.Groups.AddToGroupAsync("Admin", this.Context.ConnectionId);   
    }

    // add to other groups...

    await base.OnConnectedAsync();
}