namespace SafeCampus.Plugin.Mqtt
{
///
///
///
[Injection(Named = "mqtt")]
public class MqttNoticeService : INoticeService
{
public MqttNoticeService()
{
}
///
public async Task UpdatePassWord(string userId, List clientIds, string message)
{
var _mqttClientManager = GetMqttClientManager();
//发送修改密码消息
await _mqttClientManager.GetClient().PublishAsync(MqttConst.Mqtt_TopicPrefix + userId, new MqttMessage
{
Data = new { Message = message, ClientIds = clientIds },
MsgType = MqttConst.Mqtt_Message_UpdatePassword
});
}
///
public async Task NewMesage(List userIds, List clientIds, string message)
{
var _mqttClientManager = GetMqttClientManager();
//遍历用户Id
foreach (var userId in userIds)
{
//发送消息
await _mqttClientManager.GetClient().PublishAsync(MqttConst.Mqtt_TopicPrefix + userId, new MqttMessage
{
Data = new { Message = message },
MsgType = MqttConst.Mqtt_Message_New
});
}
}
///
public async Task UserLoginOut(string userId, List clientIds, string message)
{
var _mqttClientManager = GetMqttClientManager();
//发送通知下线消息
await _mqttClientManager.GetClient().PublishAsync(MqttConst.Mqtt_TopicPrefix + userId, new MqttMessage
{
Data = new { Message = message, ClientIds = clientIds },
MsgType = MqttConst.Mqtt_Message_LoginOut
});
}
///
/// 获取hubContext
///
///
private IMqttClientManager GetMqttClientManager()
{
//解析服务
var service = App.GetService();
return service;
}
}
}