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

首頁 > 編程 > .NET > 正文

ASP.NET在線用戶列表精確版——解決用戶意外退出在線列表無法及時更新問題

2024-07-10 12:55:42
字體:
供稿:網(wǎng)友


最近所做的一個項目需要用到的在線用戶列表,上網(wǎng)搜索了一下發(fā)現(xiàn)現(xiàn)有的解決方案對用戶意外退出的處理均不是太理想。一般來說,用戶離開系統(tǒng)的方式有三種:主動注銷、會話超時、直接關(guān)閉瀏覽器,對于前兩種,我們很容易便可將該用戶從在線列表中清除,關(guān)鍵是第三種(很多用戶都是直接關(guān)閉窗口的~~郁悶ing),程序無法捕獲窗口關(guān)閉的精確時間,只能等到會話超時后在能將該用戶清除出在線列表,假設(shè)我們設(shè)置會話超時時間為60分鐘,而用戶登陸系統(tǒng)隨便瀏覽一個頁面就以關(guān)閉瀏覽器的方式退出的話,我們要在將近1小時后才能從在線列表中將該用戶清除出去(想象一下,系統(tǒng)顯示n多人在線,可能除了你之外其他的n-1人都關(guān)機走人了,汗一個先```),而本文將嘗試尋找一個解決方案把這種尷尬降至最低。
    我的大概思路是,給每在線用戶增加一個refreshtime屬性,建立一個負責將當前用戶的refreshtime屬性設(shè)置為當前時間的單獨頁面(refresh.aspx),然后在系統(tǒng)的主要頁面(也可以是所有頁面)中通過xmlhttp不斷地請求refresh.aspx頁面,一旦用戶關(guān)閉了與本系統(tǒng)相關(guān)的所有窗口,即以直接關(guān)閉瀏覽器的方式退出系統(tǒng),那么該用戶的refreshtime屬性便不會自動更新了,我們再設(shè)置一個自動刷新的超時時間(這個要比會話超時短很多_refreshtimeout),當發(fā)現(xiàn)某用戶超過_refreshtimeout的時間沒有自動刷新,就能判定該用戶已經(jīng)以直接關(guān)閉瀏覽器的方式退出了。
    假設(shè)我們設(shè)置會話超時時間為60分鐘,自動刷新超時時間為1分鐘,在客戶端通過xmlhttp每隔25秒(之所以不設(shè)1分鐘,是防止網(wǎng)速慢的時候訪問refresh.aspx超時,個人感覺,不一定正確)訪問一次refresh.aspx頁面,在用戶登陸、用戶注銷、檢測用戶是否在線的時候都執(zhí)行清理超時用戶(包括會話超時和自動刷新超時)操作,這樣一來,在線用戶列表的統(tǒng)計誤差就由60分鐘降至1分鐘了。

==========================================



具體實現(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。


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。

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、 大功告成。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 五莲县| 沙坪坝区| 饶阳县| 高清| 珠海市| 昂仁县| 建平县| 南陵县| 灵寿县| 拉萨市| 石河子市| 来宾市| 彭阳县| 永嘉县| 西充县| 深泽县| 富蕴县| 安顺市| 蓝田县| 陇西县| 江达县| 罗田县| 松原市| 平湖市| 伊川县| 乐至县| 广昌县| 灵石县| 琼海市| 清原| 临泉县| 裕民县| 河东区| 兴山县| 青神县| 桃江县| 浦城县| 舒兰市| 葫芦岛市| 广水市| 喀喇|