Eddie005(♂) 暴赱 『零零伍』兄在吗?我想使用您的在线会员列表与统计功能,可是不知道如何用啊,其他的路过的各位也帮我看看吧,谢谢了

fphuang 2005-03-30 09:03:59
总的来说,要做个在线人数统计简单,但是要做在线名单并且保存用户的访问日志,就需要耗费比较多的系统资源,是否划算就难说了(我只看需求文档,其他不管...);

前面用过的IHttpModule方法也不错,原先每用过,也学了一招...

感谢思归老大的帮忙,分就散了吧~

using 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();
}


}
}
...全文
174 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
stoneyu 2005-07-09
  • 打赏
  • 举报
回复
學習.........

fphuang 2005-03-30
  • 打赏
  • 举报
回复
在线列表功能好用了!
fphuang 2005-03-30
  • 打赏
  • 举报
回复
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];
---------------------------------------------------------------
//保存到数据库
string s="a insert into [online]([UserID],[UserName]) values('"+curRow["UserID"].ToString()+"','"+curRow["UserName"].ToString()+"')";
string strConnection = "";
string dbname = ".\\database\\issuebase1.mdb";
strConnection = @"Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " + System.Web.HttpContext.Current.Server.MapPath (dbname) + ";";
OleDbConnection cn = new OleDbConnection(strConnection);
cn.Open();

OleDbCommand OleDbCmd;
OleDbCmd =new OleDbCommand(s,cn);
OleDbCmd.ExecuteNonQuery();
cn.Close();
--------------------------------------------------------

curRow.Delete();


}
userTable.AcceptChanges();

Application.Lock();
Application["UserOnLine"] = userTable;
Application.UnLock();
}

to: Eddie005(♂) 暴赱 『零零伍』

还是没有保存到数据库中阿,应该如何写
fphuang 2005-03-30
  • 打赏
  • 举报
回复
to: Eddie005(♂) 暴赱 『零零伍』
多谢!我试试。
afanti_nj 2005-03-30
  • 打赏
  • 举报
回复
这代码我很喜欢收藏起来了~~~
Eddie005 2005-03-30
  • 打赏
  • 举报
回复
1)保存数据库需要在方法ScheduledWorkCallback里面的 curRow.Delete();前把 curRow这一行的数据保存,代码需要自己写了

2)想在页面显示在线名单,则
DataTable dt = Application["UserOnLine"] as DataTable;
DataGrid1.DataSource = dt;
DataGrid1.DataBind();就可以了

===================
不直接用户据库是有原因的,Application 变量爆掉的可能性没有考虑,需要做个测试才知道
akinggmx 2005-03-30
  • 打赏
  • 举报
回复
学习,最近也要用到统计在线人数
fphuang 2005-03-30
  • 打赏
  • 举报
回复
to: drk928(一起看斜阳)
你好!谢谢您的回复,我是按照上面的原文考过来的,只是把
//保存到数据库
XsStudio.Database db = new XsStudio.Database();
--------------------------------------------------------------------
这句改为 string s="insert into table_name() values()";
OleDbCmd =new OleDbCommand(s,myDb_Conn2.Conn);
OleDbCmd.ExecuteNonQuery();
使其保存到数据库中,编译的时候倒是没有错误,可是也没有什么东西保存到我的数据库当中,所以——————,总之吧我就是不知道怎么用
SeeSunSet 2005-03-30
  • 打赏
  • 举报
回复
干嘛不直接用数据库啊?你不怕你的Application 变量爆掉了吗?
fphuang 2005-03-30
  • 打赏
  • 举报
回复
我在数据库中建立了一个数据表,字段如下:
userID;
userName;
FirstRequestTime
ClientIP
ClientName
ClientAgent
fphuang 2005-03-30
  • 打赏
  • 举报
回复
我将上面的代码几乎没有改变的考到我的Global.asax.cs文件中了,可是我不知道如何的使用,希望谁帮我解释解释,
1、比如如何保存到数据库中。
2、如何把Application["UserOnLine"] 表中的数据显示到其他的页面中。

62,039

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧