using Furion.InstantMessaging;
using Masuit.Tools;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.Connections;
namespace SafeCampus.Plugin.SignalR
{
///
///
///
[Authorize]
[MapHub("/hubs/simple")]
public class SimpleHub : Hub
{
private readonly ISimpleCacheService _simpleCacheService;
public SimpleHub(ISimpleCacheService simpleCacheService)
{
_simpleCacheService = simpleCacheService;
}
///
/// 连接
///
///
public override async Task OnConnectedAsync()
{
var token = Context.GetHttpContext().Request.Query["access_token"];//获取token
if (!string.IsNullOrEmpty(token))
{
var userIdentifier = Context.UserIdentifier;//自定义的Id
UpdateRedis(userIdentifier, token);//更新redis
}
}
///
/// 断开连接
///
///
///
public override async Task OnDisconnectedAsync(Exception? exception)
{
var userIdentifier = Context.UserIdentifier;//自定义的Id
UpdateRedis(userIdentifier, null, false);//更新redis
await base.OnDisconnectedAsync(exception);
}
///
/// 退出登录
///
///
///
public async Task LoginOut(string userId)
{
await Clients.User(userId).LoginOut("退出登录");
}
#region 方法
///
/// 更新redis
///
/// 用户id
/// token
/// 是否是上线
private void UpdateRedis(string userIdentifier, string token, bool ifConnect = true)
{
var userId = userIdentifier.Split("_")[0];//分割取第一个
if (!string.IsNullOrEmpty(userId))
{
//获取redis当前用户的token信息列表
var tokenInfos = _simpleCacheService.HashGetOne>(CacheConst.Cache_UserToken, userId);
if (tokenInfos != null)
{
if (ifConnect)
{
//获取redis中当前token
var tokenInfo = tokenInfos.Where(it => it.Token == token).FirstOrDefault();
if (tokenInfo != null)
{
tokenInfo.ClientIds.Add(userIdentifier);//添加到客户端列表
_simpleCacheService.HashAdd(CacheConst.Cache_UserToken, userId, tokenInfos);//更新Redis
}
}
else
{
//获取当前客户端ID所在的token信息
var tokenInfo = tokenInfos.Where(it => it.ClientIds.Contains(userIdentifier)).FirstOrDefault();
if (tokenInfo != null)
{
tokenInfo.ClientIds.RemoveWhere(it => it == userIdentifier);//从客户端列表删除
_simpleCacheService.HashAdd(CacheConst.Cache_UserToken, userId, tokenInfos);//更新Redis
}
}
}
}
}
#endregion 方法
}
}