/// <summary> /// 订阅主题 /// </summary> /// <param name="topic">主题内容</param> /// <param name="requestedQoS">主题服务等级</param> /// <returns></returns> public virtual Result Subscribe(string topic, Qos requestedQoS = Qos.QoS0) { return(SendPackage(new TopicDataPackage(MessageType.Subscribe) { Topic = topic, Identifier = ++Identifier, RequestedQoS = requestedQoS })); }
public void Publish(string topic, string value, Qos qos = Qos.ExactlyOnce, bool retain = false, bool sendOnlyChanges = false) { try { if (sendOnlyChanges) { var isChangedValue = IsChangedValue(topic, value); if (isChangedValue == false) { return; } } var message = Encoding.UTF8.GetBytes(value); lock (_lock) { ++_publishedButNotConfirmedMessages; _resetEvent.Reset(); } _mqttClient.Publish(topic, message, (byte)qos, retain); } catch (Exception) { //Ignored } }
public void Subscribe(string topic, Qos qos) { Topics.Add(new TopicQos { Topic = topic, Qos = qos, }); }
public void Subscribe(string topic, Qos qos = Qos.AtMostOnce) { var msg = new SubscribeMessage(); msg.FixedHeader.Qos = Qos.AtLeastOnce; msg.MessageIdentifier = 0; msg.Subscribe(topic, qos); conn.SendMessage(msg); }
public void Publish(string topic, byte[] data, Qos qos = Qos.AtMostOnce) { var msg = new PublishMessage(); msg.FixedHeader.Qos = qos; //msg.MessageIdentifier += MessageIdentifier; msg.TopicName = topic; msg.Payload = data; conn.SendMessage(msg); }
public Subscription(string topic, Action <ReceivedMessageArguments> receivedMessageAction, Qos qos) { Topic = topic; ReceivedMessageAction = receivedMessageAction; Qos = qos; if (topic.Contains("+") || topic.Contains("#")) { _regexTopic = topic.Replace("+", "[^/]+").Replace("#", ".*"); } }
/// <summary> /// 初始化内容。 /// </summary> protected virtual void Init() { var index = 0; Identifier = (ushort)(Data[0] << 8 | Data[1]); var topicLength = Data[2] << 8 | Data[3]; Topic = System.Text.ASCIIEncoding.UTF8.GetString(Data, 4, topicLength); index += 4 + topicLength; if (Data.Length > index) { RequestedQoS = (Qos)Data[index]; } }
public async Task PublishAsync(string topic, byte[] payload, Dictionary <string, string> properties, Qos qos, CancellationToken cancellationToken) { var mqttMessageBuilder = new MqttApplicationMessageBuilder() .WithTopic(topic) .WithPayload(payload) .WithQualityOfServiceLevel(MapQos(qos)); if (properties != null) { foreach (var entry in properties) { mqttMessageBuilder.WithUserProperty(entry.Key, entry.Value); } } MqttClientPublishResult publishResult; try { publishResult = await client.PublishAsync(mqttMessageBuilder.Build(), cancellationToken); } catch (Exception e) { throw new MqttException(cause: e); } ValidatePublishResult(publishResult); }
static MqttQualityOfServiceLevel MapQos(Qos qos) => (MqttQualityOfServiceLevel)(int)qos;