asp.net 用户在线列表设计,请高手指教 本帖最后由 rindybo 于 2010-06-27 13:41:50 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Singleton<OnlineInfo> 是一个Singleton<T>,单件模式实现的类,没有做类是的在线统计,请大大们看看这样设计合理吗 把我做的登录的代码也放出来吧 这样清晰一些[AcceptVerbs(HttpVerbs.Post)] public ActionResult Login() { var guid = ""; var json = new JsonMsg(); var admin = new AdminInfoService(); AdminInfo user = null; string name = Request.Form["name"]; string pass = Request.Form["pass"]; string code = Request.Form["code"]; //验证参数是否为空 if (string.IsNullOrEmpty(name)) { json.Msg = "账户名不能为空."; return Json(json); } else if (string.IsNullOrEmpty(pass)) { json.Msg = "密码不能为空."; return Json(json); } else if (string.IsNullOrEmpty(code)) { json.Msg = "验证码不能为空."; return Json(json); } //验证验证码是否为空 if (Session["Admin_valiCode"] != null) { if (Session["Admin_valiCode"].ToString() != code) { json.Msg = "验证码错误."; return Json(json); } } else { json.Msg = "验证码失效,请刷新."; return Json(json); } //获取用户信息 user = admin.GetInfo(name); if (name.ToLower() == "admin") { //admin判断区 if (user == null) { } else if (user.iPass != (name + pass).ToHash()) { json.Msg = "用户名或密码错误."; return Json(json); } } else if (user == null || user.iPass != (name + pass).ToHash()) { //其他用户判断区 json.Msg = "用户名或密码错误."; return Json(json); } user.iLine = true; user.iTime = user.iTime == null ? 1 : user.iTime + 1; user.iLast = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); user.iIpad = WebUtlity.GetClientIP(); admin.UpdateInfo(user); HttpCookie userck = Request.Cookies["userGuid"]; OnlineInfo online = null; if (userck == null) { guid = StringUtlity.NewGuid(); userck = new HttpCookie("userGuid", guid); AddInfo(guid,user); } else { guid = userck.Value; online = Common.Users.GetUser(guid) as OnlineInfo; if (online == null) { AddInfo(guid, user); } else { online.IsEdit = true; online.IsOnline = true; online.LginTime = (int)user.iTime; online.UserIp = user.iIpad; online.UserAdd = ""; online.UserHand = user.iLast; online.UserUrl = "Login"; online.CookieId = guid; online.UserGuid = user.iGuid; online.UserCode = user.iCode; online.UserName = user.iName; online.UserStar = user.iLast; online.VistTime = (int)user.iTime; } } Response.Cookies.Set(userck); return JavaScript("window.location.reload();"); } 谢谢各位指点我后台有个线程 每一分钟就把更改状态的用户更新到数据库中 贴出代码来 /// <summary> /// 使用单例模式控制线程检测在线人员状态 /// </summary> private void SetLoopUsers() { if (!Singleton<Boolean>.Instance) { Singleton<Boolean>.Instance = true; Thread thCheck = new Thread(CheckOnline) { IsBackground = true, Priority = ThreadPriority.Lowest }; thCheck.Start(); } } /// <summary> /// 检测用户状态(60秒一次),若有修改过的则存进数据库,下线的则删除 /// </summary> private void CheckOnline() { while (true) { var keys = new object[Users.GetUser().Count]; Users.GetUser().Keys.CopyTo(keys, 0); for (int i = keys.Length - 1; i >= 0; i--) { var onUser = Users.GetUser(keys[i]) as OnlineInfo; if (onUser != null && onUser.IsEdit) { //ToDo update online table if (!onUser.IsOnline) { Users.DelUser(onUser.CookieId); } } } Thread.Sleep(60000); } } discuz 论坛有这个功能,可以参考下 winform异步委托:无法访问已释放的对象。对象名:“Form1”。 问个jquery 里的get的使用问题? 关于插入时间空值问题 用Vs2005实现一行一显示? 一个老程序员的工作经历->程序员进者必看 请教:web页面数据绑定和页面传递值的问题 .net 3.5里两侧悬浮 能上下浮动div 怎样写?? 求“找出最近三年年度考核为称职以上的职工的SQL语句”,难!!! 关于gridView的问题 为什么这段代码不行啊?? 图片异步上传并保存问题(在线等候) asp.net 中重启iis 进程
public ActionResult Login()
{
var guid = "";
var json = new JsonMsg();
var admin = new AdminInfoService(); AdminInfo user = null; string name = Request.Form["name"];
string pass = Request.Form["pass"];
string code = Request.Form["code"]; //验证参数是否为空
if (string.IsNullOrEmpty(name))
{
json.Msg = "账户名不能为空.";
return Json(json);
}
else if (string.IsNullOrEmpty(pass))
{
json.Msg = "密码不能为空.";
return Json(json);
}
else if (string.IsNullOrEmpty(code))
{
json.Msg = "验证码不能为空.";
return Json(json);
} //验证验证码是否为空
if (Session["Admin_valiCode"] != null)
{
if (Session["Admin_valiCode"].ToString() != code)
{
json.Msg = "验证码错误.";
return Json(json);
}
}
else
{
json.Msg = "验证码失效,请刷新.";
return Json(json);
} //获取用户信息
user = admin.GetInfo(name); if (name.ToLower() == "admin")
{
//admin判断区 if (user == null)
{ }
else if (user.iPass != (name + pass).ToHash())
{
json.Msg = "用户名或密码错误.";
return Json(json);
}
}
else if (user == null || user.iPass != (name + pass).ToHash())
{
//其他用户判断区
json.Msg = "用户名或密码错误.";
return Json(json); } user.iLine = true;
user.iTime = user.iTime == null ? 1 : user.iTime + 1;
user.iLast = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
user.iIpad = WebUtlity.GetClientIP();
admin.UpdateInfo(user); HttpCookie userck = Request.Cookies["userGuid"];
OnlineInfo online = null; if (userck == null)
{
guid = StringUtlity.NewGuid();
userck = new HttpCookie("userGuid", guid);
AddInfo(guid,user); }
else
{
guid = userck.Value;
online = Common.Users.GetUser(guid) as OnlineInfo;
if (online == null)
{
AddInfo(guid, user);
}
else
{
online.IsEdit = true;
online.IsOnline = true;
online.LginTime = (int)user.iTime;
online.UserIp = user.iIpad;
online.UserAdd = "";
online.UserHand = user.iLast;
online.UserUrl = "Login";
online.CookieId = guid;
online.UserGuid = user.iGuid;
online.UserCode = user.iCode;
online.UserName = user.iName;
online.UserStar = user.iLast;
online.VistTime = (int)user.iTime;
}
} Response.Cookies.Set(userck); return JavaScript("window.location.reload();");
}
/// 使用单例模式控制线程检测在线人员状态
/// </summary>
private void SetLoopUsers()
{
if (!Singleton<Boolean>.Instance)
{
Singleton<Boolean>.Instance = true;
Thread thCheck = new Thread(CheckOnline) { IsBackground = true, Priority = ThreadPriority.Lowest };
thCheck.Start();
}
} /// <summary>
/// 检测用户状态(60秒一次),若有修改过的则存进数据库,下线的则删除
/// </summary>
private void CheckOnline() { while (true)
{
var keys = new object[Users.GetUser().Count];
Users.GetUser().Keys.CopyTo(keys, 0); for (int i = keys.Length - 1; i >= 0; i--)
{
var onUser = Users.GetUser(keys[i]) as OnlineInfo;
if (onUser != null && onUser.IsEdit)
{
//ToDo update online table
if (!onUser.IsOnline)
{
Users.DelUser(onUser.CookieId);
}
}
}
Thread.Sleep(60000);
}
}