ASP.NET实现用户在线检测的类源码
ASP.NET实现用户在线检测的类源码
//online.cs(用户在线检测)
/*程序实现思路:
该用户有以下几个属性:
name:用户名
sessionID:用户ID,通过它唯一表示一个用户
iswhere :附加信息,用户当前所在位置
lasttime:用户登陆时间
curtime:本次刷新时间
在客户端,使用一个IFRAME,装载一个刷新页面,每隔XX秒更新一下他的名字对应的curtime,就表示他仍然在
在服务器端,建立一个守护线程,每隔固定时间就运行一遍,然后判断当前所有用户列表中的时间间隔是否超出了规定的时间,如果超出,则将该用户从在线列表中删除,这样就可以做到检测用户是否在线了,而如果再单独
写个用户离线后的处理,就可以解决好多人问到的:用户意外吊线后的处理。
*/
#define DEBUG
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections ;
using System.Threading ;
using System.Web;
using System.Diagnostics;
namespace SohoProject
{
//定义了一个结构
public struct User
{
public string name;
public DateTime lasttime;
public DateTime curtime;
public string sessionid;
public string ip;
public string iswhere;
}
public class OnLineUser
{
private static DataTable _alluser;
//只读属性
public DataTable alluser{
get{return _alluser;}
}
public OnLineUser()
{
if(_alluser==null)
{
//define user list
// Declare variables for DataColumn and DataRow objects.
_alluser = new DataTable("onlineuser");
DataColumn myDataColumn;
// Create new DataColumn, set DataType, ColumnName and add to DataTable.
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "name";
myDataColumn.AutoIncrement = false;
myDataColumn.Caption = "name";
myDataColumn.ReadOnly = false;
myDataColumn.Unique = false;
_alluser.Columns.Add(myDataColumn);
// Create sessionid column.
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "sessionid";
myDataColumn.AutoIncrement = false;
myDataColumn.Caption = "sessionid";
myDataColumn.ReadOnly = false;
myDataColumn.Unique = true;
_alluser.Columns.Add(myDataColumn);
// Create ip column.
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "ip";
myDataColumn.AutoIncrement = false;
myDataColumn.Caption = "ip";
myDataColumn.ReadOnly = false;
myDataColumn.Unique = false;
_alluser.Columns.Add(myDataColumn);
// Create iswhere column.
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "iswhere";
myDataColumn.AutoIncrement = false;
myDataColumn.Caption = "iswhere";
myDataColumn.ReadOnly = false;
myDataColumn.Unique = false;
_alluser.Columns.Add(myDataColumn);
// Create iswhere column.
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.DateTime");
myDataColumn.ColumnName = "lasttime";
myDataColumn.AutoIncrement = false;
myDataColumn.Caption = "lasttime";
myDataColumn.ReadOnly = false;
myDataColumn.Unique = false;
_alluser.Columns.Add(myDataColumn);
// Create iswhere column.
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.DateTime");
myDataColumn.ColumnName = "curtime";
myDataColumn.AutoIncrement = false;
myDataColumn.Caption = "curtime";