ChatHub.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using Microsoft.AspNetCore.SignalR;
  2. using OASystem.API.OAMethodLib.Hub.HubClients;
  3. using OASystem.API.OAMethodLib.SignalR.Hubs;
  4. using OASystem.API.OAMethodLib.SignalR.HubService;
  5. using System.DirectoryServices.Protocols;
  6. using System.Text.RegularExpressions;
  7. using static OASystem.API.OAMethodLib.Hub.Hubs.ChatHub;
  8. using static OASystem.API.OAMethodLib.JWTHelper;
  9. namespace OASystem.API.OAMethodLib.Hub.Hubs
  10. {
  11. public class ChatHub : Hub<IChatClient>
  12. {
  13. private readonly ILogger<ChatHub> _logger;
  14. private readonly IHttpContextAccessor _accessor;
  15. private readonly CommonService _common;
  16. /// <summary>
  17. /// 已登录的用户信息
  18. /// </summary>
  19. public static List<UserModel> OnlineUser { get; set; } = new List<UserModel>();
  20. public ChatHub(ILogger<ChatHub> logger, CommonService common, IHttpContextAccessor accessor)
  21. {
  22. _logger = logger;
  23. _common = common;
  24. _accessor = accessor;
  25. }
  26. /// <summary>
  27. /// SignalR登录验证
  28. /// </summary>
  29. public async Task SignalRLogin(int userId)
  30. {
  31. string connid = Context.ConnectionId;
  32. bool status = false;
  33. if (!OnlineUser.Exists(u => u.ConnectionId == connid))
  34. {
  35. status = true;
  36. OnlineUser.Add( new UserModel() { UserId = userId,ConnectionId = connid,GroupName = "FMGJ-OASystem" });
  37. }
  38. //给当前连接返回消息
  39. await Clients.Client(connid).SendAsync("SignalRLoginResponse", status);
  40. }
  41. /// <summary>
  42. /// 客户端连接服务端
  43. /// </summary>
  44. /// <returns></returns>
  45. public override Task OnConnectedAsync()
  46. {
  47. var id = Context.ConnectionId;
  48. _logger.LogInformation($"Client ConnectionId=> [[{id}]] Already Connection Server!");
  49. //验证Token
  50. //var token = _accessor.HttpContext.Request.Query["access_token"];
  51. //var user = JwtHelper.SerializeJwt(token);
  52. return base.OnConnectedAsync();
  53. }
  54. /// <summary>
  55. /// 客户端断开连接
  56. /// </summary>
  57. /// <param name="exception"></param>
  58. /// <returns></returns>
  59. public override Task OnDisconnectedAsync(Exception exception)
  60. {
  61. var id = Context.ConnectionId;
  62. _logger.LogInformation($"Client ConnectionId=> [[{id}]] Already Close Connection Server!");
  63. return base.OnDisconnectedAsync(exception);
  64. }
  65. /**
  66. * 测试
  67. * */
  68. /// <summary>
  69. /// 给所有客户端发送消息
  70. /// </summary>
  71. /// <returns></returns>
  72. public async Task SendMessage(string data)
  73. {
  74. Console.WriteLine("Have one Data!");
  75. await Clients.All.SendAll(_common.SendAll(data));
  76. }
  77. public class UserModel
  78. {
  79. public int UserId { get; set; }
  80. public string ConnectionId { get; set; }
  81. public string GroupName { get; set; }
  82. }
  83. }
  84. }