简体   繁体   English

计算匿名用户和登录用户-ASP.NET网站

[英]Counting anonymous and logged in users - ASP.NET website

I would like to implement reliable user counter. 我想实施可靠的用户计数器。 Currently I'm using session variables. 目前,我正在使用会话变量。 Here is a look on my implementation: 这是我的实现的外观:

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    Application["OnlineUsers"] = 0;
    Application["LoggedInUsers"] = 0;
}

void Session_Start(object sender, EventArgs e) 
{
    // Code that runs when a new session is started
    Application.Lock();
    Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
    Application.UnLock();
}

void Session_End(object sender, EventArgs e) 
{
    Application.Lock();
    if ((int)Application["OnlineUsers"] > 0)
    {
        Application["OnlineUsers"] = (int) Application["OnlineUsers"] - 1;
    }
    Application.UnLock();
}

protected void Login1_LoggedIn(object sender, EventArgs e)
{
    Application.Lock();
    Application["LoggedInUsers"] = (int)Application["LoggedInUsers"] + 1;
    Application.UnLock();
}

protected void LoginStatus1_LoggedOut(object sender, EventArgs e)
{
    Application.Lock();
    if ((int)Application["LoggedInUsers"] > 0)
    {
        Application["LoggedInUsers"] = (int) Application["LoggedInUsers"] - 1;
    }
    Application.UnLock();
}

Downsides of that approach are well known. 这种方法的缺点是众所周知的。 I couldn't find anything more accurate. 我找不到更准确的信息。 Can you help me, please? 你能帮我吗?

Given the somewhat fuzzy nature of Session_End, the approach I've used is to just define an "Online User" as someone who has had activity within the last 5 minutes. 鉴于Session_End的模糊性质,我使用的方法是将“在线用户”定义为最近5分钟内有活动的人。 You can then just count the number of users who have activity within that time frame. 然后,您可以只计算在该时间段内有活动的用户数。

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

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