1,用户登陆后,在每一次加载新的页面时判断权限(这是常规操作) 的同时,把用户状态写入一张OnlineUser表里,如果发现没有这个用户,就添加进OnlineUser表。
2,在查询当前在线用户时,查询OnlineUser表即可。
3,用户退出程序时,加删除OnlineUser表中对应用户ID的记录。
4,用户非正常退出程序时,用DELETE FROM OnlineUser WHERE (DATEDIFF(ss, LastActTime, GETDATE()) > " + GlobalVar.Timeouts * 60 + ")的记录。
BasePage中刷新在线用户操作状态的代码如下:
1/// <summary>
2 /// 刷新在线用户
3 /// </summary>
4 private void RefreshOnlineUser()
5 {
6 string sUserID = Session["UserID"].ToString();
7 string sSQL = "";
8 sSQL = "SELECT ID FROM OnlineUser WHERE (UserID = '" + sUserID + "')";
9 DataTable dt = Query.ProcessSql(sSQL, GlobalVar.DBName);
10 if (dt.Rows.Count > 0)
11 {
12 string sID = dt.Rows[0]["ID"].ToString();
13 sSQL = "UPDATE OnlineUser SET LastActTime = '" + DateTime.Now.ToString() + "' WHERE (ID = '" + sID + "')";
14 Query.ProcessSql(sSQL, GlobalVar.DBName);
15 }
16 else
17 {
18 OnlineUserEntity sou = new OnlineUserEntity(); //此处用了听棠的SPL来做数据操作,具体可查看听棠.Net BLOG
19 sou.ID = Guid.NewGuid().ToString();
20 sou.UserID = sUserID;
21 sou.IPAddress = Request.UserHostAddress;
22 sou.Browser = Request.ServerVariables["HTTP_USER_AGENT"].ToString();
23 sou.StartTime = DateTime.Now;
24 sou.LastActTime = DateTime.Now;
25 sou.Save();
26 }
27 dt.Dispose();
28 sSQL = "DELETE FROM OnlineUser WHERE (DATEDIFF(ss, LastActTime, GETDATE()) > " + GlobalVar.Timeouts * 60 + ")";
29 Query.ProcessSql(sSQL, GlobalVar.DBName);
30 }
判断权限函数中部分涉及在线用户的代码:2 /// 刷新在线用户
3 /// </summary>
4 private void RefreshOnlineUser()
5 {
6 string sUserID = Session["UserID"].ToString();
7 string sSQL = "";
8 sSQL = "SELECT ID FROM OnlineUser WHERE (UserID = '" + sUserID + "')";
9 DataTable dt = Query.ProcessSql(sSQL, GlobalVar.DBName);
10 if (dt.Rows.Count > 0)
11 {
12 string sID = dt.Rows[0]["ID"].ToString();
13 sSQL = "UPDATE OnlineUser SET LastActTime = '" + DateTime.Now.ToString() + "' WHERE (ID = '" + sID + "')";
14 Query.ProcessSql(sSQL, GlobalVar.DBName);
15 }
16 else
17 {
18 OnlineUserEntity sou = new OnlineUserEntity(); //此处用了听棠的SPL来做数据操作,具体可查看听棠.Net BLOG
19 sou.ID = Guid.NewGuid().ToString();
20 sou.UserID = sUserID;
21 sou.IPAddress = Request.UserHostAddress;
22 sou.Browser = Request.ServerVariables["HTTP_USER_AGENT"].ToString();
23 sou.StartTime = DateTime.Now;
24 sou.LastActTime = DateTime.Now;
25 sou.Save();
26 }
27 dt.Dispose();
28 sSQL = "DELETE FROM OnlineUser WHERE (DATEDIFF(ss, LastActTime, GETDATE()) > " + GlobalVar.Timeouts * 60 + ")";
29 Query.ProcessSql(sSQL, GlobalVar.DBName);
30 }
if (Session["UserID"] == null)
{
this.ErrorMsg(GlobalVar.PE_LOGIN_TIME_OUT);
}
else
{
this.RefreshOnlineUser();
}
{
this.ErrorMsg(GlobalVar.PE_LOGIN_TIME_OUT);
}
else
{
this.RefreshOnlineUser();
}
其中GlobalValTimeouts 为自定义超时时间,GlobalVar.DBName为数据库,Query.ProcessSql是用听棠.Net的SPL持久层。
大家如有不同建议,请提出啊,我下一步想法是用AJAX来实现在线用户列出来。