我用vb.net。
对于计数器,网上搜索的时候大部分都是用Application 做+1、-1的工作。好一些的就是存到一个text中,却没有对客户端的异常情况进行处理,并且实时性很差。哪里有更好的办法计数器呢?这么成熟的网站了,怎么都没有好的计数器啊?
至于在线用户列表,就很更麻烦了,郁闷我。有没有好的啊!

解决方案 »

  1.   

    你可以考虑在ASP.NET管道中处理,可以实现一个IHttpModule接口来做UserTrace Module。
    实现该接口时只要在初始化的时候挂上AuthorizeRequest事件的处理函数即可,该函数来完成用户的跟踪功能。
    你可以将用户的信息保存在一个HashTable或者其他什么集合中(数据结构由你自己的需要来定),再保存到Application或者Cache这些全局持久的对象中即可。--个人意见,仅供参考。
      

  2.   

    我去年应该有个帖子讨论过这个问题,不知道为什么现在找不到了,实现方法cuike519说的差不多,总结来说在性能上面牺牲的代价蛮高的,正是因为这个原因才没有比较好的统计在线名单的东西
      

  3.   

    参考
    http://community.csdn.net/Expert/topic/3692/3692181.xml?temp=.9495355using System;
    using System.ComponentModel;
    using System.Web;
    using System.Web.SessionState;
    using System.Data;
    using System.Data.OleDb;namespace XsExam
    {
    /// <summary>
    /// Global 的摘要说明。
    /// </summary>
    public class Global : System.Web.HttpApplication
    {
            private static System.Threading.Timer timer;
            private const int interval = 1000 * 60 * 10;//检查在线用户的间隔时间
            
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;public Global()
    {
    InitializeComponent();
    }protected void Application_Start(Object sender, EventArgs e)
    {
                if (timer == null)
                    timer = new System.Threading.Timer(new System.Threading.TimerCallback(ScheduledWorkCallback),
                        sender, 0, interval);            DataTable userTable = new DataTable();
                userTable.Columns.Add("UserID");//用户ID
                userTable.Columns.Add("UserName");//用户姓名
                userTable.Columns.Add("FirstRequestTime");//第一次请求的时间
                userTable.Columns.Add("LastRequestTime");//最后一次请求的时间
                userTable.Columns.Add("ClientIP");//
                userTable.Columns.Add("ClientName");//
                userTable.Columns.Add("ClientAgent");//
                //userTable.Columns.Add("LastRequestPath");//最后访问的页面            userTable.PrimaryKey = new DataColumn[]{userTable.Columns[0]};
                userTable.AcceptChanges();            Application.Lock();
                Application["UserOnLine"] = userTable;
                Application.UnLock();
    }
     
    protected void Session_Start(Object sender, EventArgs e)
    {        }        protected void Application_BeginRequest(Object sender, EventArgs e)
    {
                
    }protected void Application_EndRequest(Object sender, EventArgs e)
    {}        protected void Application_AcquireRequestState(Object sender, EventArgs e)
            {
                HttpApplication mApp = (HttpApplication)sender;
                if(mApp.Context.Session == null) return;
                if(mApp.Context.Session["UserID"]==null ) return;
                string userID = mApp.Context.Session["UserID"].ToString();
                
                DataTable userTable = (DataTable)Application["UserOnLine"];
                DataRow curRow = userTable.Rows.Find(new object[]{userID});
                if(curRow != null)
                {
                    this.GetDataRowFromHttpApp(mApp,ref curRow);
                }
                else
                {
                    DataRow newRow = userTable.NewRow();
                    this.GetDataRowFromHttpApp(mApp,ref newRow);
                    userTable.Rows.Add(newRow);
                }
                userTable.AcceptChanges();            Application.Lock();
                Application["UserOnLine"] = userTable;
                Application.UnLock();        }protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
               
    }protected void Application_Error(Object sender, EventArgs e)
    {
                
    }       
    protected void Session_End(Object sender, EventArgs e)
    {
                
    }protected void Application_End(Object sender, EventArgs e)
    {
                
    }#region Web 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
                this.components = new System.ComponentModel.Container();
                
            }
    #endregion        private void GetDataRowFromHttpApp(HttpApplication mApp,ref DataRow mRow)
            {
                if(mApp.Context.Session == null) return;
                if(mApp.Context.Session["UserID"]==null || mApp.Context.Session["UserName"]==null) return;
                string userID = mApp.Context.Session["UserID"].ToString();
                string userName = mApp.Context.Session["UserName"].ToString();
                //string requestPath = mApp.Request.Path;
                
                if(mRow["UserID"].ToString().Length<1)
                {
                    mRow["UserID"]      = userID;
                    mRow["UserName"]    = userName;
                    mRow["FirstRequestTime"] = System.DateTime.Now;
                    mRow["ClientIP"]    = mApp.Context.Request.UserHostAddress;
                    mRow["ClientName"]  = mApp.Context.Request.UserHostName;
                    mRow["ClientAgent"] = mApp.Context.Request.UserAgent;
                }            mRow["LastRequestTime"] = System.DateTime.Now;
                //mRow["LastRequestPath"] = requestPath;
                
            }        private void ScheduledWorkCallback (object sender)
            {
                string filter = "Convert(LastRequestTime,'System.DateTime') < Convert('" + System.DateTime.Now.AddSeconds(-interval/1000).ToString() + "','System.DateTime')";
                DataTable userTable = (DataTable)Application["UserOnLine"];
                DataRow[] lineOutUsers = userTable.Select(filter);
                for(int i=0;i<lineOutUsers.Length;i++)
                {
                    DataRow curRow = lineOutUsers[i];
                    
                    //保存到数据库
                    XsStudio.Database db = new XsStudio.Database();
           curRow.Delete();
                    
                }
                userTable.AcceptChanges();            Application.Lock();
                Application["UserOnLine"] = userTable;
                Application.UnLock();
            }       
    }
    }
      

  4.   

    最麻烦的就是不能知道用户意外退出的情况,所以计算不准确
    楼主参考:
    http://blog.csdn.net/cncxz/archive/2005/07/18/427778.aspx
      

  5.   

    主要方法:
    1、HttpModule
    2、session+application
    3、结合数据库不断更新用户在线时间,超过一定时间作为过时(此方法还有基于xmlhttp的改良版本)
      

  6.   

    http://blog.csdn.net/cncxz/archive/2005/07/18/427778.aspx
      

  7.   

    看看这个,我对原来的刚做了一点点修改。
    http://blog.csdn.net/cncxz/archive/2005/07/18/427778.aspx
      

  8.   

    http://jillzhang.cnblogs.com/archive/2006/04/12/373566.html
    判断用户什么时候离开,以什么方式离开
      

  9.   

    其实,楼上的方法不错,但是在服务器端无休止Refresh,而且Refresh的次数也是随着Online User  More to More,效率可想而知,如果应用在大型网站,在线人数如果过w的话,那需要占用多少服务器资源,虽然每次Refresh并一定做多少事情.理想的想,如果用户在意外退出之后能以光速跑到管理员来说,我退出啦,然后管理员将其删除,那就太好了。所以要想即解决这个问题,又不浪费性能,最先考虑的方法还是让用户发信息给我们,告诉我们他要退出啦,赶紧吧我删掉吧。