Exemplo n.º 1
0
 /// <summary>
 /// Subscribe the specified topic.
 /// </summary>
 /// <param name="topic">Topic name.</param>
 /// <param name="callback">Callback for receiving the subscribed topic messages.</param>
 public MqttClientHelper Subscribe(string topic, Action <string, string> callback)
 {
     mqttClient.ListenTo <String, AsciiPayloadConverter>(topic, (MqttQos)1)
     //.ObserveOn(System.Threading.SynchronizationContext.Current)
     .Subscribe(msg =>
     {
         callback(msg.Topic, msg.Payload);
         //Console.WriteLine("MQTT {0} : {1}", msg.Topic, msg.Payload);
     });
     return(this);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Subscribes to the specified topic.
        /// </summary>
        /// <param name="topic">The topic.</param>
        /// <param name="qos">The qos.</param>
        public void Subscribe(string topic, byte qos)
        {
            if (client == null)
            {
                throw new InvalidOperationException("You must connect before you can subscribe to a topic.");
            }

            var sub = client.ListenTo(topic, (MqttQos)qos)
                      .ObserveOn(SynchronizationContext.Current)
                      .Subscribe(msg => ClientMessageArrived(instance, new MqttMessageEventArgs(msg.Topic, msg.Payload)));

            topicSubscriptions.Add(topic, sub);
            Trace.WriteLine(String.Format("Subscribed to Topic '{0}'.", topic));
            if (TopicSubscribed != null)
            {
                syncContext.Post((data) => this.TopicSubscribed(instance, new TopicSubscribedEventArgs(topic)), null);
            }
        }