// 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
using Furion.InstantMessaging;
using Microsoft.AspNetCore.SignalR;
namespace Admin.NET.Core;
///
/// 在线用户集线器
///
[MapHub("/hubs/onlineUser")]
public class OnlineUserHub : Hub
{
private const string GROUP_ONLINE = "GROUP_ONLINE_"; // 租户分组前缀
private readonly SqlSugarRepository _sysOnlineUerRep;
private readonly SysMessageService _sysMessageService;
private readonly IHubContext _onlineUserHubContext;
private readonly SysCacheService _sysCacheService;
public OnlineUserHub(SqlSugarRepository sysOnlineUerRep,
SysMessageService sysMessageService,
IHubContext onlineUserHubContext,
SysCacheService sysCacheService)
{
_sysOnlineUerRep = sysOnlineUerRep;
_sysMessageService = sysMessageService;
_onlineUserHubContext = onlineUserHubContext;
_sysCacheService = sysCacheService;
}
///
/// 连接
///
///
public override async Task OnConnectedAsync()
{
var httpContext = Context.GetHttpContext();
var token = httpContext.Request.Query["access_token"];
var claims = JWTEncryption.ReadJwtToken(token)?.Claims;
var client = Parser.GetDefault().Parse(httpContext.Request.Headers["User-Agent"]);
var userId = claims.FirstOrDefault(u => u.Type == ClaimConst.UserId)?.Value;
var tenantId = claims.FirstOrDefault(u => u.Type == ClaimConst.TenantId)?.Value;
var user = new SysOnlineUser
{
ConnectionId = Context.ConnectionId,
UserId = string.IsNullOrWhiteSpace(userId) ? 0 : long.Parse(userId),
UserName = claims.FirstOrDefault(u => u.Type == ClaimConst.Account)?.Value,
RealName = claims.FirstOrDefault(u => u.Type == ClaimConst.RealName)?.Value,
Time = DateTime.Now,
//Ip = App.HttpContext.GetRemoteIpAddressToIPv4(),
Ip = httpContext.Connection.RemoteIpAddress.ToString(),
Browser = client.UA.Family + client.UA.Major,
Os = client.OS.Family + client.OS.Major,
TenantId = string.IsNullOrWhiteSpace(tenantId) ? 0 : Convert.ToInt64(tenantId),
};
await _sysOnlineUerRep.InsertAsync(user);
_sysCacheService.Set(CacheConst.KeyUserOnline + user.UserId, user);
// 以租户Id进行分组
var groupName = $"{GROUP_ONLINE}{user.TenantId}";
await _onlineUserHubContext.Groups.AddToGroupAsync(Context.ConnectionId, groupName);
var userList = await _sysOnlineUerRep.AsQueryable().Filter("", true)
.Where(u => u.TenantId == user.TenantId).Take(10).ToListAsync();
await _onlineUserHubContext.Clients.Groups(groupName).OnlineUserList(new OnlineUserList
{
RealName = user.RealName,
Online = true,
UserList = userList
});
}
///
/// 断开
///
///
///
public override async Task OnDisconnectedAsync(Exception exception)
{
if (string.IsNullOrEmpty(Context.ConnectionId)) return;
var user = await _sysOnlineUerRep.AsQueryable().Filter("", true).FirstAsync(u => u.ConnectionId == Context.ConnectionId);
if (user == null) return;
await _sysOnlineUerRep.DeleteAsync(u => u.Id == user.Id);
_sysCacheService.Remove(CacheConst.KeyUserOnline + user.UserId);
// 通知当前组用户变动
var userList = await _sysOnlineUerRep.AsQueryable().Filter("", true)
.Where(u => u.TenantId == user.TenantId).Take(10).ToListAsync();
await _onlineUserHubContext.Clients.Groups($"{GROUP_ONLINE}{user.TenantId}").OnlineUserList(new OnlineUserList
{
RealName = user.RealName,
Online = false,
UserList = userList
});
}
///
/// 强制下线
///
///
///
public async Task ForceOffline(OnlineUserHubInput input)
{
await _onlineUserHubContext.Clients.Client(input.ConnectionId).ForceOffline("强制下线");
}
///
/// 发送信息给某个人
///
///
///
public async Task ClientsSendMessage(MessageInput message)
{
await _sysMessageService.SendUser(message);
}
///
/// 发送信息给所有人
///
///
///
public async Task ClientsSendMessagetoAll(MessageInput message)
{
await _sysMessageService.SendAllUser(message);
}
///
/// 发送消息给某些人(除了本人)
///
///
///
public async Task ClientsSendMessagetoOther(MessageInput message)
{
await _sysMessageService.SendOtherUser(message);
}
///
/// 发送消息给某些人
///
///
///
public async Task ClientsSendMessagetoUsers(MessageInput message)
{
await _sysMessageService.SendUsers(message);
}
}