//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) ; } } } }
|