如何知道有多少客户正在使用网站 [英] How to know how many clients are using website

查看:77
本文介绍了如何知道有多少客户正在使用网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,需要知道有多少人在使用它.实际上,我需要一个计数器并为客户设置最大数量.例如,如果最大数量为3,则第4个客户端无法加载我的网站.

I have a website and need to know haw many people are using it. actually i need a counter and set a max number for clients. for example if max number is 3, then 4th client couldn''t load my website.

推荐答案

此处是执行此操作的示例.我是从Brij先生的文章中摘录的,所以如果它也有助于对该文章进行投票(下面是链接)

应用程序变量的经典示例可以是显示网站中的在线用户数.这可以通过以下步骤完成:

将Global.asax文件的在线计数器变量ApplicationStart方法添加为:

Here is the example of doing the same. i took it from Mr Brij''s article so if it helps do vote that article too(link is below)

A classic example of Application variable can be to show the number of online user in a website. This can be done in the following steps:

Add an online counter variable ApplicationStart method of Global.asax file as:

Application["OnlineCounter"] = 0;



因此,在这种情况下,将在应用程序首次启动时添加一个变量,并将其初始化为0,因为此时将没有登录用户.
现在我们知道,只要有新用户打开网站,就会创建一个新会话,并调用Global.asax的Session_Start方法.因此,我们可以使用以下方法增加计数器:



So in this, a variable will be added when the application first starts and will be initialized to 0 as there will be no logged in user at that point of time.
Now as we know whenever a new user opens the website, a new session is created and Session_Start method of Global.asax is called. So we can increase the counter in this method as:

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


我们应该使用锁,否则可能会得到错误的结果,因为这可能会同时更新,并且更新的数据不正确.方式:假设我们当前的Application ["OnlineCounter"]为5,同时,两个会话读取值5,并将其增量为6,然后对其进行更新.应用程序状态为6.因此,尽管有两个用户登录,但计数器仅增加了一个.因此,为避免这种情况,我们应该使用锁.
因此,在会话结束时,我们也应将其减少一.正如我已经讨论的,每当会话结束时都会触发一个Session_End事件.可以这样做:



We should use the Locks, else we may get the wrong result because this may be updated at the same time and updated data is not correct. How: Let''s say we currently have Application["OnlineCounter"] is 5 and at the same time, two sessions read the value 5 and make an increment to 6 and updated it. Application state as 6. So although two users are logged in, the counter is increased by one only. So to avoid this, we should use the locks.
So also at the time session ends, we should decrease it by one. As I already discussed, an event Session_End is fired whenever a session ends. So it can be done as:


void Session_End(object sender, EventArgs e)
      {
          // Code that runs when a new session is started
          if (Application["OnlineCounter"] != null)
          {
              Application.Lock();
              Application["OnlineCounter"] =
          ((int)Application["OnlineCounter"]) - 1;
              Application.UnLock();
          }
      }



并且可以在应用程序中的任何时间在整个应用程序中访问此值,如下所示:



And this value can be accessed throughout the application at any point of time in the application as:

if (Application["OnlineCounter"] != null)
            {
                int OnlineUsers = ((int)Application["OnlineCounter"]);
            }   



并且该值可以在应用程序中的任何位置使用.

P.S.文章链接:到应用程序状态的演练 [



and this value can be used anywhere in the application.

P.S. link of the article: A Walkthrough to Application State[^]


您可以通过使用应用程序状态来解决您的问题.
有关更多信息,请参见:-
到应用程序状态的演练 [
you can solve your problem by using Application State.
for more please see:-
A Walkthrough to Application State[^]

best of luck.


Global.asax 文件中编写与整个应用程序交互的代码.
示例代码如下:

Write code in Global.asax file which interacts with the entire application.
The sample code is given below:

void Application_OnStart(Object Sender, EventArgs E)
{
  Application["CurrentUsers"] = 0;
}
void Session_OnStart(object Sender, EventArgs E)
{
  Application.Lock();
  Application["CurrentUsers"] = System.Convert.ToInt32(Application["CurrentUsers"]) + 1;
  Application.UnLock();
}
void Session_OnEnd(object Sender, EventArgs E)
{
  Application.Lock();
  Application["CurrentUsers"] = System.Convert.ToInt32(Application["CurrentUsers"]) - 1;
  Application.UnLock();
}


您可以根据需要在此处更新/进行更改.

请参阅:
如何显示编号的在线用户访问者 [


You can update/make changes here as per your need.

Refer: How to show number of online users visitors[^]


这篇关于如何知道有多少客户正在使用网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆