init lserp cs 5.0
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using wojilu.Web.Jobs;
|
||||
|
||||
namespace wojilu.Common.Onlines {
|
||||
|
||||
|
||||
public class OnlineJob : IWebJobItem {
|
||||
|
||||
private static readonly ILog logger = LogManager.GetLogger( typeof( OnlineJob ) );
|
||||
|
||||
public void Execute() {
|
||||
deleteTimeoutVisitor();
|
||||
updateMaxOnline();
|
||||
}
|
||||
|
||||
public void End() {
|
||||
}
|
||||
|
||||
private static void deleteTimeoutVisitor() {
|
||||
|
||||
logger.Info( "---------------deleteTimeoutVisitor---------------" );
|
||||
|
||||
List<OnlineUser> allVisitors = cdb.findAll<OnlineUser>();
|
||||
for (int i = 0; i < allVisitors.Count; i++) {
|
||||
|
||||
OnlineUser online = allVisitors[i] as OnlineUser;
|
||||
if (online == null) continue;
|
||||
TimeSpan span = DateTime.Now.Subtract( online.LastActive );
|
||||
try {
|
||||
if (span.TotalMinutes > 20) online.delete();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
logger.Error( "DeleteTimeoutVisitor:" + ex );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static void updateMaxOnline() {
|
||||
|
||||
logger.Info( "---------------updateMaxOnline---------------" );
|
||||
|
||||
|
||||
if (OnlineStats.Instance.Count <= config.Instance.Site.MaxOnline) return;
|
||||
|
||||
config.Instance.Site.MaxOnline = OnlineStats.Instance.Count;
|
||||
config.Instance.Site.MaxOnlineTime = DateTime.Now;
|
||||
|
||||
config.Instance.Site.Update( "MaxOnline", OnlineStats.Instance.Count );
|
||||
config.Instance.Site.Update( "MaxOnlineTime", DateTime.Now );
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Zhaizj.Framework.Web.Context;
|
||||
using Zhaizj.Framework.Web;
|
||||
using Zhaizj.Framework.Web.Mvc;
|
||||
|
||||
namespace Zhaizj.Framework.Common.Onlines {
|
||||
|
||||
/// <summary>
|
||||
/// 在线管理器
|
||||
/// </summary>
|
||||
public class OnlineManager {
|
||||
|
||||
private static readonly ILog logger = LogManager.GetLogger( typeof( OnlineManager ) );
|
||||
|
||||
public static void Refresh( MvcContext ctx ) {
|
||||
|
||||
if (isAgentSpider( ctx )) return;
|
||||
|
||||
String sid = getSessionId( ctx );
|
||||
List<OnlineUser> users = cdb.findByName<OnlineUser>( sid );
|
||||
Boolean isNew = (users.Count == 0 ? true : false);
|
||||
|
||||
if (isNew) {
|
||||
addNewVisitor( ctx, sid );
|
||||
}
|
||||
else {
|
||||
updateVisitor( ctx, users[0] );
|
||||
}
|
||||
|
||||
|
||||
deleteSameUser( ctx, sid );
|
||||
}
|
||||
|
||||
|
||||
private static Boolean isAgentSpider( MvcContext ctx ) {
|
||||
|
||||
String agent = ctx.web.ClientAgent;
|
||||
if (agent == null) return true;
|
||||
|
||||
foreach (String spiderName in config.Instance.Site.Spider) {
|
||||
|
||||
if (agent.ToLower().IndexOf( spiderName.ToLower() ) >= 0) {
|
||||
return true; // TODO 记录spider在线情况
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private static void updateVisitor( MvcContext ctx, OnlineUser myOnline ) {
|
||||
populateOnline( myOnline, ctx );
|
||||
Dictionary<String, Object> dic = getIndexMap( myOnline );
|
||||
myOnline.updateByIndex( dic );
|
||||
}
|
||||
|
||||
private static void addNewVisitor( MvcContext ctx, String sid ) {
|
||||
OnlineUser visitor = new OnlineUser();
|
||||
visitor.Name = sid;
|
||||
visitor.StartTime = DateTime.Now;
|
||||
|
||||
populateOnline( visitor, ctx );
|
||||
|
||||
Dictionary<String, Object> dic = getIndexMap( visitor );
|
||||
visitor.insertByIndex( dic );
|
||||
|
||||
|
||||
if (ctx.viewer != null && ctx.viewer.Id > 0) OnlineStats.Instance.AddMemberCount();
|
||||
}
|
||||
|
||||
// 检查是否有同名的登录用户
|
||||
private static void deleteSameUser( MvcContext ctx, String sid ) {
|
||||
|
||||
if (ctx.viewer == null) return;
|
||||
if (ctx.viewer.Id <= 0) return;
|
||||
|
||||
List<OnlineUser> sameUsers = cdb.findBy<OnlineUser>( "UserName", ctx.viewer.obj.Name );
|
||||
if (sameUsers.Count == 0) return;
|
||||
|
||||
foreach (OnlineUser u in sameUsers) {
|
||||
if (u.Name == sid) continue;
|
||||
cdb.delete( u );
|
||||
OnlineStats.Instance.SubtractMemberCount();
|
||||
}
|
||||
}
|
||||
|
||||
private static Dictionary<String, Object> getIndexMap( OnlineUser visitor ) {
|
||||
Dictionary<String, Object> dic = new Dictionary<String, Object>();
|
||||
dic.Add( "Name", visitor.Name );
|
||||
dic.Add( "UserName", visitor.UserName );
|
||||
return dic;
|
||||
}
|
||||
|
||||
private static void populateOnline( OnlineUser visitor, MvcContext ctx ) {
|
||||
|
||||
visitor.Role = "";
|
||||
visitor.Ip = ctx.Ip;
|
||||
visitor.TrueIp = ctx.Ip;
|
||||
visitor.Agent = strUtil.CutString( ctx.web.ClientAgent, 240 );
|
||||
|
||||
if (ctx.viewer != null && ctx.viewer.obj != null) {
|
||||
|
||||
visitor.UserName = ctx.viewer.obj.Name;
|
||||
visitor.UserId = ctx.viewer.Id;
|
||||
visitor.UserUrl = Link.ToMember( ctx.viewer.obj );
|
||||
visitor.UserPicUrl = ctx.viewer.obj.PicSmall;
|
||||
}
|
||||
else { // 游客
|
||||
|
||||
visitor.UserName = "guest";
|
||||
visitor.UserId = 0;
|
||||
visitor.UserUrl = "";
|
||||
visitor.UserPicUrl = "";
|
||||
|
||||
}
|
||||
|
||||
if (ctx.owner != null && ctx.owner.obj != null)
|
||||
visitor.Target = ctx.owner.obj.Name;
|
||||
|
||||
visitor.IsHidden = 0;
|
||||
|
||||
String referrer = "";
|
||||
if (ctx.web.PathReferrer != null) referrer = ctx.web.PathReferrer;
|
||||
visitor.Referrer = referrer;
|
||||
|
||||
visitor.LastActive = DateTime.Now;
|
||||
visitor.Location = getLocation( ctx );
|
||||
}
|
||||
|
||||
|
||||
private static String getSessionId( MvcContext ctx ) {
|
||||
|
||||
String sidstring = "sessionId";
|
||||
|
||||
String cookieValue = ctx.web.CookieGet( sidstring );
|
||||
|
||||
if (strUtil.HasText( cookieValue )) {
|
||||
|
||||
return cookieValue;
|
||||
}
|
||||
|
||||
else {
|
||||
return setGuestSessionId( ctx, sidstring );
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static String setGuestSessionId( MvcContext ctx, String sidstring ) {
|
||||
String guidSessionId = getGuidString();
|
||||
ctx.web.CookieSet( sidstring, guidSessionId );
|
||||
return guidSessionId;
|
||||
}
|
||||
|
||||
|
||||
private static String getGuidString() {
|
||||
return Guid.NewGuid().ToString().Replace( "-", "" );
|
||||
}
|
||||
|
||||
// TODO 精确描述当前位置
|
||||
private static String getLocation( MvcContext ctx ) {
|
||||
String result = ctx.url.PathAndQuery;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Zhaizj.Framework.Web.Context;
|
||||
using Zhaizj.Framework.Web;
|
||||
using Zhaizj.Framework.Web.Mvc;
|
||||
|
||||
namespace Zhaizj.Framework.Common.Onlines {
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 提供在线状态的各类数据
|
||||
/// </summary>
|
||||
public class OnlineService {
|
||||
|
||||
public static List<OnlineUser> GetAll() {
|
||||
|
||||
List<OnlineUser> all = cdb.findAll<OnlineUser>();
|
||||
all.Sort();
|
||||
return all;
|
||||
}
|
||||
|
||||
public static List<OnlineUser> GetGuest() {
|
||||
|
||||
List<OnlineUser> all = cdb.findAll<OnlineUser>();
|
||||
|
||||
List<OnlineUser> allUsers = new List<OnlineUser>();
|
||||
foreach (OnlineUser info in all) {
|
||||
if (info.UserId <= 0) allUsers.Add( info );
|
||||
}
|
||||
|
||||
allUsers.Sort();
|
||||
|
||||
return allUsers;
|
||||
}
|
||||
|
||||
public static List<OnlineUser> GetLoggerUser() {
|
||||
|
||||
List<OnlineUser> all = cdb.findAll<OnlineUser>();
|
||||
|
||||
List<OnlineUser> allUsers = new List<OnlineUser>();
|
||||
foreach (OnlineUser info in all) {
|
||||
if (info.UserId > 0) allUsers.Add( info );
|
||||
}
|
||||
|
||||
allUsers.Sort();
|
||||
|
||||
return allUsers;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 最新登录用户
|
||||
/// </summary>
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
public static List<OnlineUser> GetRecent( int count ) {
|
||||
|
||||
List<OnlineUser> all = cdb.findAll<OnlineUser>();
|
||||
|
||||
int icount = 1;
|
||||
List<OnlineUser> results = new List<OnlineUser>();
|
||||
foreach (OnlineUser info in all) {
|
||||
if (icount > count) break;
|
||||
if (info.UserId > 0) {
|
||||
results.Add( info );
|
||||
icount++;
|
||||
}
|
||||
}
|
||||
|
||||
results.Sort();
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 最新所有用户
|
||||
/// </summary>
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
public static List<OnlineUser> GetRecentAll( int count ) {
|
||||
|
||||
List<OnlineUser> all = cdb.findAll<OnlineUser>();
|
||||
|
||||
int icount = 1;
|
||||
List<OnlineUser> results = new List<OnlineUser>();
|
||||
foreach (OnlineUser info in all) {
|
||||
if (icount > count) break;
|
||||
|
||||
results.Add( info );
|
||||
icount++;
|
||||
}
|
||||
|
||||
results.Sort();
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Zhaizj.Framework.Common.Onlines {
|
||||
|
||||
/// <summary>
|
||||
/// 在线状态数据
|
||||
/// </summary>
|
||||
public class OnlineStats {
|
||||
|
||||
public static readonly OnlineStats Instance = new OnlineStats();
|
||||
private object objLock = new object();
|
||||
|
||||
private OnlineStats() { }
|
||||
|
||||
public int Count {
|
||||
get { return cdb.findAll<OnlineUser>().Count; }
|
||||
}
|
||||
|
||||
public int GuestCount {
|
||||
get { return this.Count - this.MemberCount; }
|
||||
}
|
||||
|
||||
private int _memberCount;
|
||||
|
||||
public int MemberCount {
|
||||
get { return _memberCount; }
|
||||
}
|
||||
|
||||
// 1) 新用户登录的时候(+1)
|
||||
// 2) 用户(+1)->游客(注销会-1)->用户(登录会+1)
|
||||
public void AddMemberCount() {
|
||||
|
||||
lock (objLock) {
|
||||
_memberCount = _memberCount + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 1) 注销的时候-1
|
||||
// 2) 用其他客户端,同一账号登录-1
|
||||
// 3) 超时,由系统自动清除-1
|
||||
public void SubtractMemberCount() {
|
||||
|
||||
lock (objLock) {
|
||||
_memberCount = _memberCount - 1;
|
||||
if (_memberCount < 0) _memberCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void ReCount() {
|
||||
|
||||
lock (objLock) {
|
||||
|
||||
_memberCount = OnlineService.GetLoggerUser().Count;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public int MaxCount {
|
||||
get { return config.Instance.Site.MaxOnline; }
|
||||
}
|
||||
public DateTime MaxTime {
|
||||
get { return config.Instance.Site.MaxOnlineTime; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
using Zhaizj.Framework.Data;
|
||||
using Zhaizj.Framework.ORM;
|
||||
|
||||
namespace Zhaizj.Framework.Common.Onlines {
|
||||
|
||||
/// <summary>
|
||||
/// ÔÚÏßÓû§ÐÅÏ¢·â×°
|
||||
/// </summary>
|
||||
[NotSave]
|
||||
public class OnlineUser : CacheObject, IComparable {
|
||||
|
||||
public int UserId { get; set; }
|
||||
public String UserName { get; set; }
|
||||
public String UserUrl { get; set; }
|
||||
public String UserPicUrl { get; set; }
|
||||
|
||||
public String Role { get; set; }
|
||||
|
||||
public int IsHidden { get; set; }
|
||||
|
||||
public String Location { get; set; }
|
||||
public String Referrer { get; set; }
|
||||
public String Agent { get; set; }
|
||||
|
||||
public DateTime StartTime { get; set; }
|
||||
public DateTime LastActive { get; set; }
|
||||
|
||||
public String Target { get; set; }
|
||||
|
||||
public String Ip { get; set; }
|
||||
public String TrueIp { get; set; }
|
||||
|
||||
|
||||
public int CompareTo( Object obj ) {
|
||||
OnlineUser target = obj as OnlineUser;
|
||||
if (target.LastActive > this.LastActive) return 1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user