==========================================
具體實現(xiàn)如下:
1、 新建一個名為activeuser的類,存儲單個活動用戶數(shù)據(jù)。
/// <summary>
/// 單個在線用戶數(shù)據(jù),無法繼承此類。
/// </summary>
public sealed class activeuser
{
private readonly string _ticket; //票據(jù)名稱
private readonly string _username; //登陸用戶名
private readonly string _truename; //登陸用戶名
private readonly string _roleid; //角色
private readonly datetime _refreshtime; //最新刷新時間
private readonly datetime _activetime; //最新活動時間
private readonly string _clientip; //登陸ip
public activeuser(string ticket,string username,string truename,string roleid,string clientip) {
this._ticket=ticket;
this._username=username;
this._truename=truename;
this._roleid=roleid;
this._refreshtime=datetime.now;
this._activetime=datetime.now;
this._clientip=clientip;
}
public activeuser(string ticket,string username,string truename,string roleid,datetime refreshtime,datetime activetime,string clientip) {
this._ticket=ticket;
this._username=username;
this._truename=truename;
this._roleid=roleid;
this._refreshtime=refreshtime;
this._activetime=activetime;
this._clientip=clientip;
}
public string ticket { get{return _ticket;} }
public string username { get{return _username;} }
public string truename { get{return _truename;} }
public string roleid { get{return _roleid;} }
public datetime refreshtime { get{return _refreshtime;} }
public datetime activetime { get{return _activetime;} }
public string clientip { get{return _clientip;} }
}
2、 新建一個名為passport的類,存儲在線用戶列表。
/// <summary>
/// 單個在線用戶數(shù)據(jù),無法繼承此類。
/// </summary>
public sealed class activeuser
{
private readonly string _ticket; //票據(jù)名稱
private readonly string _username; //登陸用戶名
private readonly string _truename; //登陸用戶名
private readonly string _roleid; //角色
private readonly datetime _refreshtime; //最新刷新時間
private readonly datetime _activetime; //最新活動時間
private readonly string _clientip; //登陸ip
public activeuser(string ticket,string username,string truename,string roleid,string clientip) {
this._ticket=ticket;
this._username=username;
this._truename=truename;
this._roleid=roleid;
this._refreshtime=datetime.now;
this._activetime=datetime.now;
this._clientip=clientip;
}
public activeuser(string ticket,string username,string truename,string roleid,datetime refreshtime,datetime activetime,string clientip) {
this._ticket=ticket;
this._username=username;
this._truename=truename;
this._roleid=roleid;
this._refreshtime=refreshtime;
this._activetime=activetime;
this._clientip=clientip;
}
public string ticket { get{return _ticket;} }
public string username { get{return _username;} }
public string truename { get{return _truename;} }
public string roleid { get{return _roleid;} }
public datetime refreshtime { get{return _refreshtime;} }
public datetime activetime { get{return _activetime;} }
public string clientip { get{return _clientip;} }
}
2、 新建一個名為passport的類,存儲在線用戶列表。
/// <summary>
/// 單個在線用戶數(shù)據(jù),無法繼承此類。
/// </summary>
public sealed class activeuser
{
private readonly string _ticket; //票據(jù)名稱
private readonly string _username; //登陸用戶名
private readonly string _truename; //登陸用戶名
private readonly string _roleid; //角色
private readonly datetime _refreshtime; //最新刷新時間
private readonly datetime _activetime; //最新活動時間
private readonly string _clientip; //登陸ip
public activeuser(string ticket,string username,string truename,string roleid,string clientip) {
this._ticket=ticket;
this._username=username;
this._truename=truename;
this._roleid=roleid;
this._refreshtime=datetime.now;
this._activetime=datetime.now;
this._clientip=clientip;
}
public activeuser(string ticket,string username,string truename,string roleid,datetime refreshtime,datetime activetime,string clientip) {
this._ticket=ticket;
this._username=username;
this._truename=truename;
this._roleid=roleid;
this._refreshtime=refreshtime;
this._activetime=activetime;
this._clientip=clientip;
}
public string ticket { get{return _ticket;} }
public string username { get{return _username;} }
public string truename { get{return _truename;} }
public string roleid { get{return _roleid;} }
public datetime refreshtime { get{return _refreshtime;} }
public datetime activetime { get{return _activetime;} }
public string clientip { get{return _clientip;} }
}
2、 新建一個名為passport的類,存儲在線用戶列表。
/// <summary>
/// passport 存儲在線用戶列表。
/// </summary>
public class passport
{
private static datatable _activeusers;
private int _activetimeout;
private int _refreshtimeout;
/// <summary>
/// 初始化在線用戶表。
/// </summary>
private void userstableformat()
{
if(_activeusers==null) {
_activeusers = new datatable("activeusers");
datacolumn mydatacolumn;
system.type mystringtype;
mystringtype = system.type.gettype("system.string");
system.type mytimetype;
mytimetype = system.type.gettype("system.datetime");
mydatacolumn = new datacolumn("ticket",mystringtype);
_activeusers.columns.add(mydatacolumn);
mydatacolumn = new datacolumn("username",mystringtype);
_activeusers.columns.add(mydatacolumn);
mydatacolumn = new datacolumn("truename",mystringtype);
_activeusers.columns.add(mydatacolumn);
mydatacolumn = new datacolumn("roleid",mystringtype);
_activeusers.columns.add(mydatacolumn);
mydatacolumn = new datacolumn("refreshtime",mytimetype);
_activeusers.columns.add(mydatacolumn);
mydatacolumn = new datacolumn("activetime",mytimetype);
_activeusers.columns.add(mydatacolumn);
mydatacolumn = new datacolumn("clientip",mystringtype);
_activeusers.columns.add(mydatacolumn);
}
}
public passport()
{
userstableformat(); //初始化在線用戶表
//活動超時時間初始化 單位:分鐘
try { _activetimeout=int.parse(configurationsettings.appsettings["activetimeout"]); }
catch{ _activetimeout=60; }
//自動刷新超時時間初始化 單位:分鐘
try { _refreshtimeout=int.parse(configurationsettings.appsettings["refreshtimeout"]); }
catch{ _refreshtimeout=1; }
}
//全部用戶列表
public datatable activeusers
{
get{return _activeusers.copy();}
}
/// <summary>
/// 新用戶登陸。
/// </summary>
public void login(activeuser user,bool singlelogin)
{
deltimeout(); //清除超時用戶
if(singlelogin){
//若是單人登陸則注銷原來登陸的用戶
this.logout(user.username,false);
}
datarow myrow;
try
{
myrow = _activeusers.newrow();
myrow["ticket"] = user.ticket.trim();
myrow["username"] = user.username.trim();
myrow["truename"] = ""+user.truename.trim();
myrow["roleid"] = ""+user.roleid.trim();
myrow["activetime"] = datetime.now;
myrow["refreshtime"] = datetime.now;
myrow["clientip"] = user.clientip.trim();
_activeusers.rows.add(myrow);
}
catch(exception e)
{
throw(new exception(e.message));
}
_activeusers.acceptchanges();
}
/// <summary>
///用戶注銷,根據(jù)ticket或username。
/// </summary>
private void logout(string struserkey,bool byticket)
{
deltimeout(); //清除超時用戶
struserkey=struserkey.trim();
string strexpr;
strexpr =byticket ? "ticket='" + struserkey +"'" : "username='" + struserkey + "'";
datarow[] curuser;
curuser = _activeusers.select(strexpr);
if (curuser.length >0 )
{
for(int i = 0; i < curuser.length; i ++)
{
curuser[i].delete();
}
}
_activeusers.acceptchanges();
}
/// <summary>
///用戶注銷,根據(jù)ticket。
/// </summary>
/// <param name="strticket">要注銷的用戶ticket</param>
public void logout(string strticket){
this.logout(strticket,true);
}
/// <summary>
///清除超時用戶。
/// </summary>
private bool deltimeout()
{
string strexpr;
strexpr = "activetime < '" + datetime.now.addminutes( 0 - _activetimeout) + "'or refreshtime < '"+datetime.now.addminutes( 0 - _refreshtimeout)+"'";
datarow[] curuser;
curuser = _activeusers.select(strexpr);
if (curuser.length >0 )
{
for(int i = 0; i < curuser.length; i ++)
{
curuser[i].delete();
}
}
_activeusers.acceptchanges();
return true;
}
/// <summary>
///更新用戶活動時間。
/// </summary>
public void activetime(string strticket)
{
deltimeout();
string strexpr;
strexpr = "ticket='" + strticket + "'";
datarow[] curuser;
curuser = _activeusers.select(strexpr);
if (curuser.length >0 )
{
for(int i = 0; i < curuser.length; i ++)
{
curuser[i]["activetime"]=datetime.now;
curuser[i]["refreshtime"]=datetime.now;
}
}
_activeusers.acceptchanges();
}
/// <summary>
///更新系統(tǒng)自動刷新時間。
/// </summary>
public void refreshtime(string strticket)
{
deltimeout();
string strexpr;
strexpr = "ticket='" + strticket + "'";
datarow[] curuser;
curuser = _activeusers.select(strexpr);
if (curuser.length >0 )
{
for(int i = 0; i < curuser.length; i ++)
{
curuser[i]["refreshtime"]=datetime.now;
}
}
_activeusers.acceptchanges();
}
private activeuser singleuser(string struserkey,bool byticket)
{
struserkey=struserkey.trim();
string strexpr;
activeuser myuser;
strexpr =byticket ? "ticket='" + struserkey +"'" : "username='" + struserkey + "'";
datarow[] curuser;
curuser = _activeusers.select(strexpr);
if (curuser.length >0 )
{
string myticket=(string)curuser[0]["ticket"];
string myuser=(string)curuser[0]["username"];
string myname=(string)curuser[0]["truename"];
string myroleid=(string)curuser[0]["roleid"];
datetime myactivetime=(datetime)curuser[0]["activetime"];
datetime myrefreshtime=(datetime)curuser[0]["refreshtime"];
string myclientip =(string)curuser[0]["clientip"];
myuser=new activeuser(myticket,myuser,myname,myroleid,myactivetime,myrefreshtime,myclientip);
}
else
{
myuser=new activeuser("","","","","");
}
return myuser;
}
/// <summary>
///按ticket獲取活動用戶。
/// </summary>
public activeuser singleuser_byticket(string strticket)
{
return this.singleuser(strticket,true);
}
/// <summary>
///按username獲取活動用戶。
/// </summary>
public activeuser singleuser_byusername(string strusername)
{
return this.singleuser(strusername,false);
}
/// <summary>
///按ticket判斷用戶是否在線。
/// </summary>
public bool isonline_byticket(string strticket)
{
return (bool)(this.singleuser(strticket,true).username!="");
}
/// <summary>
///按username判斷用戶是否在線。
/// </summary>
public bool isonline_byusername(string strusername)
{
return (bool)(this.singleuser(strusername,false).username!="");
}
}
3、 新建一個繼承自placeholder名為refresh的類,執(zhí)行更新自動刷新時間操作。
/// <summary>
/// refresh 執(zhí)行更新自動刷新時間操作。
/// </summary>
public class refresh: placeholder
{
/// <summary>
/// 設(shè)置存儲ticket的session名稱,默認為ticket。
/// </summary>
public virtual string sessionname
{
get{
object obj1 = this.viewstate["sessionname"];
if (obj1 != null){ return ((string) obj1).trim(); }
return "ticket";
}
set{
this.viewstate["sessionname"] = value;
}
}
protected override void render(htmltextwriter writer)
{
string myticket=(string)this.page.session[this.sessionname];
if(myticket!=null)
{
passport mypass = new passport();
mypass.refreshtime(myticket);
writer.write("ok:"+datetime.now.tostring());
}
else{
writer.write("sorry:"+datetime.now.tostring());
}
base.render(writer);
}
}
4、 新建一個繼承自placeholder名為script的類,生成執(zhí)行xmlhttp的js腳本。。
/// <summary>
/// script 生成執(zhí)行xmlhttp的js腳本。
/// </summary>
public class script: placeholder
{
/// <summary>
/// 設(shè)置js自動刷新的間隔時間,默認為25秒。
/// </summary>
public virtual int refreshtime
{
get
{
object obj1 = this.viewstate["refreshtime"];
if (obj1 != null){return int.parse(((string) obj1).trim());}
return 25;
}
set
{
this.viewstate["refreshtime"] = value;
}
}
protected override void render(htmltextwriter writer)
{
//從web.config中讀取xmlhttp的訪問地址
string refreshurl=(string)configurationsettings.appsettings["refreshurl"];
string scriptstring = @" <script language=""javascript"">"+writer.newline;
scriptstring += @" window.attachevent(""onload"", "[email protected]"_postrefresh);"+writer.newline;
scriptstring += @" var "[email protected]"_xmlhttp=null;"+writer.newline;
scriptstring += @" function "[email protected]"_postrefresh(){"+writer.newline;
scriptstring += @" var "[email protected]"_xmlhttp = new activexobject(""msxml2.xmlhttp"");"+writer.newline;
scriptstring += @" "[email protected]"_xmlhttp.open(""post"", """[email protected]""", false);"+writer.newline;
scriptstring += @" "[email protected]"_xmlhttp.send();"+writer.newline;
scriptstring += @" var refreshstr= "[email protected]"_xmlhttp.responsetext;"+writer.newline;
scriptstring += @" try {"+writer.newline;
scriptstring += @" var refreshstr2=refreshstr;"+writer.newline;
//scriptstring += @" alert(refreshstr2);"+writer.newline;
scriptstring += @" }"+writer.newline;
scriptstring += @" catch(e) {}"+writer.newline;
scriptstring += @" settimeout("""[email protected]"_postrefresh()"","+this.refreshtime.tostring()[email protected]"000);"+writer.newline;
scriptstring += @" }"+writer.newline;
scriptstring += @"<";
scriptstring += @"/";
scriptstring += @"script>"+writer.newline;
writer.write(writer.newline);
writer.write(scriptstring);
writer.write(writer.newline);
base.render(writer);
}
}
注意以上四個類同屬于一個名為onlineuser的工程,他們的命名空間為onlineuser,編譯生成一個dll。
/// <summary>
/// script 生成執(zhí)行xmlhttp的js腳本。
/// </summary>
public class script: placeholder
{
/// <summary>
/// 設(shè)置js自動刷新的間隔時間,默認為25秒。
/// </summary>
public virtual int refreshtime
{
get
{
object obj1 = this.viewstate["refreshtime"];
if (obj1 != null){return int.parse(((string) obj1).trim());}
return 25;
}
set
{
this.viewstate["refreshtime"] = value;
}
}
protected override void render(htmltextwriter writer)
{
//從web.config中讀取xmlhttp的訪問地址
string refreshurl=(string)configurationsettings.appsettings["refreshurl"];
string scriptstring = @" <script language=""javascript"">"+writer.newline;
scriptstring += @" window.attachevent(""onload"", "[email protected]"_postrefresh);"+writer.newline;
scriptstring += @" var "[email protected]"_xmlhttp=null;"+writer.newline;
scriptstring += @" function "[email protected]"_postrefresh(){"+writer.newline;
scriptstring += @" var "[email protected]"_xmlhttp = new activexobject(""msxml2.xmlhttp"");"+writer.newline;
scriptstring += @" "[email protected]"_xmlhttp.open(""post"", """[email protected]""", false);"+writer.newline;
scriptstring += @" "[email protected]"_xmlhttp.send();"+writer.newline;
scriptstring += @" var refreshstr= "[email protected]"_xmlhttp.responsetext;"+writer.newline;
scriptstring += @" try {"+writer.newline;
scriptstring += @" var refreshstr2=refreshstr;"+writer.newline;
//scriptstring += @" alert(refreshstr2);"+writer.newline;
scriptstring += @" }"+writer.newline;
scriptstring += @" catch(e) {}"+writer.newline;
scriptstring += @" settimeout("""[email protected]"_postrefresh()"","+this.refreshtime.tostring()[email protected]"000);"+writer.newline;
scriptstring += @" }"+writer.newline;
scriptstring += @"<";
scriptstring += @"/";
scriptstring += @"script>"+writer.newline;
writer.write(writer.newline);
writer.write(scriptstring);
writer.write(writer.newline);
base.render(writer);
}
}
注意以上四個類同屬于一個名為onlineuser的工程,他們的命名空間為onlineuser,編譯生成一個dll。
/// <summary>
/// refresh 執(zhí)行更新自動刷新時間操作。
/// </summary>
public class refresh: placeholder
{
/// <summary>
/// 設(shè)置存儲ticket的session名稱,默認為ticket。
/// </summary>
public virtual string sessionname
{
get{
object obj1 = this.viewstate["sessionname"];
if (obj1 != null){ return ((string) obj1).trim(); }
return "ticket";
}
set{
this.viewstate["sessionname"] = value;
}
}
protected override void render(htmltextwriter writer)
{
string myticket=(string)this.page.session[this.sessionname];
if(myticket!=null)
{
passport mypass = new passport();
mypass.refreshtime(myticket);
writer.write("ok:"+datetime.now.tostring());
}
else{
writer.write("sorry:"+datetime.now.tostring());
}
base.render(writer);
}
}
4、 新建一個繼承自placeholder名為script的類,生成執(zhí)行xmlhttp的js腳本。。
/// <summary>
/// script 生成執(zhí)行xmlhttp的js腳本。
/// </summary>
public class script: placeholder
{
/// <summary>
/// 設(shè)置js自動刷新的間隔時間,默認為25秒。
/// </summary>
public virtual int refreshtime
{
get
{
object obj1 = this.viewstate["refreshtime"];
if (obj1 != null){return int.parse(((string) obj1).trim());}
return 25;
}
set
{
this.viewstate["refreshtime"] = value;
}
}
protected override void render(htmltextwriter writer)
{
//從web.config中讀取xmlhttp的訪問地址
string refreshurl=(string)configurationsettings.appsettings["refreshurl"];
string scriptstring = @" <script language=""javascript"">"+writer.newline;
scriptstring += @" window.attachevent(""onload"", "[email protected]"_postrefresh);"+writer.newline;
scriptstring += @" var "[email protected]"_xmlhttp=null;"+writer.newline;
scriptstring += @" function "[email protected]"_postrefresh(){"+writer.newline;
scriptstring += @" var "[email protected]"_xmlhttp = new activexobject(""msxml2.xmlhttp"");"+writer.newline;
scriptstring += @" "[email protected]"_xmlhttp.open(""post"", """[email protected]""", false);"+writer.newline;
scriptstring += @" "[email protected]"_xmlhttp.send();"+writer.newline;
scriptstring += @" var refreshstr= "[email protected]"_xmlhttp.responsetext;"+writer.newline;
scriptstring += @" try {"+writer.newline;
scriptstring += @" var refreshstr2=refreshstr;"+writer.newline;
//scriptstring += @" alert(refreshstr2);"+writer.newline;
scriptstring += @" }"+writer.newline;
scriptstring += @" catch(e) {}"+writer.newline;
scriptstring += @" settimeout("""[email protected]"_postrefresh()"","+this.refreshtime.tostring()[email protected]"000);"+writer.newline;
scriptstring += @" }"+writer.newline;
scriptstring += @"<";
scriptstring += @"/";
scriptstring += @"script>"+writer.newline;
writer.write(writer.newline);
writer.write(scriptstring);
writer.write(writer.newline);
base.render(writer);
}
}
注意以上四個類同屬于一個名為onlineuser的工程,他們的命名空間為onlineuser,編譯生成一個dll。
/// <summary>
/// script 生成執(zhí)行xmlhttp的js腳本。
/// </summary>
public class script: placeholder
{
/// <summary>
/// 設(shè)置js自動刷新的間隔時間,默認為25秒。
/// </summary>
public virtual int refreshtime
{
get
{
object obj1 = this.viewstate["refreshtime"];
if (obj1 != null){return int.parse(((string) obj1).trim());}
return 25;
}
set
{
this.viewstate["refreshtime"] = value;
}
}
protected override void render(htmltextwriter writer)
{
//從web.config中讀取xmlhttp的訪問地址
string refreshurl=(string)configurationsettings.appsettings["refreshurl"];
string scriptstring = @" <script language=""javascript"">"+writer.newline;
scriptstring += @" window.attachevent(""onload"", "[email protected]"_postrefresh);"+writer.newline;
scriptstring += @" var "[email protected]"_xmlhttp=null;"+writer.newline;
scriptstring += @" function "[email protected]"_postrefresh(){"+writer.newline;
scriptstring += @" var "[email protected]"_xmlhttp = new activexobject(""msxml2.xmlhttp"");"+writer.newline;
scriptstring += @" "[email protected]"_xmlhttp.open(""post"", """[email protected]""", false);"+writer.newline;
scriptstring += @" "[email protected]"_xmlhttp.send();"+writer.newline;
scriptstring += @" var refreshstr= "[email protected]"_xmlhttp.responsetext;"+writer.newline;
scriptstring += @" try {"+writer.newline;
scriptstring += @" var refreshstr2=refreshstr;"+writer.newline;
//scriptstring += @" alert(refreshstr2);"+writer.newline;
scriptstring += @" }"+writer.newline;
scriptstring += @" catch(e) {}"+writer.newline;
scriptstring += @" settimeout("""[email protected]"_postrefresh()"","+this.refreshtime.tostring()[email protected]"000);"+writer.newline;
scriptstring += @" }"+writer.newline;
scriptstring += @"<";
scriptstring += @"/";
scriptstring += @"script>"+writer.newline;
writer.write(writer.newline);
writer.write(scriptstring);
writer.write(writer.newline);
base.render(writer);
}
}
注意以上四個類同屬于一個名為onlineuser的工程,他們的命名空間為onlineuser,編譯生成一個dll。
/// <summary>
/// refresh 執(zhí)行更新自動刷新時間操作。
/// </summary>
public class refresh: placeholder
{
/// <summary>
/// 設(shè)置存儲ticket的session名稱,默認為ticket。
/// </summary>
public virtual string sessionname
{
get{
object obj1 = this.viewstate["sessionname"];
if (obj1 != null){ return ((string) obj1).trim(); }
return "ticket";
}
set{
this.viewstate["sessionname"] = value;
}
}
protected override void render(htmltextwriter writer)
{
string myticket=(string)this.page.session[this.sessionname];
if(myticket!=null)
{
passport mypass = new passport();
mypass.refreshtime(myticket);
writer.write("ok:"+datetime.now.tostring());
}
else{
writer.write("sorry:"+datetime.now.tostring());
}
base.render(writer);
}
}
4、 新建一個繼承自placeholder名為script的類,生成執(zhí)行xmlhttp的js腳本。。
/// <summary>
/// script 生成執(zhí)行xmlhttp的js腳本。
/// </summary>
public class script: placeholder
{
/// <summary>
/// 設(shè)置js自動刷新的間隔時間,默認為25秒。
/// </summary>
public virtual int refreshtime
{
get
{
object obj1 = this.viewstate["refreshtime"];
if (obj1 != null){return int.parse(((string) obj1).trim());}
return 25;
}
set
{
this.viewstate["refreshtime"] = value;
}
}
protected override void render(htmltextwriter writer)
{
//從web.config中讀取xmlhttp的訪問地址
string refreshurl=(string)configurationsettings.appsettings["refreshurl"];
string scriptstring = @" <script language=""javascript"">"+writer.newline;
scriptstring += @" window.attachevent(""onload"", "[email protected]"_postrefresh);"+writer.newline;
scriptstring += @" var "[email protected]"_xmlhttp=null;"+writer.newline;
scriptstring += @" function "[email protected]"_postrefresh(){"+writer.newline;
scriptstring += @" var "[email protected]"_xmlhttp = new activexobject(""msxml2.xmlhttp"");"+writer.newline;
scriptstring += @" "[email protected]"_xmlhttp.open(""post"", """[email protected]""", false);"+writer.newline;
scriptstring += @" "[email protected]"_xmlhttp.send();"+writer.newline;
scriptstring += @" var refreshstr= "[email protected]"_xmlhttp.responsetext;"+writer.newline;
scriptstring += @" try {"+writer.newline;
scriptstring += @" var refreshstr2=refreshstr;"+writer.newline;
//scriptstring += @" alert(refreshstr2);"+writer.newline;
scriptstring += @" }"+writer.newline;
scriptstring += @" catch(e) {}"+writer.newline;
scriptstring += @" settimeout("""[email protected]"_postrefresh()"","+this.refreshtime.tostring()[email protected]"000);"+writer.newline;
scriptstring += @" }"+writer.newline;
scriptstring += @"<";
scriptstring += @"/";
scriptstring += @"script>"+writer.newline;
writer.write(writer.newline);
writer.write(scriptstring);
writer.write(writer.newline);
base.render(writer);
}
}
注意以上四個類同屬于一個名為onlineuser的工程,他們的命名空間為onlineuser,編譯生成一個dll。
/// <summary>
/// script 生成執(zhí)行xmlhttp的js腳本。
/// </summary>
public class script: placeholder
{
/// <summary>
/// 設(shè)置js自動刷新的間隔時間,默認為25秒。
/// </summary>
public virtual int refreshtime
{
get
{
object obj1 = this.viewstate["refreshtime"];
if (obj1 != null){return int.parse(((string) obj1).trim());}
return 25;
}
set
{
this.viewstate["refreshtime"] = value;
}
}
protected override void render(htmltextwriter writer)
{
//從web.config中讀取xmlhttp的訪問地址
string refreshurl=(string)configurationsettings.appsettings["refreshurl"];
string scriptstring = @" <script language=""javascript"">"+writer.newline;
scriptstring += @" window.attachevent(""onload"", "[email protected]"_postrefresh);"+writer.newline;
scriptstring += @" var "[email protected]"_xmlhttp=null;"+writer.newline;
scriptstring += @" function "[email protected]"_postrefresh(){"+writer.newline;
scriptstring += @" var "[email protected]"_xmlhttp = new activexobject(""msxml2.xmlhttp"");"+writer.newline;
scriptstring += @" "[email protected]"_xmlhttp.open(""post"", """[email protected]""", false);"+writer.newline;
scriptstring += @" "[email protected]"_xmlhttp.send();"+writer.newline;
scriptstring += @" var refreshstr= "[email protected]"_xmlhttp.responsetext;"+writer.newline;
scriptstring += @" try {"+writer.newline;
scriptstring += @" var refreshstr2=refreshstr;"+writer.newline;
//scriptstring += @" alert(refreshstr2);"+writer.newline;
scriptstring += @" }"+writer.newline;
scriptstring += @" catch(e) {}"+writer.newline;
scriptstring += @" settimeout("""[email protected]"_postrefresh()"","+this.refreshtime.tostring()[email protected]"000);"+writer.newline;
scriptstring += @" }"+writer.newline;
scriptstring += @"<";
scriptstring += @"/";
scriptstring += @"script>"+writer.newline;
writer.write(writer.newline);
writer.write(scriptstring);
writer.write(writer.newline);
base.render(writer);
}
}
注意以上四個類同屬于一個名為onlineuser的工程,他們的命名空間為onlineuser,編譯生成一個dll。
4、 新建一個繼承自placeholder名為script的類,生成執(zhí)行xmlhttp的js腳本。。/// <summary>
/// script 生成執(zhí)行xmlhttp的js腳本。
/// </summary>
public class script: placeholder
{
/// <summary>
/// 設(shè)置js自動刷新的間隔時間,默認為25秒。
/// </summary>
public virtual int refreshtime
{
get
{
object obj1 = this.viewstate["refreshtime"];
if (obj1 != null){return int.parse(((string) obj1).trim());}
return 25;
}
set
{
this.viewstate["refreshtime"] = value;
}
}
protected override void render(htmltextwriter writer)
{
//從web.config中讀取xmlhttp的訪問地址
string refreshurl=(string)configurationsettings.appsettings["refreshurl"];
string scriptstring = @" <script language=""javascript"">"+writer.newline;
scriptstring += @" window.attachevent(""onload"", "[email protected]"_postrefresh);"+writer.newline;
scriptstring += @" var "[email protected]"_xmlhttp=null;"+writer.newline;
scriptstring += @" function "[email protected]"_postrefresh(){"+writer.newline;
scriptstring += @" var "[email protected]"_xmlhttp = new activexobject(""msxml2.xmlhttp"");"+writer.newline;
scriptstring += @" "[email protected]"_xmlhttp.open(""post"", """[email protected]""", false);"+writer.newline;
scriptstring += @" "[email protected]"_xmlhttp.send();"+writer.newline;
scriptstring += @" var refreshstr= "[email protected]"_xmlhttp.responsetext;"+writer.newline;
scriptstring += @" try {"+writer.newline;
scriptstring += @" var refreshstr2=refreshstr;"+writer.newline;
//scriptstring += @" alert(refreshstr2);"+writer.newline;
scriptstring += @" }"+writer.newline;
scriptstring += @" catch(e) {}"+writer.newline;
scriptstring += @" settimeout("""[email protected]"_postrefresh()"","+this.refreshtime.tostring()[email protected]"000);"+writer.newline;
scriptstring += @" }"+writer.newline;
scriptstring += @"<";
scriptstring += @"/";
scriptstring += @"script>"+writer.newline;
writer.write(writer.newline);
writer.write(scriptstring);
writer.write(writer.newline);
base.render(writer);
}
}
注意以上四個類同屬于一個名為onlineuser的工程,他們的命名空間為onlineuser,編譯生成一個dll。
注意以上四個類同屬于一個名為onlineuser的工程,他們的命名空間為onlineuser,編譯生成一個dll。===============================================
下面我簡單介紹一下調(diào)用方法:
1、 新建一個名為onlineuserdemo的asp.net web應用程序
2、 在vs的工具箱選項卡上右擊,選擇[添加/移除項],瀏覽定位到onlineuser.dll,確定即可把refresh 和script添加到工具箱。
3、 把自動生成的webform1.aspx刪除,并設(shè)置web.config
<appsettings>
<add key="activetimeout" value="30" />
<add key="refreshtimeout" value="1" />
<add key="refreshurl" value="refresh.aspx" />
</appsettings>
4、 添加一個名為online.aspx的web窗體,給該窗體添加一個script控件,一個datagrid控件(id為datagrid1),兩個hyperlink控件(分別鏈接到login.aspx和logout.aspx,text屬性分別設(shè)置為“登陸”和“注銷”),調(diào)整好四個控件的位置,轉(zhuǎn)到codebehind,在page_load中加入如下代碼:
string myticket=(string)this.page.session["ticket"];
if(myticket!=null)
{
onlineuser.passport mypassport= new onlineuser.passport();
if(mypassport.isonline_byticket(this.session["ticket"].tostring()))
{
mypassport.activetime(this.session["ticket"].tostring());
datagrid1.datasource=mypassport.activeusers;
datagrid1.databind();
}
else{
//若在線用戶列表中找不到當前用戶,則定向到注銷頁面
response.redirect("logout.aspx");
}
}
else{
response.redirect("login.aspx");
}
5、 添加一個名為login.aspx的web窗體,給該窗體添加一個label控件(id為label1),設(shè)置text屬性為“輸入一個用戶名”,再添加一個textbox控件(id為textbox1)和一個button控件(id為button1),調(diào)整好他們的位置,雙擊button1控件轉(zhuǎn)到codebehind,為button1的click事件加入如下代碼:
if(textbox1.text.trim()=="")
{
//不能為空
string scriptstring = @"<script language=javascript>";
scriptstring += @"alert(""輸入一個用戶名/n"");";
scriptstring += @"history.go(-1);";
scriptstring += @"<";
scriptstring += @"/";
scriptstring += @"script>";
if(!this.page.isstartupscriptregistered("startup"))
this.page.registerstartupscript("startup", scriptstring);
}
else{
onlineuser.passport mypassport= new onlineuser.passport();
string myticket=datetime.now.tostring("yyyymmddhhmmss");
string myuser=textbox1.text.trim();
string myclintip=this.request.userhostaddress;
this.session["ticket"]=myticket;
onlineuser.activeuser myactiveuser=new onlineuser.activeuser(myticket,myuser,myuser,"test",myclintip);
mypassport.login(myactiveuser,true);
response.redirect("online.aspx");
}
6、 添加一個名為logout.aspx的web窗體,給該窗體添加一個hyperlink控件,指向login.aspx,text屬性設(shè)置為“重登陸”轉(zhuǎn)到codebehind,在page_load中加入如下代碼:
onlineuser.passport mypassport= new onlineuser.passport();
mypassport.logout(this.session["ticket"].tostring());
this.session["ticket"]="";
7、 添加一個名為refresh.txt的文本文件,設(shè)置其內(nèi)容為:
<%@ register tagprefix="cc2" namespace="onlineuser" assembly="onlineuser" %>
<%@ page %>
<cc2:refresh id="myrefresh" runat="server"></cc2:refresh>
把refresh.txt改名為refresh.aspx
8、 編譯生成工程。
===============================================
下面進行功能測試:
1、 打開瀏覽器,在地址欄輸入
http://你機器的ip地址/onlineuserdemo/login.aspx
2、 輸入一個用戶名(假設(shè)是test1)登陸,自動轉(zhuǎn)到online.aspx頁面
3、 找同網(wǎng)段的另外一臺機器(設(shè)你的機器為a,這臺機器為b),重復執(zhí)行第一步。
4、 輸入一個用戶名(假設(shè)是test2)登陸,自動轉(zhuǎn)到online.aspx頁面
5、 在b機器不斷刷新online.aspx,若發(fā)現(xiàn)test1用戶refreshtime每過25秒自動更新一次而activetime不變(這個時候a機器不要刷新頁面啊),則證明a機器的自動刷新生效。
6、 在a機器不斷刷新online.aspx,若發(fā)現(xiàn)test2用戶refreshtime每過25秒自動更新一次而activetime不變(這個時候b機器不要刷新頁面啊),則證明b機器的自動刷新生效。
7、 大功告成。
新聞熱點
疑難解答
圖片精選