【问题标题】:SignalR service: Get message depending on user roleSignalR 服务:根据用户角色获取消息
【发布时间】:2015-07-08 13:56:00
【问题描述】:

我创造了

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

  2. 网络服务 (//从 Nuget 包窗口安装 //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 服务处理一项任务之后。在该网络服务中,当任务正在处理时,我想通知用户正在处理的类似任务背后发生了什么,或者它是使用 Signalr 服务完成的。

我单独创建了所有项目。

使用网络服务,我将其称为 signalr hubs(请参阅 here

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

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

【问题讨论】:

  • 您是否仅通过 MVC 站点向该用户和给定角色中的其他用户广播?真正的挑战是管理您的在线用户集合。从那里,如果我理解正确,确定用户是否在线然后让用户担任给定角色应该不难。 asp.net/signalr/overview/guide-to-the-api/…
  • 嗨@silencedmessage 感谢您的链接。我已经检查过它并在内存/永久,外部存储上工作。 onConnectedonDisconnected 方法我在 MVC 中使用来获取用户列表并相应地存储到内存/永久数据库。
  • 但是我的新要求是不要依赖任何其他应用程序,例如 MVC 或其他应用程序。所以我需要将onConnected and onDisconnected 方法移至信号​​器服务。现在找到“如何从 html(JS 信号器代码)页面将 userid connectionid 和 applicationId 发送到信号器服务中的 onConnected 方法”的解决方案或任何其他解决方案?
  • @silencedmessage - 对this的任何建议

标签: javascript c# asp.net-mvc signalr signalr-hub


【解决方案1】:

总结:

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


工作流程:

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

管理客户端连接:

如果您使用内存中的方法来管理连接的用户,那么您可以首先将连接 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();
    }

请注意:The Context.User will be null unless you map SignalR after the authentication.

将连接 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

【讨论】:

  • 感谢 ans,但是我未来的改进是我的单个 signalR 服务将被应用程序数量使用。我在问题中添加了额外的增强功能。请让我知道任何解决方案。它是一个应用程序的绝佳解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
  • 2014-08-18
  • 1970-01-01
  • 2020-01-14
  • 2021-06-27
  • 2014-10-22
  • 1970-01-01
相关资源
最近更新 更多