分享
 
 
 

ASP.NET在线用户列表精确版

王朝asp·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

【原创作者】:丛兴滋(cncxz)[E-mail:cncxz@126.com]

【关 键 词】:xmlhttp ASP.NET在线用户列表 关闭浏览器 精确统计 单人登陆

【代码下载】:http://www.thisky.cn/down/onlineuser.rar

最近所做的一个项目需要用到的在线用户列表,上网搜索了一下发现现有的解决方案对用户意外退出的处理均不是太理想。一般来说,用户离开系统的方式有三种:主动注销、会话超时、直接关闭浏览器,对于前两种,我们很容易便可将该用户从在线列表中清除,关键是第三种(很多用户都是直接关闭窗口的~~郁闷ing),程序无法捕获窗口关闭的精确时间,只能等到会话超时后在能将该用户清除出在线列表,假设我们设置会话超时时间为60分钟,而用户登陆系统随便浏览一个页面就以关闭浏览器的方式退出的话,我们要在将近1小时后才能从在线列表中将该用户清除出去(想象一下,系统显示n多人在线,可能除了你之外其他的n-1人都关机走人了,汗一个先```),而本文将尝试寻找一个解决方案把这种尴尬降至最低。

我的大概思路是,给每在线用户增加一个RefreshTime属性,建立一个负责将当前用户的RefreshTime属性设置为当前时间的单独页面(Refresh.aspx),然后在系统的主要页面(也可以是所有页面)中通过xmlhttp不断地请求Refresh.aspx页面,一旦用户关闭了与本系统相关的所有窗口,即以直接关闭浏览器的方式退出系统,那么该用户的RefreshTime属性便不会自动更新了,我们再设置一个自动刷新的超时时间(这个要比会话超时短很多_refreshTimeout),当发现某用户超过_refreshTimeout的时间没有自动刷新,就能判定该用户已经以直接关闭浏览器的方式退出了。

假设我们设置会话超时时间为60分钟,自动刷新超时时间为1分钟,在客户端通过xmlhttp每隔25秒(之所以不设1分钟,是防止网速慢的时候访问Refresh.aspx超时,个人感觉,不一定正确)访问一次Refresh.aspx页面,在用户登陆、用户注销、检测用户是否在线的时候都执行清理超时用户(包括会话超时和自动刷新超时)操作,这样一来,在线用户列表的统计误差就由60分钟降至1分钟了。

==========================================

具体实现如下:

1、 新建一个名为ActiveUser的类,存储单个活动用户数据。

/// <summary>

/// 单个在线用户数据,无法继承此类。

/// </summary>

public sealed class ActiveUser

{

private readonly string _ticket; //票据名称

private readonly string _username; //登陆用户名

private readonly string _truename; //登陆用户名

private readonly string _roleid; //角色

private readonly DateTime _refreshtime; //最新刷新时间

private readonly DateTime _activetime; //最新活动时间

private readonly string _clientip; //登陆IP

public ActiveUser(string Ticket,string UserName,string TrueName,string RoleID,string ClientIP) {

this._ticket=Ticket;

this._username=UserName;

this._truename=TrueName;

this._roleid=RoleID;

this._refreshtime=DateTime.Now;

this._activetime=DateTime.Now;

this._clientip=ClientIP;

}

public ActiveUser(string Ticket,string UserName,string TrueName,string RoleID,DateTime RefreshTime,DateTime ActiveTime,string ClientIP) {

this._ticket=Ticket;

this._username=UserName;

this._truename=TrueName;

this._roleid=RoleID;

this._refreshtime=RefreshTime;

this._activetime=ActiveTime;

this._clientip=ClientIP;

}

public string Ticket { get{return _ticket;} }

public string UserName { get{return _username;} }

public string TrueName { get{return _truename;} }

public string RoleID { get{return _roleid;} }

public DateTime RefreshTime { get{return _refreshtime;} }

public DateTime ActiveTime { get{return _activetime;} }

public string ClientIP { get{return _clientip;} }

}

2、 新建一个名为PassPort的类,存储在线用户列表。

/// <summary>

/// 单个在线用户数据,无法继承此类。

/// </summary>

public sealed class ActiveUser

{

private readonly string _ticket; //票据名称

private readonly string _username; //登陆用户名

private readonly string _truename; //登陆用户名

private readonly string _roleid; //角色

private readonly DateTime _refreshtime; //最新刷新时间

private readonly DateTime _activetime; //最新活动时间

private readonly string _clientip; //登陆IP

public ActiveUser(string Ticket,string UserName,string TrueName,string RoleID,string ClientIP) {

this._ticket=Ticket;

this._username=UserName;

this._truename=TrueName;

this._roleid=RoleID;

this._refreshtime=DateTime.Now;

this._activetime=DateTime.Now;

this._clientip=ClientIP;

}

public ActiveUser(string Ticket,string UserName,string TrueName,string RoleID,DateTime RefreshTime,DateTime ActiveTime,string ClientIP) {

this._ticket=Ticket;

this._username=UserName;

this._truename=TrueName;

this._roleid=RoleID;

this._refreshtime=RefreshTime;

this._activetime=ActiveTime;

this._clientip=ClientIP;

}

public string Ticket { get{return _ticket;} }

public string UserName { get{return _username;} }

public string TrueName { get{return _truename;} }

public string RoleID { get{return _roleid;} }

public DateTime RefreshTime { get{return _refreshtime;} }

public DateTime ActiveTime { get{return _activetime;} }

public string ClientIP { get{return _clientip;} }

}

2、 新建一个名为PassPort的类,存储在线用户列表。

/// <summary>

/// 单个在线用户数据,无法继承此类。

/// </summary>

public sealed class ActiveUser

{

private readonly string _ticket; //票据名称

private readonly string _username; //登陆用户名

private readonly string _truename; //登陆用户名

private readonly string _roleid; //角色

private readonly DateTime _refreshtime; //最新刷新时间

private readonly DateTime _activetime; //最新活动时间

private readonly string _clientip; //登陆IP

public ActiveUser(string Ticket,string UserName,string TrueName,string RoleID,string ClientIP) {

this._ticket=Ticket;

this._username=UserName;

this._truename=TrueName;

this._roleid=RoleID;

this._refreshtime=DateTime.Now;

this._activetime=DateTime.Now;

this._clientip=ClientIP;

}

public ActiveUser(string Ticket,string UserName,string TrueName,string RoleID,DateTime RefreshTime,DateTime ActiveTime,string ClientIP) {

this._ticket=Ticket;

this._username=UserName;

this._truename=TrueName;

this._roleid=RoleID;

this._refreshtime=RefreshTime;

this._activetime=ActiveTime;

this._clientip=ClientIP;

}

public string Ticket { get{return _ticket;} }

public string UserName {&n

[1] [2] [3] [4] [5] [6] [7] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有