http://pan.whuanle.cn/index.php?dir=uploads/阿里雲IOT/AliIOTXFclient-dll類庫&responsephp
下載三個庫,頭部引入 便可使用html
using AliIOTXFClient;
示例下載地址服務器
http://pan.whuanle.cn/index.php?dir=uploads/阿里雲IOT/demo示例網絡
本章示例 AliIOTXF.Tweide
目前只設置了五個委託事件post
XFMQTT.ConnectionClosedEventHandler
XFMQTT.PubedEventHandler
XFMQTT.PubEventHandler
XFMQTT.SubedEventHandler
XFMQTT.UnSubedEventHandler
委託 | 說明 |
XFMQTT.PubEventHandler | 訂閱回調 - 當收到服務器消息時 |
XFMQTT.PubedEventHandler | 當 QOS=1或2時,收到訂閱觸發 |
XFMQTT.SubedEventHandler | 向服務器發佈 Topic 時 |
XFMQTT.SubedEventHandler | 向服務器發佈 Topic 失敗時 |
XFMQTT.ConnectionClosedEventHandler | 斷開鏈接 |
MQTT是一種可靠的消息推送協議,QOS保證了消息必須完整地推送給另外一端,關於QOS具體能夠百度~this
表明消息推送地可靠程度,當 QOS = 1是,在網絡波動等狀況下,可能會存在屢次重複的一次推送。阿里雲
// 使用默認的委託事件 client.UseDefaultEventHandler();
經過上一篇文章,程序已經能夠運行、鏈接到阿里雲服務器,而且能夠訂閱發佈消息。spa
這些操做都會觸發事件,經過 UseDefaultEventHandler() 方法,使用的默認的方法綁定到事件。code
刪除 這一行代碼
client.UseDefaultEventHandler();
頭部引入
using uPLibrary.Networking.M2Mqtt.Messages;
訂閱回調 - 當收到服務器消息時觸發
委託方法以下
委託方法(object sender, MqttMsgPublishEventArgs e) { }
MqttMsgPublishEventArgs 是與接收消息有關的對象,其屬性以下:
// message topic private string topic; // message data private byte[] message; // duplicate delivery private bool dupFlag; // quality of service level private byte qosLevel; // retain flag private bool retain;
SDK 默認的委託方法
public void Default_PubEventHandler(object sender, MqttMsgPublishEventArgs e) { // handle message received string topic = e.Topic; string message = Encoding.ASCII.GetString(e.Message); Console.WriteLine("- - - - - - - - - - "); Console.WriteLine("get topic message,Date: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("topic: " + topic); Console.WriteLine("get messgae :\n" + message); }
那麼咱們來寫一個自定義委託方法
裏面的怎麼寫都行,方法名稱隨意
public static void 收到消息(object sender, MqttMsgPublishEventArgs e) { Console.WriteLine("topic名稱:"+e.Topic); Console.WriteLine("接收時間:" + DateTime.Now.ToLongDateString()); Console.WriteLine("消息內容:\n"+Encoding.ASCII.GetString(e.Message)); }
在刪除 client.UseDefaultEventHandler(); 的位置加上
client.PubEventHandler += 收到消息;
當 QOS=1或2時,收到訂閱觸發
原型
委託方法(object sender, MqttMsgPublishedEventArgs e) { }
MqttMsgPublishedEventArgs 屬性
// message identifier ushort messageId; // published flag bool isPublished;
SDK 默認方法
public void Default_PubedEventHandler(object sender, MqttMsgPublishedEventArgs e) { Console.WriteLine("- - - - - - - - - - "); Console.WriteLine("published,Date: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("MessageId: " + e.MessageId + " Is Published: " + e.IsPublished); }
咱們來自定義寫,方法名稱隨意
public static void 重複收到消息(object sender, MqttMsgPublishedEventArgs e) { Console.WriteLine("接收時間: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("消息id: " + e.MessageId + " Is Published: " + e.IsPublished); }
加上
client.PubedEventHandler += 重複收到消息;
向服務器發佈 Topic 時觸發
原型
void 發佈消息時(object sender, MqttMsgSubscribedEventArgs e)
MqttMsgSubscribedEventArgs 屬性
/// <summary> /// Message identifier /// </summary> public ushort MessageId {get;set;} /// <summary> /// List of granted QOS Levels /// </summary> public byte[] GrantedQoSLevels{get;set;} // message identifier ushort messageId; // granted QOS levels byte[] grantedQosLevels;
SDK 默認寫法
public void Default_SubedEventHandler(object sender, MqttMsgSubscribedEventArgs e) { Console.WriteLine("- - - - - - - - - - "); Console.WriteLine("Sub topic,Date: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("MessageId: " + e.MessageId); Console.WriteLine("List of granted QOS Levels: " + Encoding.UTF8.GetString(e.GrantedQoSLevels)); }
咱們來自定義寫,方法名稱隨意
public static void 發佈消息時(object sender, MqttMsgSubscribedEventArgs e) { Console.WriteLine("向服務器發送了消息"); Console.WriteLine("發送時間: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("消息id: " + e.MessageId); Console.WriteLine("QOS 爲 : " + Encoding.UTF8.GetString(e.GrantedQoSLevels)); }
加上
client.SubedEventHandler += 發佈消息時;
向服務器發佈 Topic 失敗時觸發
void 發送失敗(object sender, MqttMsgUnsubscribedEventArgs ) { }
MqttMsgUnsubscribedEventArgs 屬性
/// <summary> /// Message identifier /// </summary> public ushort MessageId { get { return this.messageId; } internal set { this.messageId = value; } }
SDK 寫法
public void Default_UnSubedEventHandler(object sender, MqttMsgUnsubscribedEventArgs e) { Console.WriteLine("- - - - - - - - - - "); Console.WriteLine("Sub topic error,Date: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("MessageId: " + e.MessageId); }
咱們自定義寫
public static void 發送失敗(object sender, MqttMsgUnsubscribedEventArgs e) { Console.WriteLine("消息發送失敗 "); Console.WriteLine("時間: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("失敗的消息 MessageId: " + e.MessageId); }
加上
client.UnSubedEventHandler += 發送失敗;
斷開鏈接時觸發
SDK 寫法
public void Default_ConnectionClosedEventHandler(object sender, EventArgs e) { Console.WriteLine("- - - - - - - - - - "); Console.WriteLine("Connect Closed error,Date: " + DateTime.Now.ToLongTimeString()); }
自定義寫
public static void 斷開鏈接(object sender, EventArgs e) { Console.WriteLine("日。。。鏈接斷開了: " + DateTime.Now.ToLongTimeString()); }
加上
client.ConnectionClosedEventHandler += 斷開鏈接;
完整代碼
static void Main(string[] args) { // 建立鏈接對象 XFMQTT client = new XFMQTT("a1BiPoNawLI", "Raspberry"); // 初始化客戶端配置 client.Init("2NOaBeqXcIzLQEhlJFEfKbWeug0o3m0c", "cn-shanghai"); // 要訂閱的Topic string[] topic = { client.CombineHeadTopic("PubData") }; // 使用自定義的委託事件 client.PubEventHandler += 收到消息; client.PubedEventHandler += 重複收到消息; client.SubedEventHandler += 發佈消息時; client.UnSubedEventHandler += 發送失敗; client.ConnectionClosedEventHandler += 斷開鏈接; // 鏈接服務器 client.ConnectMqtt(topic); while (true) { string str = Console.ReadLine(); // 推送內容到特定的 Topic client.Subscribe(client.CombineHeadTopic("SubData"), str); } Console.ReadKey(); } public static void 收到消息(object sender, MqttMsgPublishEventArgs e) { Console.WriteLine("topic名稱:"+e.Topic); Console.WriteLine("接收時間:" + DateTime.Now.ToLongDateString()); Console.WriteLine("消息內容:\n"+Encoding.ASCII.GetString(e.Message)); } public static void 重複收到消息(object sender, MqttMsgPublishedEventArgs e) { Console.WriteLine("接收時間: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("消息id: " + e.MessageId + " Is Published: " + e.IsPublished); } public static void 發佈消息時(object sender, MqttMsgSubscribedEventArgs e) { Console.WriteLine("向服務器發送了消息"); Console.WriteLine("發送時間: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("消息id: " + e.MessageId); Console.WriteLine("QOS 爲 : " + Encoding.UTF8.GetString(e.GrantedQoSLevels)); } public static void 發送失敗(object sender, MqttMsgUnsubscribedEventArgs e) { Console.WriteLine("消息發送失敗 "); Console.WriteLine("時間: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("失敗的消息 MessageId: " + e.MessageId); } public static void 斷開鏈接(object sender, EventArgs e) { Console.WriteLine("日。。。鏈接斷開了: " + DateTime.Now.ToLongTimeString()); }
可是這樣子很差,換種寫法
class Program { static void Main(string[] args) { // 建立鏈接對象 XFMQTT client = new XFMQTT("a1BiPoNawLI", "Raspberry"); // 初始化客戶端配置 client.Init("2NOaBeqXcIzLQEhlJFEfKbWeug0o3m0c", "cn-shanghai"); // 要訂閱的Topic string[] topic = { client.CombineHeadTopic("PubData") }; // 使用自定義的委託事件 事件類 事件 = new 事件類(); client.PubEventHandler += 事件.收到消息; client.PubedEventHandler += 事件.重複收到消息; client.SubedEventHandler += 事件.發佈消息時; client.UnSubedEventHandler += 事件.發送失敗; client.ConnectionClosedEventHandler += 事件.斷開鏈接; // 鏈接服務器 client.ConnectMqtt(topic); while (true) { string str = Console.ReadLine(); // 推送內容到特定的 Topic client.Subscribe(client.CombineHeadTopic("SubData"), str); } Console.ReadKey(); } } public class 事件類 { public void 收到消息(object sender, MqttMsgPublishEventArgs e) { Console.WriteLine("topic名稱:" + e.Topic); Console.WriteLine("接收時間:" + DateTime.Now.ToLongDateString()); Console.WriteLine("消息內容:\n" + Encoding.ASCII.GetString(e.Message)); } public void 重複收到消息(object sender, MqttMsgPublishedEventArgs e) { Console.WriteLine("接收時間: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("消息id: " + e.MessageId + " Is Published: " + e.IsPublished); } public void 發佈消息時(object sender, MqttMsgSubscribedEventArgs e) { Console.WriteLine("向服務器發送了消息"); Console.WriteLine("發送時間: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("消息id: " + e.MessageId); Console.WriteLine("QOS 爲 : " + Encoding.UTF8.GetString(e.GrantedQoSLevels)); } public void 發送失敗(object sender, MqttMsgUnsubscribedEventArgs e) { Console.WriteLine("消息發送失敗 "); Console.WriteLine("時間: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("失敗的消息 MessageId: " + e.MessageId); } public void 斷開鏈接(object sender, EventArgs e) { Console.WriteLine("日。。。鏈接斷開了: " + DateTime.Now.ToLongTimeString()); } }
使用默認事件方法與自定義事件方法並不衝突。
// 使用默認事件方法 client.UseDefaultEventHandler(); // 使用自定義的委託事件 事件類 事件 = new 事件類(); client.PubEventHandler += 事件.收到消息; client.PubedEventHandler += 事件.重複收到消息; client.SubedEventHandler += 事件.發佈消息時; client.UnSubedEventHandler += 事件.發送失敗; client.ConnectionClosedEventHandler += 事件.斷開鏈接;
爲便於理解,上面事件方法使用了中文名稱,請根據實際修改。