使用 MQTTnet 快速實現 MQTT 通信


1 什么是 MQTT ?

MQTT(Message Queuing Telemetry Transport,消息隊列遙測傳輸)是 IBM 開發的一個即時通訊協議,有可能成為物聯網的重要組成部分。MQTT 是基於二進制消息的發布/訂閱編程模式的消息協議,如今已經成為 OASIS 規范,由於規范很簡單,非常適合需要低功耗和網絡帶寬有限的 IoT 場景。MQTT官網

2 MQTTnet

MQTTnet 是一個基於 MQTT 通信的高性能 .NET 開源庫,它同時支持 MQTT 服務器端和客戶端。而且作者也保持更新,目前支持新版的.NET core,這也是選擇 MQTTnet 的原因。 MQTTnet 在 Github 並不是下載最多的 .NET 的 MQTT 開源庫,其他的還 MqttDotNetnMQTTM2MQTT

MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.

3 創建項目並導入類庫

這里我們使用 Visual Studio 2017 創建一個空解決方案,並在其中添加兩個項目,即一個服務端和一個客戶端,服務端項目模板選擇最新的 .NET Core 控制台應用,客戶端項目選擇傳統的 WinForm 窗體應用程序。.NET Core 項目模板如下圖所示: .NET Core 控制台應用

在解決方案在右鍵單擊-選擇“管理解決方案的 NuGet 程序包”-在“瀏覽”選項卡下面搜索 MQTTnet,為服務端項目和客戶端項目都安裝上 MQTTnet 庫,當前最新穩定版為 2.4.0。項目結構如下圖所示: 項目結構

4 服務端

MQTT 服務端主要用於與多個客戶端保持連接,並處理客戶端的發布和訂閱等邏輯。一般很少直接從服務端發送消息給客戶端(可以使用 mqttServer.Publish(appMsg); 直接發送消息),多數情況下服務端都是轉發主題匹配的客戶端消息,在系統中起到一個中介的作用。

4.1 創建服務端並啟動

創建服務端最簡單的方式是采用 MqttServerFactory 對象的 CreateMqttServer 方法來實現,該方法需要一個 MqttServerOptions 參數。

var options = new MqttServerOptions(); var mqttServer = new MqttServerFactory().CreateMqttServer(options);

通過上述方式創建了一個 IMqttServer 對象后,調用其 StartAsync 方法即可啟動 MQTT 服務。值得注意的是:之前版本采用的是 Start 方法,作者也是緊跟 C# 語言新特性,能使用異步的地方也都改為異步方式。

await mqttServer.StartAsync();

4.2 驗證客戶端

MqttServerOptions 選項中,你可以使用 ConnectionValidator 來對客戶端連接進行驗證。比如客戶端ID標識 ClientId,用戶名 Username 和密碼 Password 等。

var options = new MqttServerOptions { ConnectionValidator = c => { if (c.ClientId.Length < 10) { return MqttConnectReturnCode.ConnectionRefusedIdentifierRejected; } if (c.Username != "xxx" || c.Password != "xxx") { return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; } return MqttConnectReturnCode.ConnectionAccepted; } };

4.3 相關事件

服務端支持 ClientConnectedClientDisconnectedApplicationMessageReceived 事件,分別用來檢查客戶端連接、客戶端斷開以及接收客戶端發來的消息。

其中 ClientConnectedClientDisconnected 事件的事件參數一個客戶端連接對象 ConnectedMqttClient,通過該對象可以獲取客戶端ID標識 ClientId 和 MQTT 版本 ProtocolVersion

ApplicationMessageReceived 的事件參數包含了客戶端ID標識 ClientId 和 MQTT 應用消息 MqttApplicationMessage 對象,通過該對象可以獲取主題 Topic、QoS QualityOfServiceLevel 和消息內容 Payload 等信息。

5 客戶端

MQTT 與 HTTP 不同,后者是基於請求/響應方式的,服務器端無法直接發送數據給客戶端。而 MQTT 是基於發布/訂閱模式的,所有的客戶端均與服務端保持連接狀態。

那么客戶端之間是如何通信的呢?

具體邏輯是:某些客戶端向服務端訂閱它感興趣(主題)的消息,另一些客戶端向服務端發布(主題)消息,服務端將訂閱和發布的主題進行匹配,並將消息轉發給匹配通過的客戶端。

5.1 創建客戶端並連接

使用 MQTTnet 創建 MQTT 也非常簡單,只需要使用 MqttClientFactory 對象的 CreateMqttClient 方法即可。

var mqttClient = new MqttClientFactory().CreateMqttClient();

創建客戶端對象后,調用其異步方法 ConnectAsync 來連接到服務端。

await mqttClient.ConnectAsync(options);

調用該方法時需要傳遞一個 MqttClientTcpOptions 對象(之前的版本是在創建對象時使用該選項),該選項包含了客戶端ID標識 ClientId、服務端地址(可以使用IP地址或域名)Server、端口號 Port、用戶名 UserName、密碼 Password 等信息。

var options = new MqttClientTcpOptions { Server = "127.0.0.1", ClientId = "c001", UserName = "u001", Password = "p001", CleanSession = true };

5.2 相關事件

客戶端支持 ConnectedDisconnectedApplicationMessageReceived 事件,用來處理客戶端與服務端連接、客戶端從服務端斷開以及客戶端收到消息的事情。

5.2 訂閱消息

客戶端連接到服務端之后,可以使用 SubscribeAsync 異步方法訂閱消息,該方法可以傳入一個可枚舉或可變參數的主題過濾器 TopicFilter 參數,主題過濾器包含主題名和 QoS 等級。

mqttClient.SubscribeAsync(new List<TopicFilter> { new TopicFilter("家/客廳/空調/#", MqttQualityOfServiceLevel.AtMostOnce) });

5.3 發布消息

發布消息前需要先構建一個消息對象 MqttApplicationMessage,最直接的方法是使用其實構造函數,傳入主題、內容、Qos 等參數。

var appMsg = new MqttApplicationMessage("家/客廳/空調/開關", Encoding.UTF8.GetBytes("消息內容"), MqttQualityOfServiceLevel.AtMostOnce, false);

得到 MqttApplicationMessage 消息對象后,通過客戶端對象調用其 PublishAsync 異步方法進行消息發布。

mqttClient.PublishAsync(appMsg);

6 跟蹤消息

MQTTnet 提供了一個靜態類 MqttNetTrace 來對消息進行跟蹤,該類可用於服務端和客戶端。MqttNetTrace 的事件 TraceMessagePublished 用於跟蹤服務端和客戶端應用的日志消息,比如啟動、停止、心跳、消息訂閱和發布等。事件參數 MqttNetTraceMessagePublishedEventArgs 包含了線程ID ThreadId、來源 Source、日志級別 Level、日志消息 Message、異常信息 Exception 等。

MqttNetTrace.TraceMessagePublished += MqttNetTrace_TraceMessagePublished;

private static void MqttNetTrace_TraceMessagePublished(object sender, MqttNetTraceMessagePublishedEventArgs e) { Console.WriteLine($">> 線程ID:{e.ThreadId} 來源:{e.Source} 跟蹤級別:{e.Level} 消息: {e.Message}"); if (e.Exception != null) { Console.WriteLine(e.Exception); } }

同時 MqttNetTrace 類還提供了4個不同消息等級的靜態方法,VerboseInformationWarningError,用於給出不同級別的日志消息,該消息將會在 TraceMessagePublished 事件中輸出,你可以使用 e.Level 進行過慮。

7 運行效果

以下分別是服務端、客戶端1和客戶端2的運行效果,其中客戶端1和客戶端2只是同一個項目運行了兩個實例。客戶端1用於訂閱傳感器的“溫度”數據,並模擬上位機(如 APP 等)發送開關控制命令;客戶端2訂閱上位機傳來的“開關”控制命令,並模擬溫度傳感器上報溫度數據。

7.1 服務端

服務端

7.2 客戶端1

客戶端1

7.2 客戶端2

客戶端2

8 Demo代碼

8.1 服務端代碼

using MQTTnet; using MQTTnet.Core.Adapter; using MQTTnet.Core.Diagnostics; using MQTTnet.Core.Protocol; using MQTTnet.Core.Server; using System; using System.Text; using System.Threading; namespace MqttServerTest { class Program { private static MqttServer mqttServer = null; static void Main(string[] args) { MqttNetTrace.TraceMessagePublished += MqttNetTrace_TraceMessagePublished; new Thread(StartMqttServer).Start(); while (true) { var inputString = Console.ReadLine().ToLower().Trim(); if (inputString == "exit") { mqttServer?.StopAsync(); Console.WriteLine("MQTT服務已停止!"); break; } else if (inputString == "clients") { foreach (var item in mqttServer.GetConnectedClients()) { Console.WriteLine($"客戶端標識:{item.ClientId},協議版本:{item.ProtocolVersion}"); } } else { Console.WriteLine($"命令[{inputString}]無效!"); } } } private static void StartMqttServer() { if (mqttServer == null) { try { var options = new MqttServerOptions { ConnectionValidator = p => { if (p.ClientId == "c001") { if (p.Username != "u001" || p.Password != "p001") { return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; } } return MqttConnectReturnCode.ConnectionAccepted; } }; mqttServer = new MqttServerFactory().CreateMqttServer(options) as MqttServer; mqttServer.ApplicationMessageReceived += MqttServer_ApplicationMessageReceived; mqttServer.ClientConnected += MqttServer_ClientConnected; mqttServer.ClientDisconnected += MqttServer_ClientDisconnected; } catch (Exception ex) { Console.WriteLine(ex.Message); return; } } mqttServer.StartAsync(); Console.WriteLine("MQTT服務啟動成功!"); } private static void MqttServer_ClientConnected(object sender, MqttClientConnectedEventArgs e) { Console.WriteLine($"客戶端[{e.Client.ClientId}]已連接,協議版本:{e.Client.ProtocolVersion}"); } private static void MqttServer_ClientDisconnected(object sender, MqttClientDisconnectedEventArgs e) { Console.WriteLine($"客戶端[{e.Client.ClientId}]已斷開連接!"); } private static void MqttServer_ApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) { Console.WriteLine($"客戶端[{e.ClientId}]>> 主題:{e.ApplicationMessage.Topic} 負荷:{Encoding.UTF8.GetString(e.ApplicationMessage.Payload)} Qos:{e.ApplicationMessage.QualityOfServiceLevel} 保留:{e.ApplicationMessage.Retain}"); } private static void MqttNetTrace_TraceMessagePublished(object sender, MqttNetTraceMessagePublishedEventArgs e) { /*Console.WriteLine($">> 線程ID:{e.ThreadId} 來源:{e.Source} 跟蹤級別:{e.Level} 消息: {e.Message}"); if (e.Exception != null) { Console.WriteLine(e.Exception); }*/ } } }

8.2 客戶端代碼

using MQTTnet; using MQTTnet.Core; using MQTTnet.Core.Client; using MQTTnet.Core.Packets; using MQTTnet.Core.Protocol; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace MqttClientWin { public partial class FmMqttClient : Form { private MqttClient mqttClient = null; public FmMqttClient() { InitializeComponent(); Task.Run(async () => { await ConnectMqttServerAsync(); }); } private async Task ConnectMqttServerAsync() { if (mqttClient == null) { mqttClient = new MqttClientFactory().CreateMqttClient() as MqttClient; mqttClient.ApplicationMessageReceived += MqttClient_ApplicationMessageReceived; mqttClient.Connected += MqttClient_Connected; mqttClient.Disconnected += MqttClient_Disconnected; } try { var options = new MqttClientTcpOptions { Server = "127.0.0.1", ClientId = Guid.NewGuid().ToString().Substring(0, 5), UserName = "u001", Password = "p001", CleanSession = true }; await mqttClient.ConnectAsync(options); } catch (Exception ex) { Invoke((new Action(() => { txtReceiveMessage.AppendText($"連接到MQTT服務器失敗!" + Environment.NewLine + ex.Message + Environment.NewLine); }))); } } private void MqttClient_Connected(object sender, EventArgs e) { Invoke((new Action(() => { txtReceiveMessage.AppendText("已連接到MQTT服務器!" + Environment.NewLine); }))); } private void MqttClient_Disconnected(object sender, EventArgs e) { Invoke((new Action(() => { txtReceiveMessage.AppendText("已斷開MQTT連接!" + Environment.NewLine); }))); } private void MqttClient_ApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) { Invoke((new Action(() => { txtReceiveMessage.AppendText($">> {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}{Environment.NewLine}"); }))); } private void BtnSubscribe_ClickAsync(object sender, EventArgs e) { string topic = txtSubTopic.Text.Trim(); if (string.IsNullOrEmpty(topic)) { MessageBox.Show("訂閱主題不能為空!"); return; } if (!mqttClient.IsConnected) { MessageBox.Show("MQTT客戶端尚未連接!"); return; } mqttClient.SubscribeAsync(new List<TopicFilter> { new TopicFilter(topic, MqttQualityOfServiceLevel.AtMostOnce) }); txtReceiveMessage.AppendText($"已訂閱[{topic}]主題" + Environment.NewLine); txtSubTopic.Enabled = false; btnSubscribe.Enabled = false; } private void BtnPublish_Click(object sender, EventArgs e) { string topic = txtPubTopic.Text.Trim(); if (string.IsNullOrEmpty(topic)) { MessageBox.Show("發布主題不能為空!"); return; } string inputString = txtSendMessage.Text.Trim(); var appMsg = new MqttApplicationMessage(topic, Encoding.UTF8.GetBytes(inputString), MqttQualityOfServiceLevel.AtMostOnce, false); mqttClient.PublishAsync(appMsg); } } }

9 參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM