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服务器推送的消息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软发布Azure Pipelines,
- 下一篇: 《C# 程序员的自我修养》送书活动结果公