Count the online user list for. Net webform

Source: Internet
Author: User

. Net uses session for user State control (cookie is also acceptable, but sessionend events cannot be triggered on the server side, so the method mentioned here is based on session usage)

. Both net and ASP can use global. asax is used to set the program for the session end event. There are a lot of questions about using Global on the Internet. asax combines the application and session end events to calculate the number of online users, but I did not find detailed online user information.

Therefore, I used a solution to implement it and debug it successfully.

Idea: Use the session_end event to determine which user has expired, or click the user to exit and delete the user's information in the list, and then clear the session.

First, you need a public storage object on the server, such as SQL, text, XML, or application. For debugging convenience, I use application, in this storage object, you need to store an object with an appropriate dictionary, such as an array. Here I use hashtable. The advantage is that hashtable can be indexed by a key, you can use keys to delete, add, and perform other operations. This key is not only a digital index, but also any object you set, it is best to index the last activity time of the user to determine whether the user session expires.

In this hashtable, I set the key to the user's last time, and the value to the user's login name to facilitate the display and timeout comparison.

The session_end event in global. asax does not support any request object and response object, so it cannot read any information from the client. This is why Cookie is not used.

Although the session. Abandon () method will also trigger session_end, but the session_end method will be division only after all sessions are destroyed. At this time, it cannot be determined that the user triggered session_end. Therefore, you need to divide user actions into two types:
1: Click to exit the call session. Abandon ();
2: Expiration timeout;

In this way, the program is divided into two parts.

1. Click Exit: delete the list of users of the current session in the application, and then clear the session.
2. Expiration Timeout: The session_end event is triggered. In this event, compare the user information in all lists to determine the last activity time and the current time, and delete the expired event.

Core code:
Global. asax
========================================================== ========================================================== =
// When the application starts, an empty hashtable object is stored in the application.
Protected void application_start (Object sender, eventargs E)
{
Hashtable ht = new hashtable ();
Application ["username"] = HT;
}

// When the user's session ends, compare the user's last activity time with the current time to determine which needs to be deleted, and set the timeout time to the corresponding session timeout time in the Application
Protected void session_end (Object sender, eventargs E)
{
Hashtable ht = (hashtable) application ["username"];
Foreach (dictionaryentry item in HT)
{
If (datetime. parse (item. Key. tostring (). addseconds (20) <datetime. Now)
{
Ht. Remove (item. Key );
}
}
}
 

User Login, exit, and list display:
========================================================== ========================================================== ===
// Obtain the hashtable object in the storage object, obtain the list of online user information stored by the hashtable object, and output it to the page
Hashtable ht = (hashtable) application ["username"];
String STR = "userlist :";
Foreach (dictionaryentry item in HT)
{
STR + = "," + item. value;
}
Response. Write (STR );

// Log on to the user, record the session status, and add the user to the online user information list
Session ["username"] = username. text;
String STR = datetime. Now. tostring (); // identifies the user's activity time. You need to modify this time to the latest activity time of the user in other actions.
Session ["key"] = STR;
Hashtable ht = (hashtable) application ["username"];

Ht. Add (STR, username. Text );

// When a user clicks exit to clear the session status, the user's information is deleted from the online list, and the session status is cleared.
Hashtable ht = (hashtable) application ["username"];
Ht. Remove (session ["key"]. tostring ());

Session. Clear ();
Session. Abandon ();

 

The above is the main method, the method is very simple, and there can be a variety of processing methods, the general idea is this, But no matter which specific processing method, online statistics will consume a lot of server resources, it's not necessary.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.