/// <summary> /// 发送消息 /// 不跟踪服务端的消息处理结果 /// </summary> /// <param name="topic">主题</param> /// <param name="message">消息</param> /// <exception cref="ArgumentNullException"></exception> /// <returns></returns> public MqttError SendMessageAsync(string topic, MqttMessage message) { if (string.IsNullOrEmpty(topic)) { throw new ArgumentNullException("topic"); } if (message == null) { throw new ArgumentNullException("message"); } var msg = message.ToStruct(); var opt = new MQTTAsync_responseOptions(); opt.Init(); var er = MQTTAsync.MQTTAsync_sendMessage(this.handle, topic, ref msg, ref opt); msg.Dispose(); return(er); }
/// <summary> /// 发送消息 /// 并跟踪服务端对消息回复结果 /// </summary> /// <param name="topic">主题</param> /// <param name="message">消息</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="MqttException"></exception> /// <returns></returns> public Task <bool> SendMessageTaskAsync(string topic, MqttMessage message) { if (string.IsNullOrEmpty(topic)) { throw new ArgumentNullException("topic"); } if (message == null) { throw new ArgumentNullException("message"); } var taskId = this.GenerateTaskId(); var setter = this.taskSetterTable.Create <bool>(taskId); var msg = message.ToStruct(); var opt = this.InitResponseOptions(taskId); var er = MQTTAsync.MQTTAsync_sendMessage(this.handle, topic, ref msg, ref opt); msg.Dispose(); this.EnsureSuccessCode(er); return(setter.GetTask()); }