C# 接受MQTT服务器推送的消息
前言:
?MQTT是IBM開發(fā)的一個(gè)即時(shí)通訊協(xié)議。MQTT是面向M2M和物聯(lián)網(wǎng)的連接協(xié)議,采用輕量級發(fā)布和訂閱消息傳輸機(jī)制。
?大家可以直接上GitHub下載MQQT服務(wù)的源碼,源碼地址:https://github.com/mqtt/mqtt.github.io/wiki/libraries
主要內(nèi)容:
官方文檔翻譯:
M2Mqtt庫提供了一個(gè)主類MqttClient,代表連接到代理的MQTT客戶端。您可以連接到提供其IP地址或主機(jī)名的代理,以及可選的與MQTT協(xié)議相關(guān)的一些參數(shù)。
連接到代理后,您可以使用Publish()方法向主題和Subscribe()方法發(fā)布消息以訂閱主題并接收其上發(fā)布的消息。
MqttClient類是基于事件,以便您在郵件發(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)用單例模式,下面的是我封裝的類:
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);
? ? ? ? }
? ? }
用戶只需要把訂閱的路徑寫到Subscribe即可。
相關(guān)文章:
使用 MQTTnet 快速實(shí)現(xiàn) MQTT 通信
原文地址:?https://www.cnblogs.com/dongqinnanren/p/6839319.html
.NET社區(qū)新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com
總結(jié)
以上是生活随笔為你收集整理的C# 接受MQTT服务器推送的消息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软发布Azure Pipelines,
- 下一篇: .NET Core开发日志——Linux