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

5

我创建了以下项目:

  1. MVC应用 (已安装 -Install-Package Microsoft.AspNet.SignalR.JS) (参考 这里)

  2. Web服务 (//从Nuget Package窗口安装 //Install-Package Microsoft ASP.NET SignalR .NET Client //Install-Package Microsoft ASP.NET SignalR Core Components)

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

我的做法: 我正在调用MVC页面,并使用web服务处理一个任务。在web服务中,当任务正在处理时,我希望通过Signalr服务通知用户后台正在进行什么操作,例如任务正在处理或已完成。

我单独创建了所有的项目。

使用Web服务我调用signalr hubs(请见此处)

遇到的挑战: 我想向用户广播消息,如果有多个用户,则根据他们的角色发送消息。

enter image description here

编辑: 我的项目中有多个MVC应用程序和相应的Web服务,以及单个SignalR服务。因此,如何确定哪个MVC应用程序调用了对应的服务,并向所有或特定用户推送服务呢?例如Pusher将为应用程序创建应用程序ID并为用户创建令牌。这是可能的。


1
你只是通过MVC网站向该用户和给定角色中的其他用户广播吗?真正的挑战在于管理您的在线用户集合。从那里开始,如果我理解正确,确定用户是否在线,然后获取给定角色中的用户不应该很难。http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections - silencedmessage
嗨@silencedmessage,感谢您提供的链接。我已经检查过了,并正在处理内存/永久性、外部存储。在MVC中,我使用onConnectedonDisconnected方法来获取用户列表并相应地存储到内存/永久性数据库中。 - Prasad Phule
然而,我的新要求是不依赖于任何其他应用程序,如 MVC 或其他应用程序。因此,我需要将 onConnectedonDisconnected 方法移到 SignalR 服务中。现在找到“如何从 HTML(JS SignalR 代码)页面将 userid、connectionid 和 applicationId 发送到 SignalR 服务的 onConnected 方法”或其他解决方案? - Prasad Phule
@silencedmessage - 有关于这个问题的任何建议吗? - Zameer Ansari
1个回答

2

摘要:

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


工作流程:

首先,流程看起来更像这样: SignalR Workflow

管理客户端连接:

如果您正在使用内存中的方法来管理已连接的用户,则可以通过将连接 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 将为空。 在客户端上将连接 ID 存储在变量中也可能会有益,这样您可以稍后将其传递给您的方法。
        var connectionId;

        var testHub = $.connection.testHub;

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

中心:

中心可用于与Web服务通信。在此示例中,我将将其用作SOAP服务,但REST应该也可以正常工作。

    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服务:

执行长时间运行任务的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


谢谢回答。然而,我的未来改进是我的单个SignalR服务将被多个应用程序使用。我在问题中添加了额外的改进。请让我知道任何解决方案。你的解决方案对于一个应用程序非常好。 - Prasad Phule

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接