ClientHubController.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Microsoft.AspNetCore.SignalR;
  2. using OASystem.API.OAMethodLib;
  3. using OASystem.API.OAMethodLib.Hub.HubClients;
  4. using OASystem.API.OAMethodLib.Hub.Hubs;
  5. using OASystem.API.OAMethodLib.SignalR.Hubs;
  6. namespace OASystem.API.Controllers
  7. {
  8. /// <summary>
  9. /// 消息客户端
  10. /// </summary>
  11. [Route("api/[controller]/[action]")]
  12. public class ClientHubController : ControllerBase
  13. {
  14. private readonly ILogger<ClientHubController> _logger;
  15. private readonly IHubContext<ChatHub, IChatClient> _hubContext;
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. /// <param name="logger"></param>
  20. /// <param name="hubContext"></param>
  21. public ClientHubController(ILogger<ClientHubController> logger, IHubContext<ChatHub, IChatClient> hubContext)
  22. {
  23. _logger = logger;
  24. _hubContext = hubContext;
  25. }
  26. /// <summary>
  27. /// 获取在线用户
  28. /// </summary>
  29. /// <returns></returns>
  30. [HttpGet("GetOnlineUsers", Name = "GetOnlineUsers")]
  31. public List<UserModel> GetOnlineUsers()
  32. {
  33. return UserStore.OnlineUser;
  34. }
  35. /// <summary>
  36. /// 发送消息给客户端
  37. /// </summary>
  38. /// <param name="msg"></param>
  39. /// <returns></returns>
  40. [HttpPost("SendAllUserMessage", Name = "SendAllUserMessage")]
  41. public async Task<IActionResult> SendAllUserMessage(string msg)
  42. {
  43. await _hubContext.Clients.All.ReceiveMessage(msg);
  44. return Ok("Send Successful!");
  45. }
  46. /// <summary>
  47. /// 发送指定的消息给指定的客户端
  48. /// </summary>
  49. /// <param name="userIds"></param>
  50. /// <param name="title"></param>
  51. /// <param name="content"></param>
  52. /// <returns></returns>
  53. [HttpPost("SendCustomUserMessage", Name = "SendCustomUserMessage")]
  54. public async Task<IActionResult> SendCustomUserMessage(List<int> userIds, string title, string content)
  55. {
  56. string connId = string.Empty;
  57. //UserModel user = UserStore.OnlineUser.Where(it => it.UserId == userId).FirstOrDefault();
  58. //if (user != null)
  59. //{
  60. // connId = user.ConnectionId;
  61. //}
  62. //else
  63. //{
  64. // return Ok("Send Failed! User Not Online!");
  65. //}
  66. //await _hubContext.Clients.Client(connId).ReceiveMessage(date);
  67. await GeneralMethod.MessageIssueAndNotification(MessageTypeEnum.Announcement, title, content, userIds);
  68. return Ok("Send Successful!");
  69. }
  70. /// <summary>
  71. /// 消息 发布And 通知
  72. /// </summary>
  73. /// <param name="msgTypeEnum"></param>
  74. /// <param name="title"></param>
  75. /// <param name="content"></param>
  76. /// <param name="userIds"></param>
  77. /// <returns></returns>
  78. [HttpPost("MessageIssuserAndNotification", Name = "MessageIssuserAndNotification")]
  79. public async Task<IActionResult> MessageIssuserAndNotification(MessageTypeEnum msgTypeEnum, string title, string content, List<int> userIds)
  80. {
  81. var status = await GeneralMethod.MessageIssueAndNotification(msgTypeEnum, title, content, userIds);
  82. if (status)
  83. {
  84. return Ok("Operation Successful!");
  85. }
  86. else
  87. {
  88. return Ok("Operation Fail!");
  89. }
  90. }
  91. }
  92. }