ASP.NET實現使用者線上檢測的類源碼

來源:互聯網
上載者:User
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";
myDataColumn.ReadOnly = false;
myDataColumn.Unique = false;
_alluser.Columns.Add(myDataColumn);
}
}


//功能說明:將目前使用者加入線上列表
//如果該使用者的資料當前仍然在線上列表中,則暫時先不讓該使用者登陸,提示使用者存在
public bool AddUserToOnLine(User user)
{
#if DEBUG
(new SohoProject.SohoDebug()).WriteToDoc("開始進入<將目前使用者加入線上列表>....");
(new SohoProject.SohoDebug()).WriteToDoc("\r\n");
#endif


//開始搜尋是否已經存在該使用者,如果存在則是改變資料,否則添加新的使用者
string strExpr;
strExpr = "sessionid='" + user.sessionid + "'";
DataRow[] curUser;
// Use the Select method to find all rows matching the filter.
#if DEBUG
(new SohoProject.SohoDebug()).WriteToDoc("搜尋字串:" + strExpr);
(new SohoProject.SohoDebug()).WriteToDoc("\r\n");
#endif


curUser = _alluser.Select(strExpr);

#if DEBUG
(new SohoProject.SohoDebug()).WriteToDoc(strExpr);
(new SohoProject.SohoDebug()).WriteToDoc(curUser.Length.ToString());
#endif


if (curUser.Length >0 )
{
for(int i = 0; i < curUser.Length; i ++)
{
curUser[i]["curtime"]=DateTime.Now;
curUser[i]["iswhere"]=user.iswhere;
}
}
else
{
//直接加入新的資料
DataRow myRow;
try
{
myRow = _alluser.NewRow();
// Then add the new row to the collection.
myRow["name"] = user.name;
myRow["ip"] = user.ip;
myRow["iswhere"] = user.iswhere;
myRow["lasttime"] = user.lasttime;
myRow["curtime"] = DateTime.Now;
myRow["sessionid"] = user.sessionid;
_alluser.Rows.Add(myRow);
}
catch(Exception e)
{
throw(new Exception(e + "--------------------" + e.ToString())) ;
}
}
_alluser.AcceptChanges();
return true;
}



//功能說明:判斷某使用者是否線上,本部分暫時不用
//傳回值:TRUE代表線上,FALSE不在
public Boolean IsUserOnLine(string name)
{
//需要先判斷使用者是否已經在使用者列表中了
//開始搜尋是否已經存在該使用者,如果存在則是改變資料,否則添加新的使用者
string strExpr;
strExpr = "name ='" + name + "'";
DataRow[] curUser;
// Use the Select method to find all rows matching the filter.
curUser = _alluser.Select(strExpr);

if (curUser.Length >0 )
{
return true;
}
else
{
return false;
}
}

//功能說明:更新使用者線上時間
//傳回值:最新的線上使用者列表
public Boolean CheckUserOnLine(string name,string iswhere,string sessionid,string ip)
{
#if DEBUG
(new SohoProject.SohoDebug()).WriteToDoc("開始進入檢查使用者方法....");
(new SohoProject.SohoDebug()).WriteToDoc("");
#endif

//需要先判斷使用者是否已經在使用者列表中了
User newuser=new User();
newuser.name= name;
newuser.iswhere= iswhere;
newuser.lasttime=newuser.curtime=DateTime.Now;
newuser.sessionid=sessionid;
newuser.ip=ip;

OnLineUser alluser= new OnLineUser();
alluser.AddUserToOnLine(newuser);


#if DEBUG
(new SohoProject.SohoDebug()).WriteToDoc("離開檢查使用者方法....");
#endif

return true;
}
}

//定義線上使用者類
public class OnLineUser_old
{
private static ArrayList _alluser ; //定義使用者

public ArrayList alluser
{
get{return _alluser;}
set{_alluser=value;}
}

public OnLineUser_old() //建構函式
{
if(_alluser==null)
{
_alluser=new ArrayList();
}
}

//功能說明:將目前使用者加入線上列表
//如果該使用者的資料當前仍然在線上列表中,則暫時先不讓該使用者登陸,提示使用者存在
public bool AddUserToOnLine(User user)
{
//需要先判斷使用者是否已經在使用者列表中了
if(_alluser==null)
{
_alluser.Add(user);
return (true);
}
else
{
for ( int i = 0 ; i < _alluser.Count ; i ++)
{
//迴圈判斷使用者是否已經存在 SohoProject.User tempuser = (SohoProject.User)_alluser[i] ;

if( tempuser.sessionid.Equals(user.sessionid))
{
//更新使用者線上時間
tempuser.name=user.name;
tempuser.curtime=DateTime.Now;
tempuser.iswhere=user.iswhere;
tempuser.sessionid=user.sessionid;
tempuser.ip=user.ip;
alluser[i]=tempuser;
return(true);
//return(true); //使用者已經存在,則直接退出 }
}
_alluser.Add(user);
return (true);
}
}

//功能說明:判斷某使用者是否線上,本部分暫時不用
//傳回值:TRUE代表線上,FALSE不在
public Boolean IsUserOnLine(string name)
{
//需要先判斷使用者是否已經在使用者列表中了
if(_alluser==null)
{
return (false);
}
else
{
for ( int i = 0 ; i < _alluser.Count ; i ++)
{
//迴圈判斷使用者是否已經存在 SohoProject.User tempuser = (SohoProject.User)_alluser[i] ;
if(tempuser.name.ToLower().Equals(name.ToLower()))
{
return(true) ;
}
}
return (false);
}
}

//功能說明:更新使用者線上時間
//傳回值:最新的線上使用者列表
public Boolean CheckUserOnLine(string name,string iswhere,string sessionid,string ip)
{
//需要先判斷使用者是否已經在使用者列表中了
if(_alluser!=null)
{
User newuser=new User();
newuser.name= name;
newuser.iswhere= iswhere;
newuser.lasttime=newuser.curtime=DateTime.Now;
newuser.sessionid=sessionid;
newuser.ip=ip;

//OnLineUser alluser= new OnLineUser();
AddUserToOnLine(newuser);
}
return(false);
}
}



/*
下面開始建立守護線程類:
(註:此處,開始寫的時候本來想做成單件模式的,不過由於以前沒有做過這個東西,所以反而發生
了很多問題,最後決定放棄而使用現有的格式)
*/
public class CheckOnline
{
const int DELAY_TIMES = 10000 ; //定義執行的時間間隔為5秒
const int DELAY_SECONDS=60; //將使用者掉線時間設定為30秒

private Thread thread ; //定義內部線程
private static bool _flag=false; //定義唯一標誌

public CheckOnline()
{
if (!_flag)
{
_flag= true;
this.thread = new Thread(new ThreadStart(ThreadProc)) ;
thread.Name = "online user" ;
thread.Start() ;
}
}


internal void ThreadProc()
{
while(true)
{
// SohoProject.OnLineUser temp=new SohoProject.OnLineUser(); //定義一個使用者物件
// for (int i=0 ;i< temp.alluser.Count;i++)
// {
// User tmpuser=(User)temp.alluser[i];
// //我是將該使用者的最新時間加上30秒,然後和目前時間比較,小與目前時間,
// //則表示該使用者已經吊線,則刪除他的記錄
// if(tmpuser.curtime.AddSeconds(DELAY_SECONDS).CompareTo(DateTime.Now)<0)
// {
// temp.alluser.RemoveAt(i);
// }
// }



SohoProject.OnLineUser temp=new SohoProject.OnLineUser(); //定義一個使用者物件
//開始檢查是否有使用者到期了 string strExpr;
//tmpuser.curtime.AddSeconds(DELAY_SECONDS).CompareTo(DateTime.Now)<0
strExpr = "curtime < '" + DateTime.Now.AddSeconds( 0 - DELAY_SECONDS) + "'";
#if DEBUG
(new SohoProject.SohoDebug()).WriteToDoc(strExpr);
#endif

DataRow[] curUser;
// Use the Select method to find all rows matching the filter.
curUser = temp.alluser.Select(strExpr);

if (curUser.Length >0 )
{
//刪除這些記錄
for(int i = 0; i < curUser.Length; i ++)
{
curUser[i].Delete();
}
temp.alluser.AcceptChanges();
}
Thread.Sleep(DELAY_TIMES) ;
}
}
}
}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.