简体   繁体   English

如何检查在线和离线用户asp.net

[英]How to check online and offline users asp.net

I am developing a Web TV and Video Streaming Portal and that can be accessed only by registered users. 我正在开发网络电视和视频流门户,只有注册用户才能访问。 Now I want to check the list of online/offline users saved in database. 现在,我要检查数据库中保存的在线/离线用户的列表。 A user don't need to move across the pages as by watching TV channel or video, so I think the last activity time would not be helpful. 用户不需要像看电视频道或视频那样在页面之间移动,因此我认为上一次活动时间不会有帮助。 Do I need to ping the mac address or something like this? 我需要ping Mac地址还是类似的东西? I searched across all the forums but still failed to find the suitable solution. 我在所有论坛中进行了搜索,但仍然找不到合适的解决方案。

Many developers tend to use 'Session_End' event to mark a user as 'Offline', but this may not be a good idea, because there are no one-to-one mappings between an ASP.Net session and a user. 许多开发人员倾向于使用'Session_End'事件将用户标记为“脱机”,但这并不是一个好主意,因为ASP.Net会话和用户之间没有一对一的映射。 For example if a user opens 2 separate instances of Internet Explorer (eg 2 separate IE processes) on his workstation, he can have 2 ASP.Net sessions open. 例如,如果用户在其工作站上打开2个Internet Explorer的单独实例(例如2个单独的IE进程),则他可以打开2个ASP.Net会话。 The 'Session_End' event of one of the session does not necessarily mean that the user is offline, they can still be using the website. 会话之一的'Session_End'事件并不一定意味着用户处于脱机状态,他们仍然可以使用网站。

In addition, 'Session_End' event is only available in the 'InProc' session mode. 此外, 'Session_End'事件仅在“ InProc”会话模式下可用。 If you are trying to store session states in the State Server or SQL Server, 'Session_End' event, it will never fire. 如果您尝试将会话状态存储在状态服务器或SQL Server中,则“ Session_End”事件将永远不会触发。

I would suggest using the last visiting time as the ideal approach. 我建议使用上次访问时间作为理想的方法。 Membership helps build the user database and there is a convenient approach to get it via Membership.GetUser(stringuserName).IsOnline. 成员资格有助于构建用户数据库,并且有一种方便的方法可通过Membership.GetUser(stringuserName).IsOnline.来获取它Membership.GetUser(stringuserName).IsOnline. If you don't use Membership, you can use the method below which uses the same principle in Membership: 如果不使用成员资格,则可以使用下面的方法,该方法在成员资格中使用相同的原则:

In the application, you can define a global dictionary membershipDictionary to store the information about users online and offline. 在应用程序中,您可以定义一个全局词典MembershipDictionary来存储有关在线和离线用户的信息。

Use an AJAX Timer (or simply XmlHttpRequest in client side JavaScript) in the pages to update theLastVisitTime of the user and store it into membershipDictionary at intervals. 在页面中使用AJAX计时器(或客户端JavaScript中的XmlHttpRequest )来更新用户的LastVisitTime并将其间隔存储到MembershipDictionary中。 From time to time, visit the dictionary membershipDictionary to filter out the idle users (who have not visited the page for a long time) and also retrieve information on how many users are still online. 时不时访问字典的membershipDictionary过滤掉闲置的用户(很长时间没有访问该页面的用户),并检索仍然有多少用户在线的信息。 Regular maintenance can be done in a separate thread or a server-based timer. 定期维护可以在单独的线程中或基于服务器的计时器中完成。 Please also be aware of thread synchronization issues. 还请注意线程同步问题。

SignalR would possibly be a fit for this operation. SignalR可能适合此操作。

You could have a hub, which all users connect to, and then every user would call a method on the hub every now and then (the appropriate interval would be subjective). 您可能有一个集线器,所有用户都连接到该集线器,然后每个用户都会不时地在集线器上调用一个方法(适当的间隔是主观的)。

You would need to join every user to a group, and when the user dont poll within the interval, you'll remove the user from the group, and mark the user as offline. 您需要将每个用户加入一个组,并且当该用户不在该时间间隔内进行轮询时,您将从该组中删除该用户,并将该用户标记为脱机。

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

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