★讨论★ 通过哈希表管理网站用户状态
原文请访问:
http://blog.csdn.net/wfyfngu/archive/2007/08/30/1765829.aspx
一个网站在线状态管理的类
适用于asp.net站点和jsp站点
本文示例代码采用C#
欢迎讨论
实现原理:首先设置一个允许的最大不活动时间(TimeOut),如果用户在这个时间之内和服务器通信,则刷新该用户的最后在线时间;如果用户在这个时间内没有和服务器通信,即最后的在线时间加 TimeOut 晚于当前时间,则判定用户已经离线
下面是C#代码
using System;
using System.Collections;
namespace Wfyfngu.Web.Status {
/// <summary>
/// 在线用户信息
/// </summary>
public class UserInfo {
private string _UserId;
private bool _OnLine = false; // 是否在线
private int _OnLineTime; // 在线时长
private DateTime _LastUpdateTime; // 最后联系服务器的时间
/// <summary>
/// 默认构造
/// </summary>
public UserInfo(){}
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <param name="onLine"></param>
/// <param name="onLineTime"></param>
/// <param name="lastUpdateTime"></param>
internal UserInfo(string userId, bool onLine, int onLineTime, DateTime lastUpdateTime) {
_UserId = userId;
_OnLine = onLine;
_OnLineTime = onLineTime;
_LastUpdateTime = lastUpdateTime;
}
// 属性
/// <summary>
/// 获取用户ID
/// </summary>
public string UserId {
get {return _UserId;}
}
/// <summary>
/// 获取用户是否在线
/// </summary>
public bool OnLine {
get {return _OnLine;}
}
/// <summary>
/// 获取用户在线时长
/// 单位分
/// </summary>
public int OnLineTime {
get {return _OnLineTime;}
}
/// <summary>
/// 获取用户最后联系服务器的时间
/// </summary>
public DateTime LastUpdateTime {
get {return _LastUpdateTime;}
}
}
/// <summary>
/// 用户在线状态管理类
/// </summary>
public class OnlineManage {
private int _TimeOut = 20;
private static Hashtable CachedUser = new Hashtable(100);
/// <summary>
/// 默认构造
/// 离线的默认时间为20分钟
/// </summary>
public OnlineManage(){}
/// <summary>
/// 指定离线时间的构造
/// </summary>
/// <param name="timeout">设置多少分用户不活动则表示离线[20,720]</param>
public OnlineManage(int timeout) {
if(timeout<20)
timeout = 20;
else if(timeout>720)
timeout = 720;
_TimeOut = timeout;
}
/// <summary>
/// 更新在线用户的在线信息
/// 如果用户不存在,则添加
/// </summary>
/// <param name="userId"></param>
public void UpdateOnlineInfo(string userId) {
int time = 0; // 该用户已经在线的时间
int ctime = 0; // 指定用户在列表中存贮的时间和当前时间的差值分钟
// 如果用户存在
if(CachedUser.ContainsKey(userId)) {
UserInfo ui = (UserInfo)CachedUser[userId];
time = ui.OnLineTime;
ctime = (DateTime.Now - ui.LastUpdateTime).Minutes;
CachedUser.Remove(userId);
}
CachedUser.Add(userId, new UserInfo(userId, true, ctime+time, DateTime.Now));
ClearOffline();
}
/// <summary>
/// 从列表中删除一个已经注册的用户
/// </summary>
/// <param name="userId">要删除的用户ID</param>
public void Remove(string userId) {
if(CachedUser.ContainsKey(userId)) {
CachedUser.Remove(userId);
}
}
/// <summary>
/// 根据用户ID返回一个在列表中注册的用户对象
/// </summary>
public UserInfo this[string userId] {
get {
if(CachedUser.ContainsKey(userId)) {
UserInfo ui = (UserInfo)CachedUser[userId];
return new UserInfo(userId,
ui.LastUpdateTime.AddMinutes(_TimeOut)-DateTime.Now > TimeSpan.Zero,
ui.OnLineTime, DateTime.Now);
}
return new UserInfo();
}
}
/// <summary>
/// 返回一个在列表中注册的用户对象
/// </summary>
public UserInfo this[int index] {
get {
int i=0;
foreach(object key in CachedUser.Keys) {
if(i++ == index) {
return (UserInfo)CachedUser[key];
}
}
return new UserInfo();
}
}
/// <summary>
/// 获取列表中注册的用户总数
/// </summary>
public int Count {
get {
return CachedUser.Count;
}
}
//
// 从列表中删除已经离线的用户
//
private void ClearOffline() {
foreach(object key in CachedUser.Keys) {
if(!((UserInfo)CachedUser[key]).OnLine)
CachedUser.Remove(key);
}
}
}
}
使用方法:
1. 添加一个用户到在线列表
OnlineManage myOnlineManage = new OnlineManage();
myOnlineManage.UpdateOnlineInfo("wfyfngu");
2. 检查一个用户是否在线
OnlineManage myOnlineManage = new OnlineManage();
bool onLine = myOnlineManage["wfyfngu"].OnLine;
3. 遍历所有在线用户
OnlineManage myOnlineManage = new OnlineManage();
for(int i=0; i<myOnlineManage.Count; i++) {
Response.Write(myOnlineManage[i].UserId+"<br/>");
}