·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> ASP.NET实现在线人员实时显示

ASP.NET实现在线人员实时显示

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

asp.net实现在线人员实时显示

在最近的学习中,参考其他资源,做了一个简单的在线人员显示的功能,总结了一下,思路如下:

1、定义一个全局的内存来作为在线人员列表

2、通过实时判断用户session值,来判断某个用户的登录或离线

3、对于用户的上线和离线,通过添加用户到内存中,或删除内存中的用户列表中的用户来实现

下面是实现该功能的类:

  1 public static class UserOnline  2 {  3     /// <summary>  4     /// 获取或设置在线列表  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["OnlineUserList"]; 17         } 18         set 19         { 20             HttpContext.Current.Application["OnlineUserList"] = value; 21         } 22     } 23  24     /// <summary> 25     /// 添加在线成员 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     /// 添加在线成员 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     /// 离线删除用户 54     /// </summary> 55     public static bool OnlineUserList_Delete(string key) 56     { 57         bool re = false; 58         if (OnlineUserList.Contains(key)) 59         { 60             Hashtable userList = OnlineUserList; 61             userList.Remove(key); 62             OnlineUserList = userList; 63             return true; 64         } 65         return re; 66     } 67  68     /// <summary> 69     /// 判断用户是否在线 70     /// </summary> 71     public static bool UserIsOnline(string adminName) 72     { 73         OnlineClearUserOutTimeInOnLineList(); 74         return OnlineUserList.Contains(adminName) ? true : false; 75     } 76  77     /// <summary> 78     /// 删除超时在线用户 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             //删除超时 90             DateTime onlineTime = Convert.ToDateTime(de.Value); 91             TimeSpan timeSpan = DateTime.Now - onlineTime; 92  93             //在线时间和当前时间间隔大于超时分钟数就删除(注:用户非法关闭浏览器) 94             if (timeSpan.TotalMinutes >= (double)OnlineTimeOut) 95             { 96                 list.Remove(de.Key); 97             } 98  99         }100 101         OnlineUserList = list;102     }103 104 }

在用户登录成功的时候,添加改用户的惟一值到内存列表中

该用户的Session结束前进行删除即可。