日期:2014-05-17  浏览次数:20387 次

asp.net统计当前在线人员
我想判断当前在线人员,想了一个很笨的方法,就是数据库建一个表记录每个用户登陆和退出的时间,在进行判断,,可是退出时间也不好记录啊,如果不是正常的关闭,那退出时间就不知道了?

在网上看了一些,应该有一些简便的方法吧,监测当前在线用户。比如写到global.asax中?,可是试了很多都没有成功,大家可不可以给个详细的例子啊,谢谢了,可发到我的邮箱中540011858@qq.com

------解决方案--------------------
C# code

void application_start(object sender, eventargs e) 
  { 
  //在应用程序启动时运行的代码 
  application["count"] = 0; 
  system.io.filestream fs = system.io.file.open(server.mappath("count.txt"), system.io.filemode.openorcreate); 
  system.io.streamreader sr = new system.io.streamreader(fs); 
  application["allusers"] = convert.toint32(sr.readline()); 
  sr.close(); 
  fs.close(); 
  } 
  void application_end(object sender, eventargs e) 
  { 
  //在应用程序关闭时运行的代码 
  } 
  void application_error(object sender, eventargs e) 
  { 
  //在出现未处理的错误时运行的代码 
  } 
  void session_start(object sender, eventargs e) 
  { 
  //在新会话启动时运行的代码 
  application.lock(); 
  application["count"] = convert.toint32(application["count"]) + 1; 
  application["allusers"] = convert.toint32(application["allusers"]) + 1; 
  system.io.filestream fs = new system.io.filestream("count.txt", system.io.filemode.openorcreate, system.io.fileaccess.readwrite); 
  system.io.streamwriter sw = new system.io.streamwriter(fs); 
  sw.writeline(application["allusers"]); 
  sw.close(); 
  fs.close(); 
  application.unlock(); 
  } 
  void session_end(object sender, eventargs e) 
  { 
  //在会话结束时运行的代码。 
  // 注意: 只有在 web.config 文件中的sessionstate 模式设置为inproc时,www.3ppt.com才会引发 session_end 事件。如果会话模式设置为 stateserver 或 sqlserver,则不会引发该事件。 
  application.lock(); 
  application["count"] = convert.toint32(application["count"]) - 1; 
  application.unlock(); 
  } 
  页面只要读出application的内容就行啦: 
  response.write(application["count"]+"当前人数"+" 
总人数:"+application["allusers"]);

------解决方案--------------------
C# code

void application_start(object sender, eventargs e) 
  { 
  //在应用程序启动时运行的代码 
  application["count"] = 0; 
  system.io.filestream fs = system.io.file.open(server.mappath("count.txt"), system.io.filemode.openorcreate); 
  system.io.streamreader sr = new system.io.streamreader(fs); 
  application["allusers"] = convert.toint32(sr.readline()); 
  sr.close(); 
  fs.close(); 
  } 
  void application_end(object sender, eventargs e) 
  { 
  //在应用程序关闭时运行的代码 
  } 
  void application_error(object sender, eventargs e) 
  { 
  //在出现未处理的错误时运行的代码 
  } 
  void session_start(object sender, eventargs e) 
  { 
  //在新会话启动时运行的代码 
  application.lock(); 
  application["count"] = convert.toint32(application["count"]) + 1; 
  application["allusers"] = convert.toint32(application["allusers"]) + 1; 
  system.io.filestream fs = new system.io.filestream("count.txt", system.io.filemode.openorcreate, system.io.fileaccess.readwrite); 
  system.io.streamwriter sw = new system.io.streamwriter(fs); 
  sw.writeline(application["allusers"]); 
  sw.close(); 
  fs.close(); 
  application.unlock(); 
  } 
  void session_end(object sender, eventargs e) 
  { 
  //在会话结束时运行的代码。 
  // 注意: 只有在 web.config 文件中的sessionstate 模式设置为inproc时,www.3ppt.com才会引发 session_end 事件。如果会话模式设置为 stateserver 或 sqlserver,则不会引发该事件。 
  application.lock(); 
  application["count"] = convert.toint32(application["count"]) - 1; 
  application.unlock(); 
  } 
  页面只要读出application的内容就行啦: 
  response.write(application["count"]+"当前人数"+" 
总人数:"+application["allusers"]);

------解决方案--------------------

连注释都一样的
------解决方案--------------------
退出时间没办法记录。
可以记录一个最后活动时间,用这个时间与当前时间比较,一定范围内算是在线。