【实验四】C# Winform客户端实现M2Mqtt连接Apollo MQTT服务器

环境以及库的准备

我参考的是这个:https://blog.csdn.net/qq_37258787/article/details/80183923

win10,VS2105

代码

看了很多博客,试了代码,连不上,后来找了帖子,这位要知识费,鄙人较穷,但是他给出了其他挺系统的学习,有兴趣可以看看。最后,谢此大佬:https://www.itsvse.com/thread-4724-1-1.html,虽然对比了下之前无法实现连接的博文(apollo实现c#与android消息推送(三)) ,只是new的方式不同呢

最后给出自己基于博文做出的界面吧
【实验四】C# Winform客户端实现M2Mqtt连接Apollo MQTT服务器_第1张图片


完整可运行代码 :https://download.csdn.net/download/tai4lin/11157668 

实验文档:https://download.csdn.net/download/tai4lin/11179538


190612更新

发现这两天突然太多人下载导致现在积分有点太高um,po出下面代码吧,实现步骤以后有空再写了

        /// 
        /// 连接apache apollo
        /// 
        private void LinkClick(object sender, EventArgs e)
        {
            string clientId = "";
            string ip = "";
            string port = "";
            if (String.IsNullOrEmpty(textBoxCT.Text))
            {
                MessageBox.Show("请输入客户机标识!");
                return;
            }
            if (String.IsNullOrEmpty(textBoxAD.Text) && textBoxAD.Text.IndexOf(':') <= 0)
            {
                MessageBox.Show("请输入IP地址且带端口号!");
                return;
            }
            //生成客户端ID并连接服务器
            clientId = Guid.NewGuid().ToString();
            textBoxCT.Text = clientId;
            ip = textBoxAD.Text.Substring(0, textBoxAD.Text.IndexOf(':'));
            port = textBoxAD.Text.Substring(textBoxAD.Text.IndexOf(':') + 1);

            try
            {

                //创建客户端实例
                client = new MqttClient(ip, Convert.ToInt32(port),
                                               false,// 开启TLS
                                               MqttSslProtocols.TLSv1_0, // TLS版本
                                               null,
                                               null
                                              );
                //client = new MqttClient(IPAddress.Parse(ip), Convert.ToInt32(port), false, null);
                client.ProtocolVersion = MqttProtocolVersion.Version_3_1;
                // admin和password是之前在apache apollo中设置的用户名和密码
                byte code = client.Connect(clientId,
                                        "admin",
                                        "password"); // keepAlivePeriod
                //client.Connect(clientId, "admin", "password", false, 0x01, false, null, null, true, 60);
                buttonLink.Enabled = false;
                btnLose.Enabled = true;
                textBoxLS.ForeColor = Color.LimeGreen;
                textBoxLS.Text = "已连接";
            }
            catch (Exception ee)
            {
                MessageBox.Show("无法连接,请确定代理服务器是否启动,IP端口是否正确");
            }

        }

 

你可能感兴趣的:(嵌入式开发,树莓派,C#,WinForm)