用asp.net显示在线登陆人数及位置
http://topic.csdn.net/t/20041231/16/3692181.html
Global.aspx
http://topic.csdn.net/t/20041231/16/3692181.html
Global.aspx
Code
private static System.Threading.Timer timer;
//检查在线用户的间隔时间默认20分钟 60000 * 20
private const int interval = 60000 * 5;
private void ScheduledWorkCallback(object sender)
{
//LastRequestTime 4点 第二个当前事件是4.10 减去20分钟 是 3.50 4点 < 3.50 没有这个数据不删除。
// 第二个当前事件是4.40 减去20分钟 是 4.20 4点 < 4.20 删除。
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();
}
protected void Application_Start(object sender, EventArgs e)
{
//在global.asax做一个Timer周期性地检查
//每隔20分钟执行一次ScheduledWorkCallback函数
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.PrimaryKey = new DataColumn[] { userTable.Columns[0] };
userTable.AcceptChanges();
Application.Lock();
Application["UserOnLine"] = userTable;
Application.UnLock();
}
页面调用private static System.Threading.Timer timer;
//检查在线用户的间隔时间默认20分钟 60000 * 20
private const int interval = 60000 * 5;
private void ScheduledWorkCallback(object sender)
{
//LastRequestTime 4点 第二个当前事件是4.10 减去20分钟 是 3.50 4点 < 3.50 没有这个数据不删除。
// 第二个当前事件是4.40 减去20分钟 是 4.20 4点 < 4.20 删除。
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();
}
protected void Application_Start(object sender, EventArgs e)
{
//在global.asax做一个Timer周期性地检查
//每隔20分钟执行一次ScheduledWorkCallback函数
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.PrimaryKey = new DataColumn[] { userTable.Columns[0] };
userTable.AcceptChanges();
Application.Lock();
Application["UserOnLine"] = userTable;
Application.UnLock();
}
Code
public void online()
{
AjaxControl myCtrl = new AjaxControl("Online", this.Page);
Literal OnLine = myCtrl.AjaxLtr("OnLine");
if (Session == null) return;
if (Session["UserID"].ToString() == "0") return;
string userID = Session["UserID"].ToString();
string userName = Session["UserName"].ToString();
DataTable userTable = (DataTable)Application["UserOnLine"];
DataRow curRow = userTable.Rows.Find(new object[] { userID });
if (curRow != null)
{
this.GetDataRowFromHttpApp(curRow);
}
else
{
DataRow newRow = userTable.NewRow();
this.GetDataRowFromHttpApp(newRow);
userTable.Rows.Add(newRow);
}
userTable.AcceptChanges();
Application.Lock();
Application["UserOnLine"] = userTable;
Application.UnLock();
string Table = "";
if (userTable.Rows.Count != 0)
{
Table = "<ul>";
for (int i = 0; i < userTable.Rows.Count; i++)
{
Table += "<li class=\"olw1\">" + userTable.Rows[i][1].ToString() + "</li>";
}
Table += "</ul>";
}
OnLine.Text = Table;
response = myCtrl.GetString();
}
public void LogOut()
{
DataTable userTable = (DataTable)Application["UserOnLine"];
DataRow[] lineOutUsers = userTable.Select("UserID=" + Session["UserID"].ToString());
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();
UserHelper.SetUserSession("", "", 2, 0, 0, 0, 0, 0, DateTime.Now);
Session.Abandon();
response = "Default.aspx";
}
private void GetDataRowFromHttpApp(DataRow mRow)
{
if (Session == null) return;
if (Session["UserID"].ToString() == "0") return;
string userID = Session["UserID"].ToString();
string userName = Session["UserName"].ToString();
if (mRow["UserID"].ToString().Length < 1)
{
mRow["UserID"] = userID;
mRow["UserName"] = userName;
mRow["FirstRequestTime"] = System.DateTime.Now;
}
mRow["LastRequestTime"] = System.DateTime.Now;
//mRow["LastRequestPath"] = requestPath;
}
public void online()
{
AjaxControl myCtrl = new AjaxControl("Online", this.Page);
Literal OnLine = myCtrl.AjaxLtr("OnLine");
if (Session == null) return;
if (Session["UserID"].ToString() == "0") return;
string userID = Session["UserID"].ToString();
string userName = Session["UserName"].ToString();
DataTable userTable = (DataTable)Application["UserOnLine"];
DataRow curRow = userTable.Rows.Find(new object[] { userID });
if (curRow != null)
{
this.GetDataRowFromHttpApp(curRow);
}
else
{
DataRow newRow = userTable.NewRow();
this.GetDataRowFromHttpApp(newRow);
userTable.Rows.Add(newRow);
}
userTable.AcceptChanges();
Application.Lock();
Application["UserOnLine"] = userTable;
Application.UnLock();
string Table = "";
if (userTable.Rows.Count != 0)
{
Table = "<ul>";
for (int i = 0; i < userTable.Rows.Count; i++)
{
Table += "<li class=\"olw1\">" + userTable.Rows[i][1].ToString() + "</li>";
}
Table += "</ul>";
}
OnLine.Text = Table;
response = myCtrl.GetString();
}
public void LogOut()
{
DataTable userTable = (DataTable)Application["UserOnLine"];
DataRow[] lineOutUsers = userTable.Select("UserID=" + Session["UserID"].ToString());
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();
UserHelper.SetUserSession("", "", 2, 0, 0, 0, 0, 0, DateTime.Now);
Session.Abandon();
response = "Default.aspx";
}
private void GetDataRowFromHttpApp(DataRow mRow)
{
if (Session == null) return;
if (Session["UserID"].ToString() == "0") return;
string userID = Session["UserID"].ToString();
string userName = Session["UserName"].ToString();
if (mRow["UserID"].ToString().Length < 1)
{
mRow["UserID"] = userID;
mRow["UserName"] = userName;
mRow["FirstRequestTime"] = System.DateTime.Now;
}
mRow["LastRequestTime"] = System.DateTime.Now;
//mRow["LastRequestPath"] = requestPath;
}