CZGL.AliIoTClient 有7個委託事件,設置了默認的方法。
你可以通過下面的方法使用默認的方法綁定到委託事件中。
public void UseDefaultEventHandler()
1)默認的方法
收到服務器下發屬性設置時:
public void Default_PubPropertyEventHandler(object sender,
MqttMsgPublishEventArgs e)
收到服務器調用服務命令時:
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)
當向服務器發送消息成功時:
public void Default_SubedEventHandler(object sender,
MqttMsgSubscribedEventArgs e)
向服務器推送消息失敗時:
public void Default_UnSubedEventHandler(object sender,
MqttMsgUnsubscribedEventArgs e)
連接斷開時
public void Default_ConnectionClosedEventHandler(object sender,
System.EventArgs e)
2)方法的寫法
不同的委託參數不同,有好幾種類型,參考筆者的方法使用參數。
/// 一般的推送
/// </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());
}