M2Mqtt is a MQTT client available for all .Net platforms (.Net Framework, .Net Compact Framework and .Net Micro Framework) and WinRT platforms (Windows 8.1, Windows Phone 8.1 and Windows 10) for Internet of Things and M2M communication.
Visual Studio 使用 NuGet 工具搜索 “MQTT”:
安装即可使用:
// create client instance
MqttClient client = new MqttClient(IPAddress.Parse("10.10.0.59"));
// register to message received
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
// subscribe to the topic "/home/temperature" with QoS 2
client.Subscribe(new string[] { "/home/temperature" },
new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });
client.Publish("/home/door"/*Topic*/,
System.Text.Encoding.Default.GetBytes("qwesdfg")/*Message*/,
MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE/*QoS*/,
false/*retain*/);