• 大小: 2.19MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-08-13
  • 语言: C#
  • 标签: MQTT  物联网  net  

资源简介

此Demo对应本人博客文章《MQTT(一)C#使用 MQTTnet 快速实现 MQTT 通信》 开发环境Win7 + vs2017

资源截图

代码片段和文件信息

using MQTTnet;
using MQTTnet.Core;
using MQTTnet.Core.Client;
using MQTTnet.Core.Packets;
using MQTTnet.Core.Protocol;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MqttClientWin
{
    public partial class FmMqttClient : Form
    {
        private MqttClient mqttClient = null;

        public FmMqttClient()
        {
            InitializeComponent();

            Task.Run(async () => { await ConnectMqttServerAsync(); });
        }

        /// 
        /// 连接服务器
        /// 

        /// 
        private async Task ConnectMqttServerAsync()
        {
            if (mqttClient == null)
            {
                mqttClient = new MqttClientFactory().CreateMqttClient() as MqttClient;
                mqttClient.ApplicationMessageReceived += MqttClient_ApplicationMessageReceived;
                mqttClient.Connected += MqttClient_Connected;
                mqttClient.Disconnected += MqttClient_Disconnected;

            }

            try
            {
                var options = new MqttClientTcpOptions
                {
                    Server = “127.0.0.1“
                    //Server = “172.16.30.77“
                    ClientId = Guid.NewGuid().ToString().Substring(0 5)
                    UserName = “u001“
                    Password = “p001“
                    CleanSession = true
                };

                await mqttClient.ConnectAsync(options);
             
            }
            catch (Exception ex)
            {
                Invoke((new Action(() =>
                {
                    txtReceiveMessage.AppendText($“连接到MQTT服务器失败!“ + Environment.NewLine + ex.Message + Environment.NewLine);
                })));
            }
        }

        /// 
        /// 服务器连接成功
        /// 

        /// 
        /// 
        private void MqttClient_Connected(object sender EventArgs e)
        {
            Invoke((new Action(() =>
            {
                txtReceiveMessage.AppendText(“已连接到MQTT服务器!“ + Environment.NewLine);
            })));
        }

        /// 
        /// 断开服务器连接
        /// 

        /// 
        /// 
        private void MqttClient_Disconnected(object sender EventArgs e)
        {
            Invoke((new Action(() =>
            {
                txtReceiveMessage.AppendText(“已断开MQTT连接!“ + Environment.NewLine);
            })));
        }

        /// 
        /// 接收到消息
        /// 

        /// 
        /// 
        private void MqttClient_ApplicationMessageReceived(object sender MqttApplicationMessageReceivedEventArgs e)
        {
            Invoke((new Actio

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

     文件     429020  2019-03-14 10:11  MqttTest\.vs\MqttTest\DesignTimeBuild\.dtbcache

    ..A..H.    130560  2019-03-14 10:11  MqttTest\.vs\MqttTest\v15\.suo

     文件          0  2018-02-03 08:30  MqttTest\.vs\MqttTest\v15\Server\sqlite3\db.lock

     文件    3420160  2018-03-29 17:34  MqttTest\.vs\MqttTest\v15\Server\sqlite3\storage.ide

     文件      32768  2019-03-14 10:07  MqttTest\.vs\MqttTest\v15\Server\sqlite3\storage.ide-shm

     文件    3184792  2019-03-14 10:07  MqttTest\.vs\MqttTest\v15\Server\sqlite3\storage.ide-wal

     文件        562  2018-03-12 15:22  MqttTest\MqttClientWin\App.config

     文件     728576  2016-12-15 06:16  MqttTest\MqttClientWin\bin\Debug\Apache.NMS.ActiveMQ.dll

     文件    3550720  2016-12-15 06:16  MqttTest\MqttClientWin\bin\Debug\Apache.NMS.ActiveMQ.pdb

     文件     590688  2016-12-15 06:16  MqttTest\MqttClientWin\bin\Debug\Apache.NMS.ActiveMQ.xml

     文件      65024  2015-10-17 11:31  MqttTest\MqttClientWin\bin\Debug\Apache.NMS.dll

     文件     196096  2015-10-17 11:31  MqttTest\MqttClientWin\bin\Debug\Apache.NMS.pdb

     文件     145105  2015-10-17 11:31  MqttTest\MqttClientWin\bin\Debug\Apache.NMS.xml

     文件      22768  2016-11-05 04:55  MqttTest\MqttClientWin\bin\Debug\Microsoft.Win32.Primitives.dll

     文件      14848  2019-03-14 10:11  MqttTest\MqttClientWin\bin\Debug\MqttClientWin.exe

     文件        562  2018-03-12 15:22  MqttTest\MqttClientWin\bin\Debug\MqttClientWin.exe.config

     文件      28160  2019-03-14 10:11  MqttTest\MqttClientWin\bin\Debug\MqttClientWin.pdb

     文件      90112  2017-10-09 19:47  MqttTest\MqttClientWin\bin\Debug\MQTTnet.Core.dll

     文件      20480  2017-10-09 19:47  MqttTest\MqttClientWin\bin\Debug\MQTTnet.dll

     文件      20168  2016-11-05 04:55  MqttTest\MqttClientWin\bin\Debug\System.AppContext.dll

     文件      34496  2016-11-05 04:55  MqttTest\MqttClientWin\bin\Debug\System.Console.dll

     文件      35760  2016-11-05 04:55  MqttTest\MqttClientWin\bin\Debug\System.Diagnostics.DiagnosticSource.dll

     文件      29658  2016-11-05 04:55  MqttTest\MqttClientWin\bin\Debug\System.Diagnostics.DiagnosticSource.xml

     文件      23296  2016-11-05 04:56  MqttTest\MqttClientWin\bin\Debug\System.Globalization.Calendars.dll

     文件     111984  2016-11-05 04:56  MqttTest\MqttClientWin\bin\Debug\System.IO.Compression.dll

     文件      24824  2016-11-05 04:56  MqttTest\MqttClientWin\bin\Debug\System.IO.Compression.ZipFile.dll

     文件      22736  2016-11-05 04:56  MqttTest\MqttClientWin\bin\Debug\System.IO.FileSystem.dll

     文件      22784  2016-11-05 04:56  MqttTest\MqttClientWin\bin\Debug\System.IO.FileSystem.Primitives.dll

     文件     265048  2016-11-05 04:56  MqttTest\MqttClientWin\bin\Debug\System.Net.Http.dll

     文件      22888  2017-09-05 14:54  MqttTest\MqttClientWin\bin\Debug\System.Net.Security.dll

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

评论

共有 条评论