You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.3 KiB

using Furion.DataEncryption;
using GDZZ.Core.Entity;
using GDZZ.Core.Service;
using Microsoft.AspNetCore.SignalR;
using Furion.InstantMessaging;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace GDZZ.Core;
/// <summary>
/// 聊天集线器
/// </summary>
//[MapHub("/hubs/chathub")]
public class ChatHub : Hub<IChatClient>
{
private readonly ISysCacheService _cache;
private readonly SqlSugarRepository<OnlineUser> _sysOnlineUerRep; // 在线用户表仓储
public ChatHub(ISysCacheService cache, SqlSugarRepository<OnlineUser> sysOnlineUerRep)
{
_sysOnlineUerRep = sysOnlineUerRep;
_cache = cache;
}
//RuntimeMethodHandle-rf
/// <summary>
/// 连接
/// </summary>
/// <returns></returns>
public override async Task OnConnectedAsync()
{
Console.WriteLine("上线连接");
var token = Context.GetHttpContext().Request.Query["access_token"];
var claims = JWTEncryption.ReadJwtToken(token)?.Claims;
var userId = claims.FirstOrDefault(e => e.Type == ClaimConst.CLAINM_USERID)?.Value;
var account= claims.FirstOrDefault(e => e.Type == ClaimConst.CLAINM_ACCOUNT)?.Value;
var name= claims.FirstOrDefault(e => e.Type == ClaimConst.CLAINM_NAME)?.Value;
var tenantId= claims.FirstOrDefault(e => e.Type == ClaimConst.TENANT_ID)?.Value;
var ip = HttpNewUtil.Ip;
if (await _sysOnlineUerRep.AsQueryable().Filter("TenantId", true).AnyAsync(m => m.Account.Equals(account))) {
await _sysOnlineUerRep.DeleteAsync(m => m.Account == account && m.LastLoginIp == ip);
}
OnlineUser user = new OnlineUser()
{
ConnectionId = Context.ConnectionId,
UserId = long.Parse(userId),
LastTime = DateTime.Now,
LastLoginIp = ip,
Account = account,
Name = name,
TenantId = Convert.ToInt64(tenantId)
};
await _sysOnlineUerRep.InsertAsync(user);
}
/// <summary>
/// 断开
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
public override async Task OnDisconnectedAsync(Exception exception)
{
if (!string.IsNullOrEmpty(Context.ConnectionId))
{
await _sysOnlineUerRep.DeleteAsync(m => m.ConnectionId == Context.ConnectionId);
}
}
2 years ago
}