ChatHub.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Microsoft.AspNetCore.SignalR;
  2. using OASystem.API.OAMethodLib.Hub.HubClients;
  3. using OASystem.API.OAMethodLib.SignalR.HubService;
  4. namespace OASystem.API.OAMethodLib.Hub.Hubs
  5. {
  6. [Authorize]
  7. public class ChatHub : Hub<IChatClient>
  8. {
  9. ILogger<ChatHub> _logger;
  10. public ChatHub(ILogger<ChatHub> logger, CommonService common)
  11. {
  12. _logger = logger;
  13. _common = common;
  14. }
  15. readonly CommonService _common;
  16. /// <summary>
  17. /// 客户端连接服务端
  18. /// </summary>
  19. /// <returns></returns>
  20. public override Task OnConnectedAsync()
  21. {
  22. var id = Context.ConnectionId;
  23. _logger.LogInformation($"Client ConnectionId=> [[{id}]] Already Connection Server!");
  24. return base.OnConnectedAsync();
  25. }
  26. /// <summary>
  27. /// 客户端断开连接
  28. /// </summary>
  29. /// <param name="exception"></param>
  30. /// <returns></returns>
  31. public override Task OnDisconnectedAsync(Exception exception)
  32. {
  33. var id = Context.ConnectionId;
  34. _logger.LogInformation($"Client ConnectionId=> [[{id}]] Already Close Connection Server!");
  35. return base.OnDisconnectedAsync(exception);
  36. }
  37. /**
  38. * 测试
  39. * */
  40. /// <summary>
  41. /// 给所有客户端发送消息
  42. /// </summary>
  43. /// <returns></returns>
  44. public async Task SendMessage(string data)
  45. {
  46. Console.WriteLine("Have one Data!");
  47. await Clients.All.SendAll(_common.SendAll(data));
  48. }
  49. }
  50. }