繁体   English   中英

SignalR服务:根据用户角色获取消息

[英]SignalR service: Get message depending on user role

我建立

  1. MVC应用程序 (已安装-Install-Package Microsoft.AspNet.SignalR.JS)(参见此处

  2. Web服务 (//从Nuget包窗口安装//安装包Microsoft ASP.NET SignalR .NET客户端//安装包Microsoft ASP.NET SignalR核心组件)

  3. 信号器服务 (已安装-Install-Package Microsoft.AspNet.SignalR.SelfHost和Install-Package Microsoft.Owin.Cors)

我在做什么:我正在调用MVC页面并在使用Web服务处理一个任务之后。 在任务正在处理的那个Web服务中,我想通知用户在处理过程中看到的任务正在进行什么,或者使用Signalr服务完成。

我分别创建了All项目。

使用Web服务我正在调用信号器集线器 (见这里

面临的挑战:我想向该用户广播该消息,如果没有。 用户是否在那里取决于我想要发送消息的角色。

在此输入图像描述

编辑 :我的项目中添加了额外的增强功能:我没有。 MVC应用程序及其相应的Web服务和我的单一signalR服务,所以我如何识别哪个MVC应用程序调用它相应的服务和服务推送给所有或它的应用程序用户或特定用户。 pusher一样,将为应用程序创建应用程序ID,为用户创建令牌数量。 有可能做到这一点。

摘要:

我不确定是否可以让集线器生活在WCF SignalR服务上。 最好让MVC项目充当客户端和Web服务之间的代理。 如果这是您的要求之一,您可以稍后与其他客户端(例如桌面客户端)连接到SignalR,并且还可以从Web服务连接到此集线器以将更新发送到指定组中的客户端和/或用户。


工作流程:

首先,流程看起来更像是: SignalR工作流程

管理客户端连接:

如果您使用内存中的方法来管理已连接的用户,那么您可以首先将连接ID​​和用户ID添加到您用来处理此问题的任何集合中。 例如:

    public static ConcurrentDictionary<String, String> UsersOnline = new ConcurrentDictionary<String, String>();

    public override System.Threading.Tasks.Task OnConnected()
    {
        UsersOnline.TryAdd(Context.ConnectionId, Context.User.Identity.GetUserId());
        return base.OnConnected();
    }

需要注意的是: 除非您在身份验证后映射SignalR,否则Context.User将为null。

将连接ID​​存储在客户端的变量中可能是有益的,因此您可以稍后将其传递给您的方法。

        var connectionId;

        var testHub = $.connection.testHub;

         $.connection.hub.start().done(function () {
            connectionId = $.connection.hub.id;
        }

枢纽:

集线器可用于与Web服务通信。 在这个例子中,我将它用作肥皂服务,但休息应该是相同的。

    public void LongRunningTask(String ConnectionId)
    {
        using (var svc = new Services.MyWebService.SignalRTestServiceClient())
        {
            svc.LongRunningTask(ConnectionId);
        } // end using
    } // end LongRunningTask

请注意,我们也将连接ID​​传递给服务。 当服务开始将消息发送回MVC项目以传递给客户端时,这就发挥作用。


监听器或Web API:

在MVC站点上设置侦听器控制器或Web API以从Web服务接收消息。

    public ActionResult SignalR(String Message, String Type, String ConnectionId)
    {
        if (!String.IsNullOrWhiteSpace(Message) && !String.IsNullOrWhiteSpace(Type) && !String.IsNullOrWhiteSpace(ConnectionId))
        {
            if (Type == "ShowAlert")
            {
                // Determine if the user that started the process is still online
                bool UserIsOnline = Hubs.TestHub.UsersOnline.ContainsKey(ConnectionId);

                // We need this to execute our client methods
                IHubContext TestHub = GlobalHost.ConnectionManager.GetHubContext<Hubs.TestHub>();
                if (UserIsOnline)
                {
                    // Show the alert to only the client that started the process.
                    TestHub.Clients.Client(ConnectionId).showAlert(Message);
                } // end if
                else
                {
                    List<String> UserIdsInRole = new List<String>();
                    using (var connection = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()))
                    {
                        // Assuming you're using Identity framework since it is an MVC project, get all the ids for users in a given role.
                        // This is using Dapper
                        UserIdsInRole = connection.Query<String>(@"
SELECT                  ur.UserId
FROM                    AspNetUserRoles ur
JOIN                    AspNetRoles r ON ur.RoleId = r.Id
WHERE                   r.Name = @rolename
", new { rolename = "SpecialRole" }).ToList();
                    } // end using

                    // Find what users from that role are currently connected
                    List<String> ActiveUsersInRoleConnectionIds = Hubs.TestHub.UsersOnline.Where(x => UserIdsInRole.Contains(x.Value)).Select(y => y.Key).ToList();

                    // Send the message to the users in that role who are currently connected
                    TestHub.Clients.Clients(ActiveUsersInRoleConnectionIds).showAlert(Message);
                } // end else (user is not online)
            } // end if type show alert
        } // end if nothing is null or whitespace
        return new HttpStatusCodeResult(200);
    } // end SignalR

网络服务:

执行长时间运行工作的Web服务方法也应该接受客户端ID,因此它可以将其发送回侦听器控制器或Web API。 它可以使用类似于此的方法(使用RestSharp )连接回MVC项目:

    public void ShowAlert(String Message, String ConnectionId)
    {
        RestClient Client = new RestClient("http://localhost:8888");
        RestRequest Request = new RestRequest("/Listener/SignalR", Method.POST);
        Request.Parameters.Add(new Parameter() { Name = "Message", Type = ParameterType.QueryString, Value = Message });
        Request.Parameters.Add(new Parameter() { Name = "Type", Type = ParameterType.QueryString, Value = "ShowAlert" });
        Request.Parameters.Add(new Parameter() { Name = "ConnectionId", Type = ParameterType.QueryString, Value = ConnectionId });
        IRestResponse Response = Client.Execute(Request);
    } // end Show Alert

演示:

我做了一个概念验证并将其上传到Github

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM