2019-11-06

“D:Program Files\Nox\bin\nox_adb.exe” connect 127.0.0.1:62001



using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using uPLibrary.Networking.M2Mqtt;

using uPLibrary.Networking.M2Mqtt.Messages;

namespace M2Mmqtt

{

    public partial class Form1 : Form

    {

        String enpoint = "192.168.221.35";

        int port = 1883;

        string user = "admin";

        string pwd = "public";

        string clientId = Guid.NewGuid().ToString();

        MqttClient client;

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

        }

        private void btnConnect_Click(object sender, EventArgs e)

        {

            try

            {

                client = new MqttClient(enpoint, port, false, MqttSslProtocols.None, null, null);

                client.Connect(clientId, user, pwd);

                if(client.IsConnected)

                {

                    MessageBox.Show("连接成功!");

                    client.Subscribe(new string[] { "a/b/c" }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });

                    client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;

                }

            }catch(Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

        private void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)

        {

            Action showAct = new Action(ShowMsg);

            Invoke(showAct, new string[] { Encoding.UTF8.GetString(e.Message) });

            //throw new NotImplementedException();

        }

        public void ShowMsg(string msg)

        {

            txtReceiveMsg.AppendText(msg);

        }


    }

}

你可能感兴趣的:(2019-11-06)