コード例 #1
0
 /// <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
     }));
 }
コード例 #2
0
        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
            }
        }
コード例 #3
0
ファイル: SubscribeMessage.cs プロジェクト: expansion/nMqtt
 public void Subscribe(string topic, Qos qos)
 {
     Topics.Add(new TopicQos
     {
         Topic = topic,
         Qos = qos,
     });
 }
コード例 #4
0
 public void Subscribe(string topic, Qos qos)
 {
     Topics.Add(new TopicQos
     {
         Topic = topic,
         Qos   = qos,
     });
 }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        public Subscription(string topic, Action <ReceivedMessageArguments> receivedMessageAction, Qos qos)
        {
            Topic = topic;
            ReceivedMessageAction = receivedMessageAction;
            Qos = qos;

            if (topic.Contains("+") || topic.Contains("#"))
            {
                _regexTopic = topic.Replace("+", "[^/]+").Replace("#", ".*");
            }
        }
コード例 #8
0
        /// <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];
            }
        }
コード例 #9
0
ファイル: MQTTnetClient.cs プロジェクト: davilu/edge-test
        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);
        }
コード例 #10
0
ファイル: MQTTnetClient.cs プロジェクト: davilu/edge-test
 static MqttQualityOfServiceLevel MapQos(Qos qos) => (MqttQualityOfServiceLevel)(int)qos;