天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

97 lines
2.7 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Applications.Session
{
public class SessionEngine
{
public SessionEngine()
{
Load();
}
List<SessionInfo> SessionList = null;//会话列表
System.Timers.Timer tmr = null;
/// <summary>
/// 装载服务定义
/// </summary>
private void Load()
{
tmr = new System.Timers.Timer();
tmr.Interval = 60000;
tmr.Elapsed += new System.Timers.ElapsedEventHandler(tmr_Elapsed);
tmr.Start();
}
/// <summary>
/// 处理过期的会话
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
TimeSpan ts = Common.TextUtil.DateTimeUtil.DateDiff(DateTime.Now);
var s = SessionList.FindAll(p => Common.TextUtil.DateTimeUtil.DateDiff(p.UpdateTime).TotalMinutes > 20);
for (int i = s.Count; i >= 0; i--)
{
SessionList.RemoveAt(i);
}
}
/// <summary>
/// 向会话中添加用户,已存在时更新会话
/// </summary>
/// <param name="user">用户会话信息</param>
public void AddUser(SessionInfo user)
{
user.LogonTime = DateTime.Now;
user.UpdateTime = DateTime.Now;
var s = SessionList.Find(p => p.IP == user.IP);
if (s == null)
{
SessionList.Add(user);
}
else
{
if (s.UserName == user.UserName)
{
s.Port = user.Port;
s.UpdateTime = DateTime.Now;
}
else
{
SessionList.Remove(s);
SessionList.Add(user);
}
}
}
/// <summary>
/// 注销指定用户的会话状态
/// </summary>
/// <param name="user">用户会话信息</param>
public void RemoveUser(SessionInfo user)
{
var s = SessionList.Find(p => p.IP == user.IP);
if (s != null)
{
SessionList.Remove(s);
}
}
/// <summary>
/// 查找指定用户
/// </summary>
/// <param name="userName">用户姓名</param>
/// <returns></returns>
public SessionInfo FindUser(string userName)
{
return SessionList.Find(p => p.UserName == userName);
}
}
}