平安校园
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

4 месяцев назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Furion.InstantMessaging;
  2. using Masuit.Tools;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.AspNetCore.Http.Connections;
  5. namespace SafeCampus.Plugin.SignalR
  6. {
  7. /// <summary>
  8. /// <inheritdoc cref="ISimpleHub"/>
  9. /// </summary>
  10. [Authorize]
  11. [MapHub("/hubs/simple")]
  12. public class SimpleHub : Hub<ISimpleHub>
  13. {
  14. private readonly ISimpleCacheService _simpleCacheService;
  15. public SimpleHub(ISimpleCacheService simpleCacheService)
  16. {
  17. _simpleCacheService = simpleCacheService;
  18. }
  19. /// <summary>
  20. /// 连接
  21. /// </summary>
  22. /// <returns></returns>
  23. public override async Task OnConnectedAsync()
  24. {
  25. var token = Context.GetHttpContext().Request.Query["access_token"];//获取token
  26. if (!string.IsNullOrEmpty(token))
  27. {
  28. var userIdentifier = Context.UserIdentifier;//自定义的Id
  29. UpdateRedis(userIdentifier, token);//更新redis
  30. }
  31. }
  32. /// <summary>
  33. /// 断开连接
  34. /// </summary>
  35. /// <param name="exception"></param>
  36. /// <returns></returns>
  37. public override async Task OnDisconnectedAsync(Exception? exception)
  38. {
  39. var userIdentifier = Context.UserIdentifier;//自定义的Id
  40. UpdateRedis(userIdentifier, null, false);//更新redis
  41. await base.OnDisconnectedAsync(exception);
  42. }
  43. /// <summary>
  44. /// 退出登录
  45. /// </summary>
  46. /// <param name="userId"></param>
  47. /// <returns></returns>
  48. public async Task LoginOut(string userId)
  49. {
  50. await Clients.User(userId).LoginOut("退出登录");
  51. }
  52. #region 方法
  53. /// <summary>
  54. /// 更新redis
  55. /// </summary>
  56. /// <param name="userIdentifier">用户id</param>
  57. /// <param name="token">token</param>
  58. /// <param name="ifConnect">是否是上线</param>
  59. private void UpdateRedis(string userIdentifier, string token, bool ifConnect = true)
  60. {
  61. var userId = userIdentifier.Split("_")[0];//分割取第一个
  62. if (!string.IsNullOrEmpty(userId))
  63. {
  64. //获取redis当前用户的token信息列表
  65. var tokenInfos = _simpleCacheService.HashGetOne<List<TokenInfo>>(CacheConst.Cache_UserToken, userId);
  66. if (tokenInfos != null)
  67. {
  68. if (ifConnect)
  69. {
  70. //获取redis中当前token
  71. var tokenInfo = tokenInfos.Where(it => it.Token == token).FirstOrDefault();
  72. if (tokenInfo != null)
  73. {
  74. tokenInfo.ClientIds.Add(userIdentifier);//添加到客户端列表
  75. _simpleCacheService.HashAdd(CacheConst.Cache_UserToken, userId, tokenInfos);//更新Redis
  76. }
  77. }
  78. else
  79. {
  80. //获取当前客户端ID所在的token信息
  81. var tokenInfo = tokenInfos.Where(it => it.ClientIds.Contains(userIdentifier)).FirstOrDefault();
  82. if (tokenInfo != null)
  83. {
  84. tokenInfo.ClientIds.RemoveWhere(it => it == userIdentifier);//从客户端列表删除
  85. _simpleCacheService.HashAdd(CacheConst.Cache_UserToken, userId, tokenInfos);//更新Redis
  86. }
  87. }
  88. }
  89. }
  90. }
  91. #endregion 方法
  92. }
  93. }