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

开发者社区> IoT> 正文
登录阅读全文

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

简介: CZGL.AliIoTClient 有7个委托事件,设置了默认的方法。你可以通过下面的方法使用默认的方法绑定到委托事件中。 public void UseDefaultEventHandler() 1)默认的方法 收到服务器下发属性设置时: public void Default_PubPrope...

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());
        }

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
IoT
使用钉钉扫一扫加入圈子
+ 订阅

物联网软硬件开发者一站式基地

其他文章