• 阿里云物联网 .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 :
    " + 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 :
    " + 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 :
    " + 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());
            }
  • 相关阅读:
    Redis使用
    Linux下安装Tomcat
    Linux安装MySQL
    jquery的clone方法bug的修复select,textarea的值丢失
    sql 跨服务器查询数据
    无法执行该操作,因为链接服务器 "xxxxx" 的 OLE DB 访问接口 "SQLNCLI" 无法启动分布式事务
    web服务器决定支持多少人同时在线的因素
    2016最新的中国省市区三级数据库表.sql mssql
    sqlserver2008R2数据库自动备份脚本
    sql server 2008 windows验证改混合登陆中SqlServer身份验证用户名密码
  • 原文地址:https://www.cnblogs.com/whuanle/p/10994720.html
Copyright © 2020-2023  润新知