MQTT(Message Queuing Telemetry Transport,消息隊列遙測傳輸)是 IBM 開發的一個即時通信協議,有可能成爲物聯網的重要組成部分。MQTT 是基於二進制消息的發佈/訂閱編程模式的消息協議,現在已經成爲 OASIS 規範,因爲規範很簡單,很是適合須要低功耗和網絡帶寬有限的 IoT 場景。MQTT官網javascript
MQTTnet 是一個基於 MQTT 通訊的高性能 .NET 開源庫,它同時支持 MQTT 服務器端和客戶端。並且做者也保持更新,目前支持新版的.NET core,這也是選擇 MQTTnet 的緣由。 MQTTnet 在 Github 並非下載最多的 .NET 的 MQTT 開源庫,其餘的還 MqttDotNet、nMQTT、M2MQTT 等php
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/.css
這裏咱們使用 Visual Studio 2017 建立一個空解決方案,並在其中添加兩個項目,即一個服務端和一個客戶端,服務端項目模板選擇最新的 .NET Core 控制檯應用,客戶端項目選擇傳統的 WinForm 窗體應用程序。.NET Core 項目模板以下圖所示: html
在解決方案在右鍵單擊-選擇「管理解決方案的 NuGet 程序包」-在「瀏覽」選項卡下面搜索 MQTTnet,爲服務端項目和客戶端項目都安裝上 MQTTnet 庫,當前最新穩定版爲 2.4.0。項目結構以下圖所示: java
MQTT 服務端主要用於與多個客戶端保持鏈接,並處理客戶端的發佈和訂閱等邏輯。通常不多直接從服務端發送消息給客戶端(可使用 mqttServer.Publish(appMsg);
直接發送消息),多數狀況下服務端都是轉發主題匹配的客戶端消息,在系統中起到一箇中介的做用。python
建立服務端最簡單的方式是採用 MqttServerFactory
對象的 CreateMqttServer
方法來實現,該方法須要一個 MqttServerOptions
參數。git
var options = new MqttServerOptions(); var mqttServer = new MqttServerFactory().CreateMqttServer(options);
經過上述方式建立了一個 IMqttServer
對象後,調用其 StartAsync
方法便可啓動 MQTT 服務。值得注意的是:以前版本採用的是 Start
方法,做者也是緊跟 C# 語言新特性,能使用異步的地方也都改成異步方式。github
await mqttServer.StartAsync();
在 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; } };
服務端支持 ClientConnected
、ClientDisconnected
和 ApplicationMessageReceived
事件,分別用來檢查客戶端鏈接、客戶端斷開以及接收客戶端發來的消息。swift
其中 ClientConnected
和 ClientDisconnected
事件的事件參數一個客戶端鏈接對象 ConnectedMqttClient
,經過該對象能夠獲取客戶端ID標識 ClientId
和 MQTT 版本 ProtocolVersion
。
ApplicationMessageReceived
的事件參數包含了客戶端ID標識 ClientId
和 MQTT 應用消息 MqttApplicationMessage
對象,經過該對象能夠獲取主題 Topic
、QoS QualityOfServiceLevel
和消息內容 Payload
等信息。
MQTT 與 HTTP 不一樣,後者是基於請求/響應方式的,服務器端沒法直接發送數據給客戶端。而 MQTT 是基於發佈/訂閱模式的,全部的客戶端均與服務端保持鏈接狀態。
那麼客戶端之間是如何通訊的呢?
具體邏輯是:某些客戶端向服務端訂閱它感興趣(主題)的消息,另外一些客戶端向服務端發佈(主題)消息,服務端將訂閱和發佈的主題進行匹配,並將消息轉發給匹配經過的客戶端。
使用 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 };
客戶端支持 Connected
、Disconnected
和 ApplicationMessageReceived
事件,用來處理客戶端與服務端鏈接、客戶端從服務端斷開以及客戶端收到消息的事情。
客戶端鏈接到服務端以後,可使用 SubscribeAsync
異步方法訂閱消息,該方法能夠傳入一個可枚舉或可變參數的主題過濾器 TopicFilter
參數,主題過濾器包含主題名和 QoS 等級。
mqttClient.SubscribeAsync(new List<TopicFilter> { new TopicFilter("家/客廳/空調/#", MqttQualityOfServiceLevel.AtMostOnce) });
發佈消息前須要先構建一個消息對象 MqttApplicationMessage
,最直接的方法是使用其實構造函數,傳入主題、內容、Qos 等參數。
var appMsg = new MqttApplicationMessage("家/客廳/空調/開關", Encoding.UTF8.GetBytes("消息內容"), MqttQualityOfServiceLevel.AtMostOnce, false);
獲得 MqttApplicationMessage
消息對象後,經過客戶端對象調用其 PublishAsync
異步方法進行消息發佈。
mqttClient.PublishAsync(appMsg);
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個不一樣消息等級的靜態方法,Verbose
、Information
、Warning
和 Error
,用於給出不一樣級別的日誌消息,該消息將會在 TraceMessagePublished
事件中輸出,你可使用 e.Level
進行過慮。
如下分別是服務端、客戶端1和客戶端2的運行效果,其中客戶端1和客戶端2只是同一個項目運行了兩個實例。客戶端1用於訂閱傳感器的「溫度」數據,並模擬上位機(如 APP 等)發送開關控制命令;客戶端2訂閱上位機傳來的「開關」控制命令,並模擬溫度傳感器上報溫度數據。
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); }*/ } } }
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); } } }