I've already worked with mqtt in Java. Now I need to create a C# application to subcribe and publish mqtt messages.
using MqttDotNet library
IMqtt _client = MqttClientFactory.CreateClient(connectionString, clientId);
What is the connectionString
?
using M2Mqtt library
The connection succeeded, but I did not receive any published messages.
This is my code:
class Program
{
static void Main(string[] args)
{
var client = new MqttClient(IPAddress.Parse("myTestIP"));
// register to message received
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
var clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
// subscribe to the topic "/home/temperature" with QoS 2
client.Subscribe(
new string[] {"testTopic"},
new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
}
static void client_MqttMsgPublishReceived(
object sender, MqttMsgPublishEventArgs e)
{
// handle message received
Console.WriteLine("message=" + e.Message.ToString());
}
}
This my message publishing code:
mosquitto_pub -d -h testIp -t "testTopic" -m "haai"
I don't think that the MqttDotNet is currently mantained. I could suggest to use my M2Mqtt client and found documentation on official web site here : https://m2mqtt.wordpress.com/
The M2Mqtt client is available on Nuget as package too here : https://www.nuget.org/packages/M2Mqtt/
Paolo.