Counting online users using asp.net

前端 未结 4 1933
星月不相逢
星月不相逢 2020-12-11 20:22

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

相关标签:
4条回答
  • 2020-12-11 20:46

    No, It will take time to update until session is timedout...

    0 讨论(0)
  • 2020-12-11 20:48

    You could use onUserExit jQuery plugin to call some server side code and abandon the session. Activate onUserExit on document ready :

    <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 :

    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).

    0 讨论(0)
  • 2020-12-11 20:56

    The Session_End event fires when the server-side session times out, which is (default) 20 minutes after the last request has been served. The server does NOT know when the user "navigates away" or "closes the browser", so can't act on that.

    0 讨论(0)
  • 2020-12-11 21:03

    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

    Found this Online-active-users-counter-in-ASP-NET

    0 讨论(0)
提交回复
热议问题