实时在线用户功能
This commit is contained in:
@@ -51,8 +51,8 @@ namespace Yi.Framework.WebCore.AttributeExtend
|
||||
|
||||
//根据ip获取地址
|
||||
|
||||
//var ipTool = IpTool.Search(ip);
|
||||
//string location = ipTool.Province + " " + ipTool.City;
|
||||
var ipTool = IpTool.Search(ip);
|
||||
string location = ipTool.Province + " " + ipTool.City;
|
||||
|
||||
//日志服务插入一条操作记录即可
|
||||
|
||||
@@ -66,6 +66,7 @@ namespace Yi.Framework.WebCore.AttributeExtend
|
||||
logEntity.Method = context.HttpContext.Request.Path.Value;
|
||||
logEntity.IsDeleted = false;
|
||||
logEntity.OperUser= context.HttpContext.GetUserNameInfo();
|
||||
logEntity.OperLocation = location;
|
||||
if (logAttribute.IsSaveResponseData)
|
||||
{
|
||||
if (context.Result is ContentResult result && result.ContentType == "application/json")
|
||||
|
||||
@@ -12,6 +12,7 @@ using System.IdentityModel.Tokens.Jwt;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using UAParser;
|
||||
using IPTools.Core;
|
||||
|
||||
namespace Yi.Framework.WebCore
|
||||
{
|
||||
@@ -86,7 +87,7 @@ namespace Yi.Framework.WebCore
|
||||
try
|
||||
{
|
||||
claimlist = httpContext.User.Claims;
|
||||
resId = Convert.ToInt64(claimlist.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Sid).Value);
|
||||
resId = Convert.ToInt64(claimlist.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Sid)?.Value);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -224,8 +225,7 @@ namespace Yi.Framework.WebCore
|
||||
public static LoginLogEntity GetLoginLogInfo(this HttpContext context)
|
||||
{
|
||||
var ipAddr = context.GetClientIp();
|
||||
//var ip_info = IpTool.Search(ipAddr);
|
||||
//var location = "广州" + "-" + "深圳";
|
||||
var location = IpTool.Search(ipAddr);
|
||||
ClientInfo clientInfo = context.GetClientInfo();
|
||||
LoginLogEntity entity = new()
|
||||
{
|
||||
@@ -234,7 +234,7 @@ namespace Yi.Framework.WebCore
|
||||
LoginIp = ipAddr,
|
||||
//登录是没有token的,所有是获取不到用户名,需要在控制器赋值
|
||||
//LoginUser = context.GetUserNameInfo(),
|
||||
LoginLocation = "广州" + "-" + "深圳",
|
||||
LoginLocation = location.Province + "-" + location.City,
|
||||
IsDeleted = false
|
||||
};
|
||||
|
||||
|
||||
@@ -95,16 +95,21 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
};
|
||||
db.Aop.OnLogExecuting = (s, p) =>
|
||||
{
|
||||
var _logger = ServiceLocator.Instance.GetService<ILogger<SqlSugarClient>>();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("执行SQL:" + s.ToString());
|
||||
foreach (var i in p)
|
||||
//暂时先关闭sql打印
|
||||
if (false)
|
||||
{
|
||||
sb.Append($"\r\n参数:{i.ParameterName},参数值:{i.Value}");
|
||||
}
|
||||
var _logger = ServiceLocator.Instance.GetService<ILogger<SqlSugarClient>>();
|
||||
|
||||
_logger.LogInformation(sb.ToString());
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("执行SQL:" + s.ToString());
|
||||
foreach (var i in p)
|
||||
{
|
||||
sb.Append($"\r\n参数:{i.ParameterName},参数值:{i.Value}");
|
||||
}
|
||||
|
||||
_logger.LogInformation(sb.ToString());
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -1,21 +1,74 @@
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using IPTools.Core;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Enum;
|
||||
|
||||
namespace Yi.Framework.WebCore.SignalRHub
|
||||
{
|
||||
public class MainHub : Hub
|
||||
{
|
||||
private HttpContext _httpContext;
|
||||
private ILogger<MainHub> _logger;
|
||||
public MainHub(IHttpContextAccessor httpContextAccessor,ILogger<MainHub> logger)
|
||||
{
|
||||
_httpContext = httpContextAccessor.HttpContext;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private static readonly List<OnlineUser> clientUsers = new();
|
||||
|
||||
/// <summary>
|
||||
/// 成功连接
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override Task OnConnectedAsync()
|
||||
{
|
||||
var name = _httpContext.GetUserNameInfo();
|
||||
var ip = _httpContext.GetClientIp();
|
||||
var ip_info = IpTool.Search(ip);
|
||||
|
||||
var loginUser = _httpContext.GetUserEntityInfo(out _);
|
||||
var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId);
|
||||
//判断用户是否存在,否则添加集合
|
||||
if (!user && Context.User.Identity.IsAuthenticated)
|
||||
{
|
||||
OnlineUser users = new(Context.ConnectionId, name, loginUser.Id, ip)
|
||||
{
|
||||
Location = ip_info.City
|
||||
};
|
||||
clientUsers.Add(users);
|
||||
_logger.LogInformation($"{DateTime.Now}:{name},{Context.ConnectionId}连接服务端success,当前已连接{clientUsers.Count}个");
|
||||
|
||||
//Clients.All.SendAsync(HubsConstant.MoreNotice, SendNotice());
|
||||
}
|
||||
//当有人加入,向全部客户端发送当前总数
|
||||
Clients.All.SendAsync(HubTypeEnum.onlineNum.ToString(), clientUsers.Count);
|
||||
//Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers);
|
||||
return base.OnConnectedAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 断开连接
|
||||
/// </summary>
|
||||
/// <param name="exception"></param>
|
||||
/// <returns></returns>
|
||||
public override Task OnDisconnectedAsync(Exception exception)
|
||||
{
|
||||
var user = clientUsers.Where(p => p.ConnnectionId == Context.ConnectionId).FirstOrDefault();
|
||||
//判断用户是否存在,否则添加集合
|
||||
if (user != null)
|
||||
{
|
||||
clientUsers.Remove(user);
|
||||
Clients.All.SendAsync(HubTypeEnum.onlineNum.ToString(), clientUsers.Count);
|
||||
//Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers);
|
||||
_logger.LogInformation($"用户{user?.Name}离开了,当前已连接{clientUsers.Count}个");
|
||||
}
|
||||
return base.OnDisconnectedAsync(exception);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Yi.Framework.WebCore.SignalRHub
|
||||
{
|
||||
public class OnlineUser
|
||||
{
|
||||
/// <summary>
|
||||
/// 客户端连接Id
|
||||
/// </summary>
|
||||
public string ConnnectionId { get; set; }
|
||||
/// <summary>
|
||||
/// 用户id
|
||||
/// </summary>
|
||||
public long? Userid { get; set; }
|
||||
public string Name { get; set; }
|
||||
public DateTime LoginTime { get; set; }
|
||||
public string UserIP { get; set; }
|
||||
public string Location { get; set; }
|
||||
|
||||
public OnlineUser(string clientid, string name, long? userid, string userip)
|
||||
{
|
||||
ConnnectionId = clientid;
|
||||
Name = name;
|
||||
LoginTime = DateTime.Now;
|
||||
Userid = userid;
|
||||
UserIP = userip;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user