參考 http://blog.csdn.net/leytton/article/details/51896738服務器
一、引入M2MQTT的dll庫文件
點擊此處下載 函數
二、創建客戶端鏈接並訂閱
- MqttClient client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS));
- MqttClient client = new MqttClient(MQTT_BROKER_ADDRESS);
-
- client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
-
- string clientId = Guid.NewGuid().ToString();
- client.Connect(clientId);
-
- client.Subscribe(new string[] { "/home/temperature" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
-
- ...
-
- void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
- {
- string msg = System.Text.Encoding.Default.GetString(e.Message);
- textBox1.AppendText("收到消息:" + msg + "\r\n");
- }
三、發佈消息
- client.Publish("/home/temperature", Encoding.UTF8.GetBytes("hello"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);