SignalR将通知推送到单个经过身份验证的用户的所有浏览器实例 [英] SignalR push notifications to all browser instances of single authenticated user

查看:335
本文介绍了SignalR将通知推送到单个经过身份验证的用户的所有浏览器实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个需要在收到新通知时通知用户的应用程序。该应用程序将托管在Azure上,并且将是.NET MVC 3(可能不重要,但仅供参考)。我还

I'm working on an application that needs to notify the user when they receive a new notification. The application will be hosted on Azure and will be .NET MVC 3 (probably not important, but FYI). I also

通知保存在MSSQL数据库(2008)中,并且有一个 IsRead 字段。通知是从各种用户和服务器操作生成的。根据用户首选项,将使用 IsRead == false 创建一些通知。

Notifications are saved in an MSSQL db (2008) and have an IsRead field. Notifications are generated from various user and server actions. Based on user preferences, some notifications will be created with IsRead == false.

我有两种不同的方案,我觉得SignalR在处理方面会很完美,我只需要一些指导:

I have 2 different scenarios that I feel that SignalR will be perfect in handling, I just need a little guidance:


  1. 用户有几个窗口打开浏览网站和一个新的添加通知行,分配给该用户, IsRead == true

    • 我想要所有特定用户的浏览器实例中有一个特定的js函数被调用以显示弹出的通知


  • 我希望所有特定用户的浏览器实例都有一个特定的js函数来调用以删除弹出的通知。 (只有处理点击的特定浏览器实例也会执行js函数来显示通知详细信息)



我尝试了什么






对于第一个场景,我认为同样的动作是导致通知被写入数据库将负责通知所有用户的浏览器实例。

What I've tried


For the first scenario, I think that the same action that causes the notification to be written to the database will have the responsibility of notifying all the user's browser instances.

第二种情况类似于点击弹出窗口,将通过Ajax调用服务器端点,该端点将更新数据库以将通知标记为已读,然后通知所有其他用户的浏览器实例。

The second scenario would be similar in that by clicking on the pop up, a server endpoint would be called via Ajax and that endpoint would update the db to mark the notification as read, and then notify all the other user's browser instances.

如果有是一种基于ASP.NET Forms Authentication Username跟踪SignalR客户端的方法,然后我认为这将有助于解决两种情况下的问题。

If there is a way to keep track of SignalR Clients based on ASP.NET Forms Authentication Username, then I think that will work in solving the problems in both scenarios.

我的主要问题是我不知道如何使用SignalR向所有特定用户的浏览器实例发出信号。我正在考虑根据ASP.NET Forms Authentication Username创建一个组。这会有用吗?是否有一种更简单的方法可以内置到SignalR中来处理这个问题?

My main problem is that I don't know how to signal all of one specific user's browser instances using SignalR. I was thinking about making a group based on the ASP.NET Forms Authentication Username. Would this work? Is there an easier way that is built into SignalR that handles this?

推荐答案

你打电话给一个用户名为关键是好的,我认为这是最快的实施。您可以通过订阅以下内容每次加载页面时调用函数(并且 ConnectionId 已更改):

Your call to make a Group with the User Name as a key is good one and I think it's the most fast to implement. You can call a function every time a page loads(and ConnectionId is changed) by subscribing to the:

$.connection.hub.start(function () {
    yourHub.register();
});

在代码后面的中心:

public void Register()
{
    AddToGroup(username);
}

当您要刷新客户的屏幕时,您可以:

And when you want to refresh the client's screens you do:

Clients[username].refreshClientScreen();

其中 refreshClientScreen()是你的javascript函数,它完成了隐藏的工作浏览器中的通知。

Where refreshClientScreen() is your javascript function which does the job of hiding the notification in the browser.

另一种方法是检查每个请求上的 ConnectionId ,并有一个注册和保留的位置用户在服务器上的连接。当您需要更新它们时,您可以在每个代码上使用此代码:

Another way to do it is to check the ConnectionId on every request and have a place where you register and persist the user's connections on the server. When you need to update them you can use this code on each one of them:

Clients[ConnectionId].refreshClientScreen();

您可以在上下文中找到 ConnectionId 集线器的对象。

You can find the ConnectionId in the Context object of a Hub.

我认为第二种方法难以实现,因为您必须管理保存 ConnectionId -s的位置并订阅Disconnect通过实施IDisconnect实现事件,这样您就可以使 ConnectionId -s列表保持最新状态,因此我选择第一种方法 - 带有组的方法。

I think the second approach is harder to implement because you have to manage where you'd save the ConnectionId-s and subscribe to the Disconnect event by implementing IDisconnect so you can keep your ConnectionId-s list up to date, so I'd choose the first approach - the one with the groups.

这篇关于SignalR将通知推送到单个经过身份验证的用户的所有浏览器实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆