阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:9. 自定义委托事件方法

内容纲要

文档目录:


 

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());
        }
点赞
  1. generic viagra说道:

    Thank you for every one of your hard work on this web page. My daughter really loves making time for investigations and it's really obvious why. Most people learn all of the lively mode you offer priceless tactics through this web site and even inspire participation from some other people on the matter so our favorite girl is now starting to learn a lot of things. Take advantage of the rest of the year. You are always doing a remarkable job.

  2. I am glad for writing to make you know what a magnificent encounter my wife's girl had studying your web site. She mastered several pieces, which include what it's like to possess an awesome helping nature to let other individuals very easily know precisely some multifaceted subject matter. You truly exceeded people's expectations. Thank you for presenting these powerful, trusted, informative and in addition fun tips about this topic to Kate.

发表评论

邮箱地址不会被公开。 必填项已用*标注

You must enable javascript to see captcha here!