2023-12-11 09:55:12 +08:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-04-18 21:29:44 +08:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Volo.Abp.AspNetCore.SignalR;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.Entities;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.SignalRHubs.Model;
|
2023-04-18 21:29:44 +08:00
|
|
|
|
|
2023-12-11 09:55:12 +08:00
|
|
|
|
namespace Yi.Framework.Rbac.Domain.SignalRHubs
|
2023-04-18 21:29:44 +08:00
|
|
|
|
{
|
2023-12-11 09:55:12 +08:00
|
|
|
|
[HubRoute("/hub/main")]
|
|
|
|
|
|
[Authorize]
|
2023-12-19 16:52:50 +08:00
|
|
|
|
public class OnlineUserHub : AbpHub
|
2023-04-18 21:29:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
public static readonly List<OnlineUserModel> clientUsers = new();
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-12-11 09:55:12 +08:00
|
|
|
|
private HttpContext? _httpContext;
|
2023-12-19 16:52:50 +08:00
|
|
|
|
private ILogger<OnlineUserHub> _logger => LoggerFactory.CreateLogger<OnlineUserHub>();
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public OnlineUserHub(IHttpContextAccessor httpContextAccessor)
|
2023-04-18 21:29:44 +08:00
|
|
|
|
{
|
2023-12-11 09:55:12 +08:00
|
|
|
|
_httpContext = httpContextAccessor?.HttpContext;
|
2023-04-18 21:29:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 成功连接
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public override Task OnConnectedAsync()
|
|
|
|
|
|
{
|
2023-12-11 09:55:12 +08:00
|
|
|
|
var name = CurrentUser.UserName;
|
|
|
|
|
|
var loginUser = new LoginLogEntity().GetInfoByHttpContext(_httpContext);
|
|
|
|
|
|
var user = clientUsers.Any(u => u is not null && u.ConnnectionId == Context.ConnectionId);
|
2023-04-18 21:29:44 +08:00
|
|
|
|
//判断用户是否存在,否则添加集合
|
2023-04-19 13:39:55 +08:00
|
|
|
|
if (!user)
|
2023-04-18 21:29:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
OnlineUserModel users = new(Context.ConnectionId)
|
|
|
|
|
|
{
|
|
|
|
|
|
Browser = loginUser?.Browser,
|
|
|
|
|
|
LoginLocation = loginUser?.LoginLocation,
|
|
|
|
|
|
Ipaddr = loginUser?.LoginIp,
|
|
|
|
|
|
LoginTime = DateTime.Now,
|
|
|
|
|
|
Os = loginUser?.Os,
|
2023-12-11 09:55:12 +08:00
|
|
|
|
UserName = name ?? "Null"
|
2023-04-18 21:29:44 +08:00
|
|
|
|
};
|
|
|
|
|
|
clientUsers.Add(users);
|
|
|
|
|
|
_logger.LogInformation($"{DateTime.Now}:{name},{Context.ConnectionId}连接服务端success,当前已连接{clientUsers.Count}个");
|
|
|
|
|
|
|
|
|
|
|
|
//Clients.All.SendAsync(HubsConstant.MoreNotice, SendNotice());
|
2023-04-19 13:39:55 +08:00
|
|
|
|
//当有人加入,向全部客户端发送当前总数
|
|
|
|
|
|
Clients.All.SendAsync("onlineNum", clientUsers.Count);
|
2023-04-18 21:29:44 +08:00
|
|
|
|
}
|
2023-04-19 19:40:47 +08:00
|
|
|
|
|
2023-04-18 21:29:44 +08:00
|
|
|
|
//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)
|
|
|
|
|
|
{
|
2023-12-19 16:52:50 +08:00
|
|
|
|
var clientUser = clientUsers.FirstOrDefault(x => x.ConnnectionId == user.ConnnectionId);
|
|
|
|
|
|
if (clientUser is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
clientUsers.Remove(clientUser);
|
|
|
|
|
|
Clients.All.SendAsync("onlineNum", clientUsers.Count);
|
|
|
|
|
|
//Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers);
|
|
|
|
|
|
_logger.LogInformation($"用户{user?.UserName}离开了,当前已连接{clientUsers.Count}个");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-18 21:29:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
return base.OnDisconnectedAsync(exception);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task SendAllTest(string test)
|
|
|
|
|
|
{
|
|
|
|
|
|
await Clients.All.SendAsync("ReceiveAllInfo", test);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|