日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

c# 批量mqtt_C#使用 MQTTnet 快速实现 MQTT 通信(文末有完整Demo下载)

發(fā)布時間:2025/3/15 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c# 批量mqtt_C#使用 MQTTnet 快速实现 MQTT 通信(文末有完整Demo下载) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

MQTT(一)C#使用 MQTTnet 快速實現(xiàn) MQTT 通信(文末有完整Demo下載)

原創(chuàng)weixin_pwtank1983 發(fā)布于2018-02-03 10:22:24 閱讀數(shù) 36681 收藏

展開

目錄

MQTT(一)C#使用 MQTTnet 快速實現(xiàn) MQTT 通信(文末有完整Demo下載)

MQTT(二)在windows64位上安裝Python環(huán)境

MQTT(三)Python客戶端+net客戶端+net服務端 簡單通信

MQTT(四)樹莓派開機自動運行Python客戶端

MQTT(五)EMQ開源MQTT消息服務器

1 什么是 MQTT ?

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

2 MQTTnet

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

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 創(chuàng)建項目并導入類庫

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

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

4 服務端

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

4.1 創(chuàng)建服務端并啟動

創(chuàng)建服務端最簡單的方式是采用?MqttServerFactory?對象的?CreateMqttServer?方法來實現(xiàn),該方法需要一個MqttServerOptions?參數(shù)。

var options = new MqttServerOptions();

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

通過上述方式創(chuàng)建了一個?IMqttServer?對象后,調(diào)用其?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 相關事件

服務端支持?ClientConnected、ClientDisconnected?和?ApplicationMessageReceived?事件,分別用來檢查客戶端連接、客戶端斷開以及接收客戶端發(fā)來的消息。

其中?ClientConnected?和?ClientDisconnected?事件的事件參數(shù)一個客戶端連接對象?ConnectedMqttClient,通過該對象可以獲取客戶端ID標識?ClientId?和 MQTT 版本?ProtocolVersion。

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

5 客戶端

MQTT 與 HTTP 不同,后者是基于請求/響應方式的,服務器端無法直接發(fā)送數(shù)據(jù)給客戶端。而 MQTT 是基于發(fā)布/訂閱模式的,所有的客戶端均與服務端保持連接狀態(tài)。

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

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

5.1 創(chuàng)建客戶端并連接

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

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

創(chuàng)建客戶端對象后,調(diào)用其異步方法?ConnectAsync?來連接到服務端。

await mqttClient.ConnectAsync(options);

調(diào)用該方法時需要傳遞一個?MqttClientTcpOptions?對象(之前的版本是在創(chuàng)建對象時使用該選項),該選項包含了客戶端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 相關事件

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

5.2 訂閱消息

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

mqttClient.SubscribeAsync(new List {

new TopicFilter("家/客廳/空調(diào)/#", MqttQualityOfServiceLevel.AtMostOnce)

});

5.3 發(fā)布消息

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

mqttClient.SubscribeAsync(new List {

new TopicFilter("家/客廳/空調(diào)/#", MqttQualityOfServiceLevel.AtMostOnce)

});

得到?MqttApplicationMessage?消息對象后,通過客戶端對象調(diào)用其?PublishAsync?異步方法進行消息發(fā)布。

mqttClient.PublishAsync(appMsg);

6 跟蹤消息

MQTTnet?提供了一個靜態(tài)類?MqttNetTrace?來對消息進行跟蹤,該類可用于服務端和客戶端。MqttNetTrace?的事件TraceMessagePublished?用于跟蹤服務端和客戶端應用的日志消息,比如啟動、停止、心跳、消息訂閱和發(fā)布等。事件參數(shù)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個不同消息等級的靜態(tài)方法,Verbose、Information、Warning?和?Error,用于給出不同級別的日志消息,該消息將會在?TraceMessagePublished?事件中輸出,你可以使用?e.Level?進行過慮。

7 運行效果

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

7.1 服務端

7.2 客戶端1

7.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},協(xié)議版本:{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}]已連接,協(xié)議版本:{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 {

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("發(fā)布主題不能為空!");

return;

}

string inputString = txtSendMessage.Text.Trim();

var appMsg = new MqttApplicationMessage(topic, Encoding.UTF8.GetBytes(inputString), MqttQualityOfServiceLevel.AtMostOnce, false);

mqttClient.PublishAsync(appMsg);

}

}

}

9 本文的Demo下載地址

點擊下載 Demo

https://download.csdn.net/download/panwen1111/11018592

pw的其他原創(chuàng)文章導航

C#的MQTT系列

MQTT(一)C#使用 MQTTnet 快速實現(xiàn) MQTT 通信(文末有完整Demo下載)

MQTT(二)在windows64位上安裝Python環(huán)境

MQTT(三)Python客戶端+net客戶端+net服務端 簡單通信

MQTT(四)樹莓派開機自動運行Python客戶端

MQTT(五)EMQ開源MQTT消息服務器

C#的阿里物聯(lián)網(wǎng)平臺

阿里物聯(lián)網(wǎng)平臺(一)Windows系統(tǒng)+VS2017 模擬設備端接入

阿里物聯(lián)網(wǎng)平臺(二).net 實現(xiàn)移動端(WEB、HTML)與設備端通訊

落地項目

落地項目-智慧海綿城市

落地項目-智能焊機,鋼塑管行業(yè)物聯(lián)網(wǎng)應用

手持安卓小票打印一體機,小票打印應用

省城建設計院智慧海綿城市示范工程

————————————————

版權聲明:本文為CSDN博主「weixin_pwtank1983」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。

原文鏈接:https://blog.csdn.net/panwen1111/article/details/79245161

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結

以上是生活随笔為你收集整理的c# 批量mqtt_C#使用 MQTTnet 快速实现 MQTT 通信(文末有完整Demo下载)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。