Advanced Distributed Systems module at HSLU
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

46 lines
1.8 KiB

using System.Text;
using System.Threading;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
namespace MqttTest
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello MQTT World! Press Space to send test message to topic 'scada/status'.");
// creating an MqttClient object
var client = new uPLibrary.Networking.M2Mqtt.MqttClient("10.180.254.80");
// register to message received
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
// generate a clientID and connect to Broker
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
Console.WriteLine($"Connect result: {client.IsConnected}");
// subscribe to a topic
client.Subscribe(new string[] { "scada/status" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
// Endless loop
while (true)
{
Thread.Sleep(50);
if (Console.KeyAvailable)
{
var key = Console.ReadKey();
if (key.Key == ConsoleKey.Spacebar)
{
Console.WriteLine($"Sending \"Test Message\" to topic 'scada/status'");
var publishResult = client.Publish("scada/status", Encoding.ASCII.GetBytes("Test Message"));
Console.WriteLine($"Publish result: {publishResult}");
}
}
}
}
static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
// handle message received
Console.Write("Message received: ");
Console.Write(Encoding.UTF8.GetString(e.Message) + "\n");
}
}
}