CZGL.AliIoTClient 有7個委託事件,設置了默認的方法。 你能夠經過下面的方法使用默認的方法綁定到委託事件中。html
public void UseDefaultEventHandler()
收到服務器下發屬性設置時:node
public void Default_PubPropertyEventHandler(object sender, MqttMsgPublishEventArgs e)
收到服務器調用服務命令時:git
public void Default_PubServiceEventHandler(object sender, MqttMsgPublishEventArgs e)
收到普通Topic、上傳數據的響應等其它狀況:服務器
public void Default_PubCommonEventHandler(object sender, MqttMsgPublishEventArgs e)
收到服務器QOS爲1的推送阿里雲
public void Default_PubedEventHandler(object sender, MqttMsgPublishedEventArgs e)
當向服務器發送消息成功時:spa
public void Default_SubedEventHandler(object sender, MqttMsgSubscribedEventArgs e)
向服務器推送消息失敗時:code
public void Default_UnSubedEventHandler(object sender, MqttMsgUnsubscribedEventArgs e)
鏈接斷開時htm
public void Default_ConnectionClosedEventHandler(object sender, System.EventArgs e)
不一樣的委託參數不一樣,有好幾種類型,參考筆者的方法使用參數。blog
/// 通常的推送 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void Default_PubCommonEventHandler(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); } /// <summary> /// 收到屬性設置 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void Default_PubPropertyEventHandler(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); } /// <summary> /// 收到服務調用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void Default_PubServiceEventHandler(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); } /// <summary> /// 收到服務器QOS爲1的推送 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> 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); } /// <summary> /// 向服務器推送成功 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> 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)); } /// <summary> /// 推送失敗 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void Default_UnSubedEventHandler(object sender, MqttMsgUnsubscribedEventArgs e) { Console.WriteLine("- - - - - - - - - - "); Console.WriteLine("Sub topic error,Date: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("MessageId: " + e.MessageId); } /// <summary> /// 鏈接發生異常,斷網等 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void Default_ConnectionClosedEventHandler(object sender, EventArgs e) { Console.WriteLine("- - - - - - - - - - "); Console.WriteLine("Connect Closed error,Date: " + DateTime.Now.ToLongTimeString()); }