counting online number of users of a web application
I am using ASP.Net + .Net 3.5 + VS2008 + IIS 7.0 + C# to develop a web application. I want to count how many users are online. This is my current implementation,
- when Session_Start is called, I will increase # of users online by 1;
- when Session_End is called, I will decrease # of users online by 1.
Two questions,
A Is that implementation correct?
B. Another question is, I think this method can not track # of users of real t开发者_StackOverflowime, since when user closes the browser, Session_End will not be called immediately (Session_End will be deferred to be called). Correct?
Are you using ASP.NET membership?
Membership.GetNumberOfUsersOnline()
You can customise the amount of time a user is considered to be "online" in web.config
- eg
<membership defaultProvider="..."
userIsOnlineTimeWindow="10"
<providers>
....
</providers>
</membership>
Read more here
Another problem with using Session_End is that it does not fire reliably, in fact if you use anything but inproc session state you cannot rely on this at all.
A better option is to use the the Membership provider, take a look at Membership.GetNumberOfUsersOnline()
Your implementation won't be very accurate - if a user logs on using two browsers your counter will increase by two.
A lot of sites just record logons and then do a count of the number of unique logons in the last x minutes to get an online total.
精彩评论