平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

64 lines
2.2 KiB

  1. namespace SafeCampus.Plugin.Mqtt
  2. {
  3. /// <summary>
  4. /// <inheritdoc cref="INoticeService"/>
  5. /// </summary>
  6. [Injection(Named = "mqtt")]
  7. public class MqttNoticeService : INoticeService
  8. {
  9. public MqttNoticeService()
  10. {
  11. }
  12. /// <inheritdoc/>
  13. public async Task UpdatePassWord(string userId, List<string> clientIds, string message)
  14. {
  15. var _mqttClientManager = GetMqttClientManager();
  16. //发送修改密码消息
  17. await _mqttClientManager.GetClient().PublishAsync(MqttConst.Mqtt_TopicPrefix + userId, new MqttMessage
  18. {
  19. Data = new { Message = message, ClientIds = clientIds },
  20. MsgType = MqttConst.Mqtt_Message_UpdatePassword
  21. });
  22. }
  23. /// <inheritdoc/>
  24. public async Task NewMesage(List<string> userIds, List<string> clientIds, string message)
  25. {
  26. var _mqttClientManager = GetMqttClientManager();
  27. //遍历用户Id
  28. foreach (var userId in userIds)
  29. {
  30. //发送消息
  31. await _mqttClientManager.GetClient().PublishAsync(MqttConst.Mqtt_TopicPrefix + userId, new MqttMessage
  32. {
  33. Data = new { Message = message },
  34. MsgType = MqttConst.Mqtt_Message_New
  35. });
  36. }
  37. }
  38. /// <inheritdoc/>
  39. public async Task UserLoginOut(string userId, List<string> clientIds, string message)
  40. {
  41. var _mqttClientManager = GetMqttClientManager();
  42. //发送通知下线消息
  43. await _mqttClientManager.GetClient().PublishAsync(MqttConst.Mqtt_TopicPrefix + userId, new MqttMessage
  44. {
  45. Data = new { Message = message, ClientIds = clientIds },
  46. MsgType = MqttConst.Mqtt_Message_LoginOut
  47. });
  48. }
  49. /// <summary>
  50. /// 获取hubContext
  51. /// </summary>
  52. /// <returns></returns>
  53. private IMqttClientManager GetMqttClientManager()
  54. {
  55. //解析服务
  56. var service = App.GetService<IMqttClientManager>();
  57. return service;
  58. }
  59. }
  60. }