Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using uPLibrary.Networking.M2Mqtt;
  4.  
  5. namespace m2mqtt
  6. {
  7.   class Program
  8.   {
  9.     private const int BrokerPort = 15240;
  10.     private const string Host = "farmer.cloudmqtt.com";
  11.     private const string Username = "";
  12.     private const string Password = "";
  13.  
  14.     static void Main(string[] args)
  15.     {
  16.       var topicName = "myFunTopic";
  17.       var client = new MqttClient(Host, BrokerPort, false, MqttSslProtocols.None, null, null);
  18.       client.Connect(Guid.NewGuid().ToString(), Username, Password);
  19.  
  20.       client.Publish(topicName, UTF8Encoding.UTF8.GetBytes("Hello from console!"), 1, true);
  21.       client.Subscribe(new[] { topicName }, new byte[] { 1});
  22.       client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
  23.       Console.ReadKey();
  24.       client.Disconnect();
  25.     }
  26.  
  27.     private static void Client_MqttMsgPublishReceived(object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e)
  28.     {
  29.       Console.WriteLine(sender.ToString());
  30.       Console.WriteLine($"{UTF8Encoding.UTF8.GetString(e.Message)} received from {e.Topic}");
  31.     }
  32.   }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement