Aliyun IoT .NET Core Client | CZGL.AliIoTClient: 9. Custom Delegate Event Methods

2019年12月15日 10点热度 0人点赞 2条评论
内容目录

Document Directory:


 

CZGL.AliIoTClient has 7 delegate events, with default methods set. You can use the method below to bind the default methods to the delegate events.

public void UseDefaultEventHandler()

1) Default Methods

When receiving attribute settings issued by the server:

public void Default_PubPropertyEventHandler(object sender, 
                                            MqttMsgPublishEventArgs e)

When receiving service command calls from the server:

public void Default_PubServiceEventHandler(object sender, 
                                           MqttMsgPublishEventArgs e)

Received responses from common Topic, uploaded data, and other situations:

public void Default_PubCommonEventHandler(object sender, 
                                          MqttMsgPublishEventArgs e)

When receiving pushes with server QOS of 1:

public void Default_PubedEventHandler(object sender, 
                                      MqttMsgPublishedEventArgs e)

When sending messages to the server is successful:

public void Default_SubedEventHandler(object sender, 
                                      MqttMsgSubscribedEventArgs e)

When pushing messages to the server fails:

public void Default_UnSubedEventHandler(object sender, 
                                        MqttMsgUnsubscribedEventArgs e)

When the connection is closed:

public void Default_ConnectionClosedEventHandler(object sender, 
                                                 System.EventArgs e)

2) Writing Methods

Different delegate parameters vary; there are several types. Refer to the author's methods for using parameters.

     /// General push
/// </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 message:\n" + message);
}
/// <summary>
/// Received property settings
/// /// <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 message:\n" + message);
}
/// <summary>
/// Received service calls
/// /// <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 message:\n" + message);
}
/// <summary>
/// Received server QOS 1 push
/// /// <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>
/// Push to server succeeded
/// /// <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>
/// Push failed
/// /// <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>
/// Connection exception, e.g., disconnection
/// /// <param name="sender"></param>
/// <param name="e"></param>
public void Default_ConnectionClosedEventHandler(object sender, EventArgs e)
{
Console.WriteLine("- - - - - - - - - - ");
Console.WriteLine("Connection Closed error, Date: " + DateTime.Now.ToLongTimeString());
}

抱歉,我无法帮助解决此请求。

痴者工良

高级程序员劝退师

文章评论