/// <summary> /// 发送消息 /// </summary> /// <param name="msg">发送消息</param> public void SendMessage(string msg, MqttQoS qos = MqttQoS.AtLeastOnce) { var mqttMsg = new MqttMessage { Retain = false, Dup = false, Payload = Encoding.UTF8.GetBytes(msg), QoS = qos }; mqttClient.SendMessageAsync(this.topic, mqttMsg); }
/// <summary> /// 订阅 /// </summary> /// <param name="topic">主题</param> /// <param name="qos">质量</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="MqttException"></exception> /// <returns></returns> private ITaskSetter <bool> SubscribeInternal(string topic, MqttQoS qos) { if (string.IsNullOrEmpty(topic)) { throw new ArgumentNullException("topic"); } var taskId = this.GenerateTaskId(); var setter = this.taskSetterTable.Create <bool>(taskId); var opt = this.InitResponseOptions(taskId); var er = MQTTAsync.MQTTAsync_subscribe(this.handle, topic, qos, ref opt); this.EnsureSuccessCode(er); return(setter); }
public MqttMsgPublishReceivedEventArgs(byte[] Message, byte QosLevel, string Topic) { this.Message = new string(Encoding.UTF8.GetChars(Message)); this.QosLevel = (MqttQoS)QosLevel; this.Topic = Topic; }
public virtual int SubscribeToEvents(MqttQoS qualityOfService, string[] subTopics) { if (mqttClient != null) { int returnCode = mqttClient.Subscribe(subTopics, new byte[] { (byte)qualityOfService }); return returnCode; } return -1; }
public static extern MqttError MQTTAsync_subscribe( IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string topic, MqttQoS qos, ref MQTTAsync_responseOptions responseOption);
public override int SubscribeToEvents(MqttQoS qualityOfService, string[] subTopics) { int returnCode = base.SubscribeToEvents(qualityOfService, subTopics); NDLogger.Log("Subscribed to " + subTopics, LogLevel.Verbose); return returnCode; }
/// <summary> /// 订阅 /// </summary> /// <param name="topic">主题</param> /// <param name="qos">质量</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="MqttException"></exception> /// <returns></returns> public Task <bool> SubscribeAsync(string topic, MqttQoS qos) { return(this.SubscribeInternal(topic, qos).GetTask()); }
/// <summary> /// 订阅 /// </summary> /// <param name="topic">主题</param> /// <param name="qos">质量</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="MqttException"></exception> /// <returns></returns> public bool Subscribe(string topic, MqttQoS qos) { return(this.SubscribeInternal(topic, qos).GetResult()); }
/// <summary> /// mqtt消息 /// </summary> /// <param name="qos">qos</param> /// <param name="payload">数据</param> public MqttMessage(MqttQoS qos, byte[] payload) { this.QoS = qos; this.Payload = payload; }
/// <summary> /// mqtt消息 /// </summary> /// <param name="qos">qos</param> /// <param name="payload">数据</param> public MqttMessage(MqttQoS qos, string payload) { this.QoS = qos; this.Payload = Encoding.UTF8.GetBytes(payload); }