2024-11-29 14:58:10 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
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;
|
2024-01-04 20:54:01 +08:00
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Model;
|
2023-04-18 21:29:44 +08:00
|
|
|
|
|
2024-01-22 09:27:58 +08:00
|
|
|
|
namespace Yi.Framework.Rbac.Application.SignalRHubs
|
2023-04-18 21:29:44 +08:00
|
|
|
|
{
|
2023-12-11 09:55:12 +08:00
|
|
|
|
[HubRoute("/hub/main")]
|
2024-03-11 17:07:43 +08:00
|
|
|
|
//开放不需要授权
|
|
|
|
|
|
//[Authorize]
|
2024-02-20 18:32:48 +08:00
|
|
|
|
public class OnlineHub : AbpHub
|
2023-04-18 21:29:44 +08:00
|
|
|
|
{
|
2024-11-29 14:58:10 +08:00
|
|
|
|
public static ConcurrentDictionary<string, OnlineUserModel> ClientUsersDic { get; set; } = new();
|
2023-04-18 21:29:44 +08:00
|
|
|
|
|
2024-11-29 14:58:10 +08:00
|
|
|
|
private readonly HttpContext? _httpContext;
|
2024-02-20 18:32:48 +08:00
|
|
|
|
private ILogger<OnlineHub> _logger => LoggerFactory.CreateLogger<OnlineHub>();
|
2024-11-29 14:58:10 +08:00
|
|
|
|
|
2024-02-20 18:32:48 +08:00
|
|
|
|
public OnlineHub(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()
|
|
|
|
|
|
{
|
2024-11-29 14:58:10 +08:00
|
|
|
|
if (_httpContext is null)
|
2023-04-18 21:29:44 +08:00
|
|
|
|
{
|
2024-11-29 14:58:10 +08:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
var name = CurrentUser.UserName;
|
|
|
|
|
|
var loginUser = new LoginLogAggregateRoot().GetInfoByHttpContext(_httpContext);
|
2023-04-19 19:40:47 +08:00
|
|
|
|
|
2024-11-29 14:58:10 +08:00
|
|
|
|
OnlineUserModel user = 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
|
|
|
|
|
|
};
|
2024-03-11 17:07:43 +08:00
|
|
|
|
|
2024-11-29 14:58:10 +08:00
|
|
|
|
//已登录
|
|
|
|
|
|
if (CurrentUser.IsAuthenticated)
|
|
|
|
|
|
{
|
|
|
|
|
|
ClientUsersDic.RemoveAll(u => u.Value.UserId == CurrentUser.Id);
|
|
|
|
|
|
_logger.LogDebug(
|
|
|
|
|
|
$"{DateTime.Now}:{name},{Context.ConnectionId}连接服务端success,当前已连接{ClientUsersDic.Count}个");
|
2024-01-16 11:14:07 +08:00
|
|
|
|
}
|
2024-11-29 14:58:10 +08:00
|
|
|
|
|
|
|
|
|
|
ClientUsersDic.AddOrUpdate(Context.ConnectionId, user, (_, _) => user);
|
|
|
|
|
|
|
|
|
|
|
|
//当有人加入,向全部客户端发送当前总数
|
|
|
|
|
|
Clients.All.SendAsync("onlineNum", ClientUsersDic.Count);
|
|
|
|
|
|
|
2023-04-18 21:29:44 +08:00
|
|
|
|
return base.OnConnectedAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-16 11:14:07 +08:00
|
|
|
|
|
2023-04-18 21:29:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 断开连接
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="exception"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-11-29 14:58:10 +08:00
|
|
|
|
public override Task OnDisconnectedAsync(Exception? exception)
|
2023-04-18 21:29:44 +08:00
|
|
|
|
{
|
2024-11-29 14:58:10 +08:00
|
|
|
|
//已登录
|
|
|
|
|
|
if (CurrentUser.IsAuthenticated)
|
2023-04-18 21:29:44 +08:00
|
|
|
|
{
|
2024-11-29 14:58:10 +08:00
|
|
|
|
ClientUsersDic.RemoveAll(u => u.Value.UserId == CurrentUser.Id);
|
|
|
|
|
|
_logger.LogDebug($"用户{CurrentUser?.UserName}离开了,当前已连接{ClientUsersDic.Count}个");
|
2023-04-18 21:29:44 +08:00
|
|
|
|
}
|
2024-11-29 14:58:10 +08:00
|
|
|
|
ClientUsersDic.Remove(Context.ConnectionId, out _);
|
|
|
|
|
|
Clients.All.SendAsync("onlineNum", ClientUsersDic.Count);
|
2023-04-18 21:29:44 +08:00
|
|
|
|
return base.OnDisconnectedAsync(exception);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|