feat: 重构signalr,在线人数允许不用登录
This commit is contained in:
@@ -9,7 +9,8 @@ using Yi.Framework.Rbac.Domain.Shared.Model;
|
||||
namespace Yi.Framework.Rbac.Application.SignalRHubs
|
||||
{
|
||||
[HubRoute("/hub/main")]
|
||||
[Authorize]
|
||||
//开放不需要授权
|
||||
//[Authorize]
|
||||
public class OnlineHub : AbpHub
|
||||
{
|
||||
public static readonly List<OnlineUserModel> clientUsers = new();
|
||||
@@ -32,33 +33,32 @@ namespace Yi.Framework.Rbac.Application.SignalRHubs
|
||||
{
|
||||
lock (objLock)
|
||||
{
|
||||
|
||||
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)
|
||||
|
||||
OnlineUserModel user = new(Context.ConnectionId)
|
||||
{
|
||||
OnlineUserModel users = new(Context.ConnectionId)
|
||||
{
|
||||
Browser = loginUser?.Browser,
|
||||
LoginLocation = loginUser?.LoginLocation,
|
||||
Ipaddr = loginUser?.LoginIp,
|
||||
LoginTime = DateTime.Now,
|
||||
Os = loginUser?.Os,
|
||||
UserName = name ?? "Null",
|
||||
UserId = CurrentUser.Id ?? Guid.Empty
|
||||
};
|
||||
Browser = loginUser?.Browser,
|
||||
LoginLocation = loginUser?.LoginLocation,
|
||||
Ipaddr = loginUser?.LoginIp,
|
||||
LoginTime = DateTime.Now,
|
||||
Os = loginUser?.Os,
|
||||
UserName = name ?? "Null",
|
||||
UserId = CurrentUser.Id ?? Guid.Empty
|
||||
};
|
||||
|
||||
|
||||
//先移除之前的用户id,一个用户只能一个
|
||||
//已登录
|
||||
if (CurrentUser.Id is not null)
|
||||
{ //先移除之前的用户id,一个用户只能一个
|
||||
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);
|
||||
}
|
||||
//全部移除之后,再进行添加
|
||||
clientUsers.RemoveAll(u => u.ConnnectionId == Context.ConnectionId);
|
||||
|
||||
clientUsers.Add(user);
|
||||
//当有人加入,向全部客户端发送当前总数
|
||||
Clients.All.SendAsync("onlineNum", clientUsers.Count);
|
||||
}
|
||||
return base.OnConnectedAsync();
|
||||
}
|
||||
@@ -73,23 +73,18 @@ namespace Yi.Framework.Rbac.Application.SignalRHubs
|
||||
{
|
||||
lock (objLock)
|
||||
{
|
||||
var user = clientUsers.Where(p => p.ConnnectionId == Context.ConnectionId).FirstOrDefault();
|
||||
//判断用户是否存在,否则添加集合
|
||||
if (user != null)
|
||||
//已登录
|
||||
if (CurrentUser.Id is not null)
|
||||
{
|
||||
clientUsers.RemoveAll(u => u.UserId == CurrentUser.Id || u.ConnnectionId == u.ConnnectionId);
|
||||
Clients.All.SendAsync("onlineNum", clientUsers.Count);
|
||||
_logger.LogInformation($"用户{user?.UserName}离开了,当前已连接{clientUsers.Count}个");
|
||||
|
||||
clientUsers.RemoveAll(u => u.UserId == CurrentUser.Id);
|
||||
_logger.LogInformation($"用户{CurrentUser?.UserName}离开了,当前已连接{clientUsers.Count}个");
|
||||
}
|
||||
clientUsers.RemoveAll(u => u.ConnnectionId == Context.ConnectionId);
|
||||
Clients.All.SendAsync("onlineNum", clientUsers.Count);
|
||||
}
|
||||
return base.OnDisconnectedAsync(exception);
|
||||
}
|
||||
|
||||
public async Task SendAllTest(string test)
|
||||
{
|
||||
await Clients.All.SendAsync("ReceiveAllInfo", test);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user