ChatHub.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Microsoft.AspNetCore.SignalR;
  2. using OASystem.API.OAMethodLib.Hub;
  3. using OASystem.API.OAMethodLib.Hub.HubClients;
  4. using OASystem.API.OAMethodLib.SignalR.Hubs;
  5. using OASystem.API.OAMethodLib.SignalR.HubService;
  6. using System.DirectoryServices.Protocols;
  7. using System.Text.RegularExpressions;
  8. using static OASystem.API.OAMethodLib.Hub.Hubs.ChatHub;
  9. using static OASystem.API.OAMethodLib.JWTHelper;
  10. namespace OASystem.API.OAMethodLib.Hub.Hubs
  11. {
  12. public class ChatHub : Hub<IChatClient>
  13. {
  14. private readonly ILogger<ChatHub> _logger;
  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)
  21. {
  22. _logger = logger;
  23. _common = common;
  24. }
  25. /// <summary>
  26. /// SignalR登录验证
  27. /// </summary>
  28. public async Task SignalRLogin(int userId)
  29. {
  30. string connid = Context.ConnectionId;
  31. string result = $"[{connid}]";
  32. if (!UserStore.OnlineUser.Exists(u => u.ConnectionId == connid))
  33. {
  34. result += "上线成功!" ;
  35. UserStore.OnlineUser.Add( new UserModel() { UserId = userId,ConnectionId = connid,GroupName = "FMGJ-OASystem" });
  36. }
  37. else
  38. {
  39. result += "已上线!";
  40. }
  41. //给当前连接返回消息
  42. await Clients.Client(connid).SendAsync("SignalRLoginResponse", result);
  43. }
  44. /// <summary>
  45. /// 客户端连接服务端
  46. /// </summary>
  47. /// <returns></returns>
  48. public override Task OnConnectedAsync()
  49. {
  50. var connid = Context.ConnectionId;
  51. _logger.LogInformation($"Client ConnectionId=> [[{connid}]] Already Connection Server!");
  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 connid = Context.ConnectionId;
  62. _logger.LogInformation($"Client ConnectionId=> [[{connid}]] Already Close Connection Server!");
  63. var model = UserStore.OnlineUser.Find(u => u.ConnectionId == connid);
  64. int count = UserStore.OnlineUser.RemoveAll(u => u.ConnectionId == connid);
  65. if (model != null)
  66. {
  67. var onlineUser = UserStore.OnlineUser.FindAll(u => u.GroupName == model.GroupName);
  68. Clients.Group(model.GroupName).SendAsync("GetUsersResponse", onlineUser);
  69. }
  70. return base.OnDisconnectedAsync(exception);
  71. }
  72. /**
  73. * 测试
  74. * */
  75. /// <summary>
  76. /// 给所有客户端发送消息
  77. /// </summary>
  78. /// <returns></returns>
  79. public async Task SendMessage(string data)
  80. {
  81. Console.WriteLine("Have one Data!");
  82. await Clients.All.SendAll(_common.SendAll(data));
  83. await Clients.Caller.SendAll(_common.SendCaller(data));
  84. }
  85. }
  86. }