现在想做一个WEB在线即时通,就是当用户登录后就显示在线!然后可以聊天!
谁有这方面的资料也可以发上来!怎么设计会比较好点!比如聊天纪录保存等?
------解决方案--------------------------------------------------------
给你个在线用户代码,你在加聊天功能吧。
- C# code
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;using System.Configuration;using SDT.Info.Active;using SDT.Info;namespace SDT.Active.ActiveUser{ /// <summary> /// PassPort 存储在线用户列表。 /// </summary> public class PassPort { private static SortedList<int,ActiveUserInfo> _activeusers; private static List<UserEmployeeInfo> _userInfos; private static PassPort _instance; private static readonly object obj = new object(); /// <summary> /// 初始化在线用户表。 /// </summary> private void userstableFormat() { if (_activeusers == null) { _activeusers = new SortedList<int, ActiveUserInfo>(); _userInfos = new List<UserEmployeeInfo>(); } } public static PassPort Instance() { if (_instance == null) { _instance = new PassPort(); } return _instance; } public int ActiveTimeout { get { return Config.SysBaseConfig.GetInstance().ServoActiveTimeout; } } public int RefreshTimeout { get { return Config.SysBaseConfig.GetInstance().ServoActiveTimeout * 2; } } private PassPort() { userstableFormat(); } /// <summary> /// 获取全部用户列表 /// </summary> /// <returns></returns> public List<UserEmployeeInfo> GetActiveUsers() { return _userInfos; } /// <summary> /// 获取在线用户 /// </summary> /// <param name="sysNo">用户SysNo</param> /// <returns></returns> public UserEmployeeInfo GetActiveUserBySysNo(int sysNo) { return _userInfos.First( p => p.SysNo == sysNo); } /// <summary> /// 用户集合 /// </summary> /// <param name="userAttr">用户属性</param> /// <returns></returns> public List<UserEmployeeInfo> GetActiveUsersByUserAttr(AppEnum.UserAttr userAttr) { return _userInfos.Where(p => p.Attr == userAttr).ToList<UserEmployeeInfo>(); } /// <summary> /// 新用户登陆。 /// </summary> public void Login(UserEmployeeInfo userInfo) { DelTimeOut(); //清除超时用户 //若是单人登陆则注销原来登陆的用户 this.Logout(userInfo.SysNo); try { ActiveUserInfo activeUserInfo = new ActiveUserInfo(userInfo.SysNo,userInfo); _activeusers.Add(userInfo.SysNo, activeUserInfo); _userInfos.Add(userInfo); } catch (Exception e) { throw (new Exception(e.Message)); } } /// <summary> ///用户注销,根据Ticket或UserName。 /// </summary> public void Logout(int userSysNo) { DelTimeOut(); //清除超时用户 _activeusers.Remove(userSysNo); } /// <summary> ///清除超时用户。 /// </summary> private bool DelTimeOut() { DateTime dtNow = DateTime.Now; List<ActiveUserInfo> removeActiveUser = _activeusers.Values.Where(p => DateTime.Compare(p.RefreshTime, dtNow.AddSeconds(- RefreshTimeout)) < 0).ToList<ActiveUserInfo>(); foreach (ActiveUserInfo activeUser in removeActiveUser) { _activeusers.Remove(activeUser.SysNo); } return true; } /// <summary> ///更新用户活动时间。 /// </summary> public void ActiveTime(int userSysNo) { DelTimeOut(); if (_activeusers.ContainsKey(userSysNo)) { ActiveUserInfo activeUser = _activeusers[userSysNo]; activeUser.ActiveTime = DateTime.Now; activeUser.RefreshTime = DateTime.Now; } } }}