ASP. NET enables real-time display of online personnel and asp.net

Source: Internet
Author: User

ASP. NET enables real-time display of online personnel and asp.net

In my recent study, I made a simple online staff display function by referring to other resources and summarized the following ideas:

1. define a global memory as an online staff list

2. determine the user's Session value in real time to determine whether a user is logged on or offline.

3. for online and offline users, add users to the memory or delete users in the user list in the memory.

The following are the classes that implement this function:

1 public static class UserOnline 2 {3 /// <summary> 4 // get or set the online list 5 /// </summary> 6 public static Hashtable OnlineUserList 7 {8 get 9 {10 if (HttpContext. current. application ["OnlineUserList"] = null) 11 {12 Hashtable onlineUserList = new Hashtable (); 13 HttpContext. current. application ["OnlineUserList"] = onlineUserList; 14} 15 16 return (Hashtable) HttpContext. current. application ["OnlineUse RList "]; 17} 18 set 19 {20 HttpContext. current. application ["OnlineUserList"] = value; 21} 22} 23 24 // <summary> 25 // Add online member 26 /// </summary> 27 public static bool OnlineUserList_Add (string key, string value) 28 {29 try 30 {31 if (OnlineUserList. contains (key) 32 OnlineUserList [key] = value; 33 else 34 OnlineUserList. add (key, value); 35 return true; 36} 37 catch 38 {39 return false; 40} 41} 42 43 // <summary> 44 // Add an online member 45 // </summary> 46 public static bool OnlineUserList_Add (string key) 47 {48 string value = DateTime. now. toString (); 49 return OnlineUserList_Add (key, value ); 50} 51 52 // <summary> 53 // delete user 54 offline /// </summary> 55 public static bool OnlineUserList_Delete (string key) 56 {57 bool re = false; 58 if (OnlineUserList. contains (key) 59 {60 Hashtable user List = OnlineUserList; 61 userList. remove (key); 62 OnlineUserList = userList; 63 return true; 64} 65 return re; 66} 67 68 // <summary> 69 // determine whether the user is online 70 // </summary> 71 public static bool UserIsOnline (string adminName) 72 {73 OnlineClearUserOutTimeInOnLineList (); 74 return OnlineUserList. contains (adminName )? True: false; 75} 76 77 // <summary> 78 // deletion timeout online user 79 // </summary> 80 public static void OnlineClearUserOutTimeInOnLineList () 81 {82 int OnlineTimeOut = 20; 83 Hashtable list = new Hashtable (); 84 Hashtable temList = new Hashtable (); 85 list = OnlineUserList; 86 temList = new Hashtable (list ); 87 foreach (DictionaryEntry de in temList) 88 {89 // Delete timeout 90 DateTime onlineTime = Convert. toDateTime (de. value); 91 TimeSpan timeSpan = DateTime. now-onlineTime; 92 93 // Delete when the online time and current time interval are greater than the timeout in minutes (note: the browser is closed illegally) 94 if (timeSpan. totalMinutes> = (double) OnlineTimeOut) 95 {96 list. remove (de. key); 97} 98 99} 100 101 OnlineUserList = list; 102} 103 104}

When the user logs on successfully, add the user's unique value to the memory list.

Delete the user's Session before it ends.

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.