阅读 150

MQTT上传

实现MQTT连接传输数据到服务端

这个网址解释MQTT通俗易懂 可以先看一下理论

首先引用MQTTnet包

我是建立一个winform窗体做的

贴代码

   public partial class Form1 : Form
    {
        public static MqttClient mqttClient = null;
        private static IMqttClientOptions options = null;
        private static MqttApplicationMessageBuilder mamb = new MqttApplicationMessageBuilder();
        /// 
        /// 保留
        /// 
        private static bool Retained = false;
        /// 
        /// 服务质量
        /// 0 - 至多一次
        /// 1 - 至少一次
        /// 2 - 刚好一次
        /// 
        private static int QualityOfServiceLevel = 0;

        public Form1()
        {
            InitializeComponent();
        }


        private void BtnPublish_Click(object sender, EventArgs e)
        {
            TCYBpublish(this.txtcontent.Text, this.txtPubTopic.Text);
        }

        /// 
        /// 太仓雅本mqtt上传
        /// 
        /// 
        /// 
        /// 
        public async Task TCYBpublish(string message, string topic)
        {
            try
            {
                try
                {
                    if (mqttClient == null)
                    {
                        mqttClient = new MqttFactory().CreateMqttClient() as MqttClient;
                    }

                    if (options == null)
                    {
                        options = new MqttClientOptionsBuilder()
                            //mqtt地址 端口
                             .WithTcpServer("xx.xxx.xx.xxx", 1883)
                             //用户名 密码
                             .WithCredentials("xxx", "xxx")
                             //有的服务端要求具体的clientid 有的随机
                             .WithClientId(Guid.NewGuid().ToString().Substring(0, 5))
                             .Build();
                        await mqttClient.ConnectAsync(options);
                    }
                    //mqttClient.ApplicationMessageReceivedHandler = new MQTTMessageHandler();
                    mqttClient.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(new Func(Disconnected));
                    Console.WriteLine("mqtt连接成功");
                }
                catch (Exception e)
                {
                    Console.WriteLine("mqtt连接失败");
                }
                MqttApplicationMessageBuilder mamb1 = new MqttApplicationMessageBuilder();
                mamb1.WithTopic(topic).WithPayload(message).WithRetainFlag(Retained);
                if (QualityOfServiceLevel == 0)
                {
                    mamb1 = mamb1.WithAtMostOnceQoS();
                }
                else if (QualityOfServiceLevel == 1)
                {
                    mamb1 = mamb1.WithAtLeastOnceQoS();
                }
                else if (QualityOfServiceLevel == 2)
                {
                    mamb1 = mamb1.WithExactlyOnceQoS();
                }
                await mqttClient.PublishAsync(mamb1.Build());
                lbllog.Text =DateTime.Now+ "发布成功";

            }
            catch (Exception e)
            {
            }
        }

        private async Task Disconnected(MqttClientDisconnectedEventArgs e)
        {
            try
            {
                await Task.Delay(TimeSpan.FromSeconds(5));
                await mqttClient.ConnectAsync(options);
            }
            catch (Exception exp)
            {
            }
        }
    }

最终实现效果

原文:https://www.cnblogs.com/yunnn/p/15177087.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐