Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using uPLibrary.Networking.M2Mqtt;
  4.  
  5.  
  6. namespace m2mqtt
  7. {
  8.   class Program
  9.   {
  10.     private const int BrokerPort = 15240;
  11.     private const string Host = "farmer.cloudmqtt.com";
  12.     private const string Username = "cizoylax";
  13.     private const string Password = "TlpiHKqgu2Rd";
  14.  
  15.     static void Main(string[] args)
  16.     {
  17.       var topicName = "myFunTopic";
  18.       var client = new MqttClient(Host, BrokerPort, false, MqttSslProtocols.None, null, null);
  19.       client.Connect(Guid.NewGuid().ToString(), Username, Password);
  20.  
  21.       client.Publish(topicName, UTF8Encoding.UTF8.GetBytes("Hello from console!"), 1, true);
  22.       client.Subscribe(new[] { topicName }, new byte[] { 1});
  23.       client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
  24.       Console.ReadKey();
  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