• 大小: 1.03MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-08-16
  • 语言: C#
  • 标签: MQTT客户  Apollo  

资源简介

C#语言,编写MQTT客户端,连接到实验3的MQTT服务器上,发布一个主题,并且订阅主题。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;

namespace ApolloClientM2Mqtt
{
    public partial class ClientForm : Form
    {
        private MqttClient client;
        private string topic = ““;

        private delegate void MessDelegate(T obj);

        public ClientForm()
        {
            InitializeComponent();
            textBoxLS.ForeColor = Color.Peru;
            btnLose.Enabled = false;
            btnCa.Enabled = false;
            comboBoxPub.SelectedIndex = 1;
            comboBoxSub.SelectedIndex = 1;
        }

        /// 
        /// 连接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端口是否正确“

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     76288  2019-05-03 17:01  M2MQTT\.vs\M2MQTT\v14\.suo

     文件       4153  2019-05-02 23:17  M2MQTT\ApolloClientM2Mqtt\ApolloClientM2Mqtt.csproj

     文件        187  2019-04-26 14:22  M2MQTT\ApolloClientM2Mqtt\App.config

     文件      16384  2019-05-03 00:33  M2MQTT\ApolloClientM2Mqtt\bin\Debug\ApolloClientM2Mqtt.exe

     文件        187  2019-04-26 14:22  M2MQTT\ApolloClientM2Mqtt\bin\Debug\ApolloClientM2Mqtt.exe.config

     文件      24064  2019-05-03 00:33  M2MQTT\ApolloClientM2Mqtt\bin\Debug\ApolloClientM2Mqtt.pdb

     文件      22696  2019-05-03 16:57  M2MQTT\ApolloClientM2Mqtt\bin\Debug\ApolloClientM2Mqtt.vshost.exe

     文件        187  2019-04-26 14:22  M2MQTT\ApolloClientM2Mqtt\bin\Debug\ApolloClientM2Mqtt.vshost.exe.config

     文件        490  2018-04-12 07:35  M2MQTT\ApolloClientM2Mqtt\bin\Debug\ApolloClientM2Mqtt.vshost.exe.manifest

     文件      55808  2019-04-27 18:10  M2MQTT\ApolloClientM2Mqtt\bin\Debug\M2Mqtt.Net.dll

     文件     185856  2019-04-27 18:10  M2MQTT\ApolloClientM2Mqtt\bin\Debug\M2Mqtt.Net.pdb

     文件       8567  2019-05-03 00:33  M2MQTT\ApolloClientM2Mqtt\ClientForm.cs

     文件      14606  2019-05-03 00:33  M2MQTT\ApolloClientM2Mqtt\ClientForm.Designer.cs

     文件       5817  2019-05-03 00:33  M2MQTT\ApolloClientM2Mqtt\ClientForm.resx

     文件        180  2019-05-03 00:33  M2MQTT\ApolloClientM2Mqtt\obj\Debug\ApolloClientM2Mqtt.ClientForm.resources

     文件        820  2019-05-03 16:57  M2MQTT\ApolloClientM2Mqtt\obj\Debug\ApolloClientM2Mqtt.csproj.FileListAbsolute.txt

     文件       1017  2019-05-03 00:33  M2MQTT\ApolloClientM2Mqtt\obj\Debug\ApolloClientM2Mqtt.csproj.GenerateResource.Cache

     文件       8894  2019-05-02 23:17  M2MQTT\ApolloClientM2Mqtt\obj\Debug\ApolloClientM2Mqtt.csprojResolveAssemblyReference.cache

     文件      16384  2019-05-03 00:33  M2MQTT\ApolloClientM2Mqtt\obj\Debug\ApolloClientM2Mqtt.exe

     文件      24064  2019-05-03 00:33  M2MQTT\ApolloClientM2Mqtt\obj\Debug\ApolloClientM2Mqtt.pdb

     文件        180  2019-05-02 23:17  M2MQTT\ApolloClientM2Mqtt\obj\Debug\ApolloClientM2Mqtt.Properties.Resources.resources

     文件       1452  2019-04-26 15:26  M2MQTT\ApolloClientM2Mqtt\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7185  2019-05-02 23:09  M2MQTT\ApolloClientM2Mqtt\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2019-04-26 14:22  M2MQTT\ApolloClientM2Mqtt\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2019-04-26 14:22  M2MQTT\ApolloClientM2Mqtt\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2019-04-26 14:22  M2MQTT\ApolloClientM2Mqtt\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件        135  2019-04-26 14:22  M2MQTT\ApolloClientM2Mqtt\packages.config

     文件        743  2019-05-03 17:01  M2MQTT\ApolloClientM2Mqtt\Program.cs

     文件       1344  2019-04-26 14:22  M2MQTT\ApolloClientM2Mqtt\Properties\AssemblyInfo.cs

     文件       2849  2019-04-26 14:22  M2MQTT\ApolloClientM2Mqtt\Properties\Resources.Designer.cs

............此处省略164个文件信息

评论

共有 条评论