最近所做的一個項目需要用到的在線用戶列表
我的大概思路是
假設我們設置會話超時時間為
==========================================
具體實現如下
/// <summary>
/// 單個在線用戶數據
/// </summary>
public sealed class ActiveUser
{
private readonly string _ticket; //票據名稱
private readonly string _username; //登陸用戶名
private readonly string _truename; //登陸用戶名
private readonly string _roleid; //角色
private readonly DateTime _refreshtime; //最新刷新時間
private readonly DateTime _activetime; //最新活動時間
private readonly string _clientip; //登陸IP
public ActiveUser(string Ticket
this
this
this
this
this
this
this
}
public ActiveUser(string Ticket
this
this
this
this
this
this
this
}
public string Ticket { get{return _ticket;} }
public string UserName { get{return _username;} }
public string TrueName { get{return _truename;} }
public string RoleID { get{return _roleid;} }
public DateTime RefreshTime { get{return _refreshtime;} }
public DateTime ActiveTime { get{return _activetime;} }
public string ClientIP { get{return _clientip;} }
}
/// <summary>
/// PassPort 存儲在線用戶列表
/// </summary>
public class PassPort
{
private static DataTable _activeusers;
private int _activeTimeout;
private int _refreshTimeout;
/// <summary>
/// 初始化在線用戶表
/// </summary>
private void userstableFormat()
{
if(_activeusers==null) {
_activeusers = new DataTable(
DataColumn myDataColumn;
System
mystringtype = System
System
mytimetype = System
myDataColumn = new DataColumn(
_activeusers
myDataColumn = new DataColumn(
_activeusers
myDataColumn = new DataColumn(
_activeusers
myDataColumn = new DataColumn(
_activeusers
myDataColumn = new DataColumn(
_activeusers
myDataColumn = new DataColumn(
_activeusers
myDataColumn = new DataColumn(
_activeusers
}
}
public PassPort()
{
userstableFormat(); //初始化在線用戶表
//活動超時時間初始化 單位
try { _activeTimeout=int
catch{ _activeTimeout=
//自動刷新超時時間初始化 單位
try { _refreshTimeout=int
catch{ _refreshTimeout=
}
//全部用戶列表
public DataTable ActiveUsers
{
get{return _activeusers
}
/// <summary>
/// 新用戶登陸
/// </summary>
public void Login(ActiveUser user
{
DelTimeOut(); //清除超時用戶
if(SingleLogin){
//若是單人登陸則注銷原來登陸的用戶
this
}
DataRow myRow;
try
{
myRow = _activeusers
myRow[
myRow[
myRow[
myRow[
myRow[
myRow[
myRow[
_activeusers
}
catch(Exception e)
{
throw(new Exception(e
}
_activeusers
}
/// <summary>
///用戶注銷
/// </summary>
private void Logout(string strUserKey
{
DelTimeOut(); //清除超時用戶
strUserKey=strUserKey
string strExpr;
strExpr =byTicket ?
DataRow[] curUser;
curUser = _activeusers
if (curUser
{
for(int i =
{
curUser[i]
}
}
_activeusers
}
/// <summary>
///用戶注銷
/// </summary>
/// <param name=
public void Logout(string strTicket){
this
}
/// <summary>
///清除超時用戶
/// </summary>
private bool DelTimeOut()
{
string strExpr;
strExpr =
DataRow[] curUser;
curUser = _activeusers
if (curUser
{
for(int i =
{
curUser[i]
}
}
_activeusers
return true;
}
/// <summary>
///更新用戶活動時間
/// </summary>
public void ActiveTime(string strTicket)
{
DelTimeOut();
string strExpr;
strExpr =
DataRow[] curUser;
curUser = _activeusers
if (curUser
{
for(int i =
{
curUser[i][
curUser[i][
}
}
_activeusers
}
/// <summary>
///更新系統自動刷新時間
/// </summary>
public void RefreshTime(string strTicket)
{
DelTimeOut();
string strExpr;
strExpr =
DataRow[] curUser;
curUser = _activeusers
if (curUser
{
for(int i =
{
curUser[i][
}
}
_activeusers
}
private ActiveUser SingleUser(string strUserKey
{
strUserKey=strUserKey
string strExpr;
ActiveUser myuser;
strExpr =byTicket ?
DataRow[] curUser;
curUser = _activeusers
if (curUser
{
string myTicket=(string)curUser[
string myUser=(string)curUser[
string myName=(string)curUser[
string myRoleID=(string)curUser[
DateTime myActiveTime=(DateTime)curUser[
DateTime myRefreshtime=(DateTime)curUser[
string myClientIP =(string)curUser[
myuser=new ActiveUser(myTicket
}
else
{
myuser=new ActiveUser(
}
return myuser;
}
/// <summary>
///按Ticket獲取活動用戶
/// </summary>
public ActiveUser SingleUser_byTicket(string strTicket)
{
return this
}
/// <summary>
///按UserName獲取活動用戶
/// </summary>
public ActiveUser SingleUser_byUserName(string strUserName)
{
return this
}
/// <summary>
///按Ticket判斷用戶是否在線
/// </summary>
public bool IsOnline_byTicket(string strTicket)
{
return (bool)(this
}
/// <summary>
///按UserName判斷用戶是否在線
/// </summary>
public bool IsOnline_byUserName(string strUserName)
{
return (bool)(this
}
}
/// <summary>
/// Refresh 執行更新自動刷新時間操作
/// </summary>
public class Refresh: PlaceHolder
{
/// <summary>
/// 設置存儲Ticket的Session名稱
/// </summary>
public virtual string SessionName
{
get{
object obj
if (obj
return
}
set{
this
}
}
protected override void Render(HtmlTextWriter writer)
{
string myTicket=(string)this
if(myTicket!=null)
{
PassPort myPass = new PassPort();
myPass
writer
}
else{
writer
}
base
}
}
/// <summary>
/// Script 生成執行xmlhttp的js腳本
/// </summary>
public class Script: PlaceHolder
{
/// <summary>
/// 設置js自動刷新的間隔時間
/// </summary>
public virtual int RefreshTime
{
get
{
object obj
if (obj
return
}
set
{
this
}
}
protected override void Render(HtmlTextWriter writer)
{
//從nfig中讀取xmlhttp的訪問地址
string refreshUrl=(string)ConfigurationSettings
string scriptString = @
scriptString += @
scriptString += @
scriptString += @
scriptString += @
scriptString += @
scriptString += @
scriptString += @
scriptString += @
scriptString += @
//scriptString += @
scriptString += @
scriptString += @
scriptString += @
scriptString += @
scriptString += @
scriptString += @
scriptString += @
writer
writer
writer
base
}
}
注意以上四個類同屬於一個名為OnlineUser的工程
下面我簡單介紹一下調用方法
<appSettings>
<add key=
<add key=
<add key=
</appSettings>
string myTicket=(string)this
if(myTicket!=null)
{
OnlineUser
if(myPassPort
{
myPassPort
DataGrid
DataGrid
}
else{
//若在線用戶列表中找不到當前用戶
Response
}
}
else{
Response
}
if(TextBox
{
//不能為空
String scriptString = @
scriptString += @
scriptString += @
scriptString += @
scriptString += @
scriptString += @
if(!this
this
}
else{
OnlineUser
string myTicket=DateTime
string myUser=TextBox
string myClintIP=this
this
OnlineUser
myPassPort
Response
}
OnlineUser
myPassPort
this
<%@ Register TagPrefix=
<%@ Page %>
<cc
把Refresh
===============================================
下面進行功能測試
x
From:http://tw.wingwit.com/Article/program/net/201311/13149.html