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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發布時間:2023/12/4 C# 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 接受MQTT服务器推送的消息 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言:

?MQTT是IBM開發的一個即時通訊協議。MQTT是面向M2M和物聯網的連接協議,采用輕量級發布和訂閱消息傳輸機制。

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

主要內容:

官方文檔翻譯:

M2Mqtt庫提供了一個主類MqttClient,代表連接到代理的MQTT客戶端。您可以連接到提供其IP地址或主機名的代理,以及可選的與MQTT協議相關的一些參數。

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

MqttClient類是基于事件,以便您在郵件發布到您訂閱的主題時收到一個事件。消息發布完成后,您可以收到事件,您已訂閱或取消訂閱主題。

以客戶端為主題的例子:

...?

// 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#客戶端都是應用單例模式,下面的是我封裝的類:

public class MqttClientService

? ? {


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


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


? ? ? ? /// <summary>

? ? ? ? /// 創建單例模式

? ? ? ? /// </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>

? ? ? ? /// 實例化訂閱客戶端

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

? ? ? ? }

? ? }

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

相關文章:

  • 使用 MQTTnet 快速實現 MQTT 通信

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


.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。