动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   .Net → 阅读文章

 ASP.NET实现用户在线检测的类源码

作者来源: 
阅读 数 472 人次 , 2006-3-29 4:33:00 

(用户在线检测)
/*程序实现思路:

该用户有以下几个属性:
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) ;
  }
  }
  }
}

 本文TagsC#  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:asp.net上传图片并同时生成缩略图
· 下一篇:DotNet Form间数据传递
· 在RichTextBox控件加入图片
· c语言10个经典小程序
· 开发者面临的.Net挑战(二)
· 细细品味ASP.NET(四)
· ASP.NET连SQL7接口源代码


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077