内容目录
文档目录:
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());
}
文章评论