ClientHubController.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. public ClientHubController(ILogger<ClientHubController> logger, IHubContext<ChatHub, IChatClient> hubContext)
  17. {
  18. _logger = logger;
  19. _hubContext = hubContext;
  20. }
  21. /// <summary>
  22. /// 获取在线用户
  23. /// </summary>
  24. /// <returns></returns>
  25. [HttpGet("GetOnlineUsers", Name = "GetOnlineUsers")]
  26. public List<UserModel> GetOnlineUsers()
  27. {
  28. return UserStore.OnlineUser;
  29. }
  30. /// <summary>
  31. /// 发送消息给客户端
  32. /// </summary>
  33. /// <param name="userid"></param>
  34. /// <param name="date"></param>
  35. /// <param name="hubContext"></param>
  36. /// <returns></returns>
  37. [HttpPost("SendAllUserMessage", Name = "SendAllUserMessage")]
  38. public async Task<IActionResult> SendAllUserMessage(string msg)
  39. {
  40. await _hubContext.Clients.All.ReceiveMessage(msg);
  41. return Ok("Send Successful!");
  42. }
  43. /// <summary>
  44. /// 发送指定的消息给指定的客户端
  45. /// </summary>
  46. /// <param name="userid"></param>
  47. /// <param name="date"></param>
  48. /// <param name="hubContext"></param>
  49. /// <returns></returns>
  50. [HttpPost("SendCustomUserMessage", Name = "SendCustomUserMessage")]
  51. public async Task<IActionResult> SendCustomUserMessage(List<int> userIds, string title, string content)
  52. {
  53. string connId = string.Empty;
  54. //UserModel user = UserStore.OnlineUser.Where(it => it.UserId == userId).FirstOrDefault();
  55. //if (user != null)
  56. //{
  57. // connId = user.ConnectionId;
  58. //}
  59. //else
  60. //{
  61. // return Ok("Send Failed! User Not Online!");
  62. //}
  63. //await _hubContext.Clients.Client(connId).ReceiveMessage(date);
  64. GeneralMethod.MessageIssueAndNotification(MessageTypeEnum.Announcement, title, content, userIds);
  65. return Ok("Send Successful!");
  66. }
  67. /// <summary>
  68. /// 消息 发布And 通知
  69. /// </summary>
  70. /// <param name="msgTypeEnum"></param>
  71. /// <param name="title"></param>
  72. /// <param name="content"></param>
  73. /// <param name="userIds"></param>
  74. /// <returns></returns>
  75. [HttpPost("MessageIssuserAndNotification", Name = "MessageIssuserAndNotification")]
  76. public async Task<IActionResult> MessageIssuserAndNotification(MessageTypeEnum msgTypeEnum, string title, string content, List<int> userIds)
  77. {
  78. var status = await GeneralMethod.MessageIssueAndNotification(msgTypeEnum, title, content, userIds);
  79. if (status)
  80. {
  81. return Ok("Operation Successful!");
  82. }
  83. else
  84. {
  85. return Ok("Operation Fail!");
  86. }
  87. }
  88. }
  89. }