Ejemplo n.º 1
0
        public void Subscribe(string topic = null)
        {
            if (!IsConnected)
            {
                Console.Error.WriteLine("No connection for client: " + ClientId);
                throw new Exception("No connection for client: " + ClientId);
            }
            if (topic == null)
            {
                topic = Settings.Client.DefaultSubscribeTopicName;
            }

            if (m_DecoderTask == null && !Settings.Client.RawDataMode)
            {
                m_MessageQueue            = new ConcurrentQueue <DecodeMessage.MqttMessage>();
                m_Decoder                 = new DecodeMessage(m_MessageQueue, Options);
                m_Decoder.MessageDecoded += DecoderOnMessageDecoded;
                m_DecoderTask             = Task.Run(() => m_Decoder.Start());
            }

            m_MqttClient.SubscribeAsync(new MqttTopicFilter
            {
                Topic = topic,
                QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce
            }
                                        ).Wait();

            Logger.Debug($"MQTT SubscribeAsync DONE, Topic: {topic}");
        }