首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

关于ASP.NET2.0环境中显示在线用户的办法,该如何处理

2012-01-19 
关于ASP.NET2.0环境中显示在线用户的办法讨论一下关于ASP.NET2.0环境中显示在线用户的办法,因为最近工作中

关于ASP.NET2.0环境中显示在线用户的办法
讨论一下关于ASP.NET2.0环境中显示在线用户的办法,因为最近工作中遇到这个问题,虽然解决了,但是办法不好,而且不想使用MEMBER   SHIP,因为感觉真的很麻烦,特别是对于我们的程序,权限设计比较复杂,希望大家可以说说自己的示在线用户de好办法@@@


[解决办法]
帮楼主顶!
[解决办法]
不用MEMBER SHIP也没什么特别好的办法,在会话开始事件中在全局变量中增加1个名额,然后别人退出时在会话结束事件中在全局变量中减少一个名额。
[解决办法]
显示在线用户还是显示在线用户名字还是人数?
[解决办法]
public class MemberOnlineInfo
{
public string userName;
public string userId;
public string IP;
public DateTime lastActive;
public string lastUrl;

public MemberOnlineInfo(string name, string id, string ip, DateTime lastactive, string lasturl)
{
userName = name;
userId = id;
IP = ip;
lastActive = lastactive;
lastUrl = lasturl;
}

}

public class MemberOnlineList
{
public static Hashtable memberList = new Hashtable();

public static bool IsOnline(string name)
{
return (memberList.ContainsKey(name)) ? true : false;
}

public static void AddOnline(MemberOnlineInfo online)
{
memberList.Add(online.userName, online);
}

public static void UpdateOnline(MemberOnlineInfo online)
{
memberList[online.userName] = online;
}

public static void DeleteOnline() //超过20分钟的删除
{
DateTime now = DateTime.Now;
foreach(DictionaryEntry deChair in memberList)
{
if ((now - ((MemberOnlineInfo)(deChair.Value)).lastActive) > TimeSpan.FromMinutes(20)){
memberList.Remove(((MemberOnlineInfo)(deChair.Value)).userName);
}
}

}

}

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
string strName =string.Empty;
string strUid =string.Empty;

if (Request.IsAuthenticated)
{
strName = User.Identity.Name;
strUid = Request.Cookies[ "uid "].Value;
}
else {
strName = Request.Cookies[ "uid "].Value;
}

MemberOnlineInfo Online = new MemberOnlineInfo(strName, strUid, Request.UserHostAddress, DateTime.Now, Request.FilePath);
if (MemberOnlineList.IsOnline(strName))
{
MemberOnlineList.UpdateOnline(Online);
}
else
{
MemberOnlineList.AddOnline(Online);
}

MemberOnlineList.DeleteOnline();
}


//显示在线的用户
public StringBuilder online=new StringBuilder();
protected void Page_Load(object sender, EventArgs e)
{

online.Append( " <table cellSpacing=\ "2\ " cellPadding=\ "2\ " width=\ "90%\ " class=\ "grid\ "> ");
online.Append( " <tr> ");
online.Append( " <th > User </th> ");
online.Append( " <th> &nbsp; </th> ");


online.Append( " <th> URL </th> ");
online.Append( " <th> IP </th> ");
online.Append( " </tr> ");
bool f = false;
foreach (DictionaryEntry deChair in MemberOnlineList.memberList)
{
online.Append( " <tr class=\ "row " + ((f = !f) ? "0 " : "1 ") + "\ "> ");
online.Append( " <td> " + ((MemberOnlineInfo)(deChair.Value)).userName + " </td> ");
online.Append( " <td> " + ((MemberOnlineInfo)(deChair.Value)).lastActive + " </td> ");
online.Append( " <td> <a href=\ " " + ((MemberOnlineInfo)(deChair.Value)).lastUrl + "\ "> " + ((MemberOnlineInfo)(deChair.Value)).lastUrl + " </a> </td> ");
online.Append( " <td> " + ((MemberOnlineInfo)(deChair.Value)).IP + " </td> ");
}

online.Append( " </table> ");
}

热点排行