本帖最后由 rindybo 于 2010-06-27 13:41:50 编辑

解决方案 »

  1.   

    Singleton<OnlineInfo> 是一个Singleton<T>,单件模式实现的类,没有做类是的在线统计,请大大们看看这样设计合理吗
      

  2.   

    把我做的登录的代码也放出来吧 这样清晰一些[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();");
            }
      

  3.   

    谢谢各位指点我后台有个线程 每一分钟就把更改状态的用户更新到数据库中 贴出代码来  /// <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);
                }
            }
      

  4.   

    discuz 论坛有这个功能,可以参考下