码迷,mamicode.com
首页 > Windows程序 > 详细

ASP.NET C# 实现实时用户在线

时间:2015-03-06 18:44:38      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

public static class UserOnline
{
    /// <summary>
    /// 获取或设置在线列表
    /// </summary>
    public static Hashtable OnlineUserList
    {
        get
        {
            if (HttpContext.Current.Application["OnlineUserList"] == null)
            {
                Hashtable onlineUserList = new Hashtable();
                HttpContext.Current.Application["OnlineUserList"] = onlineUserList;
            }

            return (Hashtable)HttpContext.Current.Application["OnlineUserList"];
        }
        set
        {
            HttpContext.Current.Application["OnlineUserList"] = value;
        }
    }

    /// <summary>
    /// 添加在线成员
    /// </summary>
    public static bool OnlineUserList_Add(string key, string value)
    {
        try
        {
            if (OnlineUserList.Contains(key))
                OnlineUserList[key] = value;
            else
                OnlineUserList.Add(key, value);
            return true;
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// 添加在线成员
    /// </summary>
    public static bool OnlineUserList_Add(string key)
    {
        string value = DateTime.Now.ToString();
        return OnlineUserList_Add(key, value);
    }

    /// <summary>
    /// 离线删除用户
    /// </summary>
    public static bool OnlineUserList_Delete(string key)
    {
        bool re = false;
        if (OnlineUserList.Contains(key))
        {
            Hashtable userList = OnlineUserList;
            userList.Remove(key);
            OnlineUserList = userList;
            return true;
        }
        return re;
    }

    /// <summary>
    /// 判断用户是否在线
    /// </summary>
    public static bool UserIsOnline(string adminName)
    {
        OnlineClearUserOutTimeInOnLineList();
        return OnlineUserList.Contains(adminName) ? true : false;
    }

    /// <summary>
    /// 删除超时在线用户
    /// </summary>
    public static void OnlineClearUserOutTimeInOnLineList()
    {
        int OnlineTimeOut = 20;
        Hashtable list = new Hashtable();
        Hashtable temList = new Hashtable();
        list = OnlineUserList;
        temList = new Hashtable(list);
        foreach (DictionaryEntry de in temList)
        {
            //删除超时
            DateTime onlineTime = Convert.ToDateTime(de.Value);
            TimeSpan timeSpan = DateTime.Now - onlineTime;

            //在线时间和当前时间间隔大于超时分钟数就删除(注:用户非法关闭浏览器)
            if (timeSpan.TotalMinutes >= (double)OnlineTimeOut)
            {
                list.Remove(de.Key);
            }

        }

        OnlineUserList = list;
    }

}

 

ASP.NET C# 实现实时用户在线

标签:

原文地址:http://www.cnblogs.com/xiaosa-blog/p/4318869.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!