日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > C# >内容正文

C#

C# 接受MQTT服务器推送的消息

發(fā)布時(shí)間:2023/12/4 C# 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 接受MQTT服务器推送的消息 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言:

?MQTT是IBM開(kāi)發(fā)的一個(gè)即時(shí)通訊協(xié)議。MQTT是面向M2M和物聯(lián)網(wǎng)的連接協(xié)議,采用輕量級(jí)發(fā)布和訂閱消息傳輸機(jī)制。

?大家可以直接上GitHub下載MQQT服務(wù)的源碼,源碼地址:https://github.com/mqtt/mqtt.github.io/wiki/libraries

主要內(nèi)容:

官方文檔翻譯:

M2Mqtt庫(kù)提供了一個(gè)主類(lèi)MqttClient,代表連接到代理的MQTT客戶端。您可以連接到提供其IP地址或主機(jī)名的代理,以及可選的與MQTT協(xié)議相關(guān)的一些參數(shù)。

連接到代理后,您可以使用Publish()方法向主題和Subscribe()方法發(fā)布消息以訂閱主題并接收其上發(fā)布的消息。

MqttClient類(lèi)是基于事件,以便您在郵件發(fā)布到您訂閱的主題時(shí)收到一個(gè)事件。消息發(fā)布完成后,您可以收到事件,您已訂閱或取消訂閱主題。

以客戶端為主題的例子:

...?

// create client instance?
MqttClient client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS));?

// 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_EXACTLY_ONCE });?

...?

static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)?
{?
// handle message received?
}

一般C#客戶端都是應(yīng)用單例模式,下面的是我封裝的類(lèi):

public class MqttClientService

? ? {


? ? ? ? private static volatile MqttClientService _instance = null;


? ? ? ? private static readonly object LockHelper = new object();


? ? ? ? /// <summary>

? ? ? ? /// 創(chuàng)建單例模式

? ? ? ? /// </summary>

? ? ? ? /// <param name="ipAddress"></param>

? ? ? ? /// <returns></returns>

? ? ? ? public static MqttClientService CreateInstance(string ipAddress)

? ? ? ? {

? ? ? ? ? ? if (_instance == null)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? lock (LockHelper)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? if (_instance == null)

? ? ? ? ? ? ? ? ? ? ? ? _instance = new MqttClientService(ipAddress);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? return _instance;

? ? ? ? }


? ? ? ? /// <summary>

? ? ? ? /// 實(shí)例化訂閱客戶端

? ? ? ? /// </summary>

? ? ? ? public MqttClient SubscribeClient { get; set; }



? ? ? ? public Action<Object, MqttMsgPublishEventArgs> ClientPublishReceivedAction { get; set; }


? ? ? ? public MqttClientService(string ipAddress)

? ? ? ? {

? ? ? ? ? ? // create client instance?

? ? ? ? ? ? SubscribeClient = new MqttClient(IPAddress.Parse(ipAddress));


? ? ? ? ? ? // register to message received?

? ? ? ? ? ? SubscribeClient.MqttMsgPublishReceived += client_MqttMsgPublishReceived;


? ? ? ? ? ? string clientId = Guid.NewGuid().ToString();


? ? ? ? ? ? SubscribeClient.Connect(clientId);


? ? ? ? ? ? // subscribe to the topic "/home/temperature" with QoS 2?

? ? ? ? ? ? SubscribeClient.Subscribe(new string[] { "avatar/uploaded" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });

? ? ? ? }


? ? ? ? void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)

? ? ? ? {

? ? ? ? ? ? // handle message received?

? ? ? ? ? ? ClientPublishReceivedAction.Invoke(sender, e);

? ? ? ? }


? ? ? ? public void client_MqttMsgPublish(string publishString)

? ? ? ? {

? ? ? ? ? ? SubscribeClient.Publish("avatar/signed", Encoding.UTF8.GetBytes(publishString), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);

? ? ? ? }

? ? }

用戶只需要把訂閱的路徑寫(xiě)到Subscribe即可。

相關(guān)文章:

  • 使用 MQTTnet 快速實(shí)現(xiàn) MQTT 通信

原文地址:?https://www.cnblogs.com/dongqinnanren/p/6839319.html


.NET社區(qū)新聞,深度好文,歡迎訪問(wèn)公眾號(hào)文章匯總 http://www.csharpkit.com

總結(jié)

以上是生活随笔為你收集整理的C# 接受MQTT服务器推送的消息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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