国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET實現用戶在線檢測的類源碼

2024-07-10 12:59:00
字體:
來源:轉載
供稿:網友
//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) ;
}
}
}
}





收集最實用的網頁特效代碼!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇安县| 洛扎县| 鄯善县| 武夷山市| 称多县| 准格尔旗| 旺苍县| 白河县| 湘西| 台南县| 怀安县| 丽水市| 汕头市| 衡山县| 桐城市| 天镇县| 漠河县| 兰坪| 普兰店市| 奉化市| 白朗县| 南开区| 漯河市| 卓尼县| 波密县| 巩义市| 当雄县| 绥棱县| 通州区| 哈密市| 普陀区| 柳林县| 鄂伦春自治旗| 乾安县| 蚌埠市| 安徽省| 客服| 巩义市| 兰溪市| 朝阳县| 苍南县|