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

namespace Sample_MQTT_Client
{
    public partial class Form1 : Form
    {
        IMqttClient mqttClient;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ReceiveData();
        }

        public async void ReceiveData() 
        {
            //不等空,断开连接
            if (mqttClient == null) {
                var factory = new MqttFactory();
                mqttClient = factory.CreateMqttClient();
                var options = new MqttClientOptionsBuilder()
                    .WithTcpServer("192.168.240.53", 1883)
                    .Build();

                await mqttClient.ConnectAsync(options);

                var subOption = new MqttClientSubscribeOptions()
                {
                    TopicFilters = new List<MqttTopicFilter> 
                    { 
                        new MqttTopicFilterBuilder()
                        .WithTopic("ShuttleLoom")
                        .WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce)
                        .Build()
                    }
                };

                await mqttClient.SubscribeAsync(subOption);
                mqttClient.ApplicationMessageReceivedAsync += MqttClient_ApplicationMessageReceivedAsync;
            }
            else {
                await mqttClient.DisconnectAsync();
            }
        }

        private Task MqttClient_ApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs arg)
        {
            try
            {
                string data = arg.ApplicationMessage.ConvertPayloadToString();
                string strdata = "\n获得数据:" + "\n" + data + "\n";

                //this.txtData.Text = strdata;

                this.txtData.Invoke(
                               new Action(() =>
                               {
                                   this.txtData.Text += strdata;
                               }));
            }
            catch (Exception)
            {

            }

            return null;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
    }
}

 

posted @ 2025-03-10 10:37 chengeng 阅读(10) 评论(0) 推荐(0) 编辑
摘要: AtomicReference<Boolean> isSyncSuccess = new AtomicReference<>(false);CompletableFuture<Void> future = CompletableFuture.runAsync(() ->{ isSyncSuccess 阅读全文
posted @ 2024-11-15 14:07 chengeng 阅读(3) 评论(0) 推荐(0) 编辑
摘要: https://vip.kingdee.com/article/537953720802328576?productLineId=1&lang=zh-CN 阅读全文
posted @ 2024-09-13 17:35 chengeng 阅读(10) 评论(0) 推荐(0) 编辑
摘要: 检测:Get-WindowsOptionalFeature -Online -FeatureName SMB1Protocol启用:Enable-WindowsOptionalFeature -Online -FeatureName SMB1Protocol禁用:Disable-WindowsOpt 阅读全文
posted @ 2024-09-10 22:16 chengeng 阅读(43) 评论(0) 推荐(0) 编辑
摘要: 打开PowerShell(以管理员身份运行)。 输入 irm massgrave.dev/get |iex 自动下载激活脚本 阅读全文
posted @ 2024-06-28 14:25 chengeng 阅读(46) 评论(0) 推荐(0) 编辑
摘要: /// <summary> /// 将一个类型对象数据赋值到另一个类型对象(名字相同的情况) /// </summary> /// <typeparam name="T">目标类型</typeparam> /// <param name="entity">目标类型对象</param> /// <pa 阅读全文
posted @ 2024-04-28 15:26 chengeng 阅读(429) 评论(0) 推荐(0) 编辑
摘要: public static int Update(string strJson){ int count = 0; List<Dictionary<string, object>> listRows = JsonConvert.DeserializeObject<List<Dictionary<str 阅读全文
posted @ 2024-04-26 08:09 chengeng 阅读(27) 评论(0) 推荐(0) 编辑
摘要: private void FormMain_Load(object sender, EventArgs e) { ShareDirec(); } public void ShareDirec() { ManagementClass management =new ManagementClass("W 阅读全文
posted @ 2024-03-24 13:58 chengeng 阅读(13) 评论(0) 推荐(0) 编辑
摘要: 现象 FastReport 打印弹框,无法关闭 解决方法 阅读全文
posted @ 2024-03-19 17:18 chengeng 阅读(67) 评论(0) 推荐(0) 编辑
摘要: #region 屏幕保护 System.Timers.Timer timerRunScreenSaverTask = null; F_ScreenSaver f_ScreenSaver = null; private void RunScreenSaverTask() { if (timerRunS 阅读全文
posted @ 2024-03-18 10:50 chengeng 阅读(59) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示