简体   繁体   English

使用asp.net统计在线用户

[英]Counting online users using asp.net

I'm to create a website for which I need to count the online users and show it on the home page all the time. 我要创建一个网站,我需要统计在线用户并在主页上显示它。 I'm not interested to apply ready-to-use modules for it. 我对使用即用型模块不感兴趣。 Here is what I have already done: 这是我已经做过的事情:

Adding a Global.asax file to my project 将Global.asax文件添加到我的项目中

Writing the following code snippet in the Global.asax file: 在Global.asax文件中编写以下代码片段:

void Application_Start(object sender, EventArgs e) 
{
    Application["OnlineUsers"] = 0;
}

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

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

Actually it works fine but I have found the following bug: --> Even if the user close the browser, the real count of online users is not shown since the session timeout is still alive! 实际上它工作正常,但我发现了以下错误: - >即使用户关闭浏览器,由于会话超时仍然存在,因此未显示在线用户的实际数量!

Is there any solution but changing the session timeout interval? 是否有任何解决方案,但更改会话超时间隔?

The Session_End event fires when the server-side session times out, which is (default) 20 minutes after the last request has been served. 当服务器端会话超时时会触发Session_End事件,这是(默认)在最后一个请求被提供后20分钟。 The server does NOT know when the user "navigates away" or "closes the browser", so can't act on that. 服务器不知道用户何时“导航”或“关闭浏览器”,因此无法对此进行操作。

You could use onUserExit jQuery plugin to call some server side code and abandon the session. 你可以使用onUserExit jQuery插件来调用一些服务器端代码并​​放弃会话。 Activate onUserExit on document ready : 在文档就绪时激活onUserExit:

<script type="text/javascript">
  jQuery(document).ready(function () {

    jQuery().onUserExit({
      execute: function () {
        jQuery.ajax({
          url: '/EndSession.ashx', async: false
        });
      },
      internalURLs: 'www.yourdomain.com|yourdomain.com'
    });
  });
</script>

And in EndSession.ashx abandon the session and server side Session_End will be called : 并在EndSession.ashx中放弃会话和服务器端将调用Session_End:

public void ProcessRequest(HttpContext context)
{
  context.Session.Abandon();
  context.Response.ContentType = "text/plain";
  context.Response.Write("My session abandoned !");
}

note that this will not cover all cases (for example if user kills browser trough task manager). 请注意,这不会涵盖所有情况(例如,如果用户通过任务管理器杀死浏览器)。

不,需要时间才能更新,直到会话超时...

It's the limitation of this approach that server will think user is logged in unless the session ends actually; 这种方法的局限性在于服务器会认为用户已登录,除非会话实际结束; which will happen only when the number of minutes has passed as specified in the session timeout configuration. 只有在会话超时配置中指定的分钟数过后才会发生。

Check this post: http://forums.asp.net/t/1283350.aspx 查看这篇文章: http//forums.asp.net/t/1283350.aspx

Found this Online-active-users-counter-in-ASP-NET 在ASP-NET中找到了这个在线活动用户计数器

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

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