feat: 优化在线用户
This commit is contained in:
@@ -24,7 +24,7 @@ namespace Yi.Framework.Rbac.Domain.Shared.Model
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用户id
|
/// 用户id
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public long? UserId { get; set; }
|
public Guid? UserId { get; set; }
|
||||||
public string? UserName { get; set; }
|
public string? UserName { get; set; }
|
||||||
public DateTime LoginTime { get; set; }
|
public DateTime LoginTime { get; set; }
|
||||||
public string? Ipaddr { get; set; }
|
public string? Ipaddr { get; set; }
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace Yi.Framework.Rbac.Domain.SignalRHubs
|
|||||||
public class OnlineUserHub : AbpHub
|
public class OnlineUserHub : AbpHub
|
||||||
{
|
{
|
||||||
public static readonly List<OnlineUserModel> clientUsers = new();
|
public static readonly List<OnlineUserModel> clientUsers = new();
|
||||||
|
private readonly static object objLock = new object();
|
||||||
|
|
||||||
private HttpContext? _httpContext;
|
private HttpContext? _httpContext;
|
||||||
private ILogger<OnlineUserHub> _logger => LoggerFactory.CreateLogger<OnlineUserHub>();
|
private ILogger<OnlineUserHub> _logger => LoggerFactory.CreateLogger<OnlineUserHub>();
|
||||||
@@ -30,33 +30,40 @@ namespace Yi.Framework.Rbac.Domain.SignalRHubs
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override Task OnConnectedAsync()
|
public override Task OnConnectedAsync()
|
||||||
{
|
{
|
||||||
var name = CurrentUser.UserName;
|
lock (objLock)
|
||||||
var loginUser = new LoginLogEntity().GetInfoByHttpContext(_httpContext);
|
|
||||||
var user = clientUsers.Any(u => u is not null && u.ConnnectionId == Context.ConnectionId);
|
|
||||||
//判断用户是否存在,否则添加集合
|
|
||||||
if (!user)
|
|
||||||
{
|
{
|
||||||
OnlineUserModel users = new(Context.ConnectionId)
|
|
||||||
|
var name = CurrentUser.UserName;
|
||||||
|
var loginUser = new LoginLogEntity().GetInfoByHttpContext(_httpContext);
|
||||||
|
var user = clientUsers.Any(u => u is not null && u.ConnnectionId == Context.ConnectionId);
|
||||||
|
//判断用户是否存在,否则添加集合
|
||||||
|
if (!user)
|
||||||
{
|
{
|
||||||
Browser = loginUser?.Browser,
|
OnlineUserModel users = new(Context.ConnectionId)
|
||||||
LoginLocation = loginUser?.LoginLocation,
|
{
|
||||||
Ipaddr = loginUser?.LoginIp,
|
Browser = loginUser?.Browser,
|
||||||
LoginTime = DateTime.Now,
|
LoginLocation = loginUser?.LoginLocation,
|
||||||
Os = loginUser?.Os,
|
Ipaddr = loginUser?.LoginIp,
|
||||||
UserName = name ?? "Null"
|
LoginTime = DateTime.Now,
|
||||||
};
|
Os = loginUser?.Os,
|
||||||
clientUsers.Add(users);
|
UserName = name ?? "Null",
|
||||||
_logger.LogInformation($"{DateTime.Now}:{name},{Context.ConnectionId}连接服务端success,当前已连接{clientUsers.Count}个");
|
UserId = CurrentUser.Id ?? Guid.Empty
|
||||||
|
};
|
||||||
|
|
||||||
//Clients.All.SendAsync(HubsConstant.MoreNotice, SendNotice());
|
|
||||||
//当有人加入,向全部客户端发送当前总数
|
//先移除之前的用户id,一个用户只能一个
|
||||||
Clients.All.SendAsync("onlineNum", clientUsers.Count);
|
clientUsers.RemoveAll(u => u.UserId == CurrentUser.Id);
|
||||||
|
clientUsers.Add(users);
|
||||||
|
_logger.LogInformation($"{DateTime.Now}:{name},{Context.ConnectionId}连接服务端success,当前已连接{clientUsers.Count}个");
|
||||||
|
|
||||||
|
//当有人加入,向全部客户端发送当前总数
|
||||||
|
Clients.All.SendAsync("onlineNum", clientUsers.Count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers);
|
|
||||||
return base.OnConnectedAsync();
|
return base.OnConnectedAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 断开连接
|
/// 断开连接
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -64,19 +71,17 @@ namespace Yi.Framework.Rbac.Domain.SignalRHubs
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override Task OnDisconnectedAsync(Exception exception)
|
public override Task OnDisconnectedAsync(Exception exception)
|
||||||
{
|
{
|
||||||
var user = clientUsers.Where(p => p.ConnnectionId == Context.ConnectionId).FirstOrDefault();
|
lock (objLock)
|
||||||
//判断用户是否存在,否则添加集合
|
|
||||||
if (user != null)
|
|
||||||
{
|
{
|
||||||
var clientUser = clientUsers.FirstOrDefault(x => x.ConnnectionId == user.ConnnectionId);
|
var user = clientUsers.Where(p => p.ConnnectionId == Context.ConnectionId).FirstOrDefault();
|
||||||
if (clientUser is not null)
|
//判断用户是否存在,否则添加集合
|
||||||
|
if (user != null)
|
||||||
{
|
{
|
||||||
clientUsers.Remove(clientUser);
|
clientUsers.RemoveAll(u => u.UserId == CurrentUser.Id || u.ConnnectionId == u.ConnnectionId);
|
||||||
Clients.All.SendAsync("onlineNum", clientUsers.Count);
|
Clients.All.SendAsync("onlineNum", clientUsers.Count);
|
||||||
//Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers);
|
|
||||||
_logger.LogInformation($"用户{user?.UserName}离开了,当前已连接{clientUsers.Count}个");
|
_logger.LogInformation($"用户{user?.UserName}离开了,当前已连接{clientUsers.Count}个");
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return base.OnDisconnectedAsync(exception);
|
return base.OnDisconnectedAsync(exception);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user