mqtt单例测试

This commit is contained in:
liming 蔡
2026-07-20 15:14:26 +08:00
parent 40b0df237e
commit b0c47af030
3 changed files with 32 additions and 4 deletions
+3 -3
View File
@@ -5,8 +5,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Protocol;
namespace JY.Inspection.Common
@@ -66,7 +64,9 @@ namespace JY.Inspection.Common
if (useTls)
{
optionsBuilder.WithTls();
optionsBuilder.WithTlsOptions(new MqttClientTlsOptions() {
UseTls = true
});
}
_options = optionsBuilder.Build();
+13
View File
@@ -31,6 +31,7 @@
this.btn_readCollectItemCfg = new MetroFramework.Controls.MetroButton();
this.btn_stationExit = new MetroFramework.Controls.MetroButton();
this.btn_stationArrival = new MetroFramework.Controls.MetroButton();
this.btn_sendMQTTClient = new MetroFramework.Controls.MetroButton();
this.SuspendLayout();
//
// btn_readCollectItemCfg
@@ -63,11 +64,22 @@
this.btn_stationArrival.UseSelectable = true;
this.btn_stationArrival.Click += new System.EventHandler(this.btn_stationArrival_Click_1);
//
// btn_sendMQTTClient
//
this.btn_sendMQTTClient.Location = new System.Drawing.Point(87, 229);
this.btn_sendMQTTClient.Name = "btn_sendMQTTClient";
this.btn_sendMQTTClient.Size = new System.Drawing.Size(125, 53);
this.btn_sendMQTTClient.TabIndex = 3;
this.btn_sendMQTTClient.Text = "mqtt客户端发数据";
this.btn_sendMQTTClient.UseSelectable = true;
this.btn_sendMQTTClient.Click += new System.EventHandler(this.btn_sendMQTTClient_Click);
//
// FrmTest
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(998, 586);
this.Controls.Add(this.btn_sendMQTTClient);
this.Controls.Add(this.btn_stationArrival);
this.Controls.Add(this.btn_stationExit);
this.Controls.Add(this.btn_readCollectItemCfg);
@@ -82,5 +94,6 @@
private MetroFramework.Controls.MetroButton btn_readCollectItemCfg;
private MetroFramework.Controls.MetroButton btn_stationExit;
private MetroFramework.Controls.MetroButton btn_stationArrival;
private MetroFramework.Controls.MetroButton btn_sendMQTTClient;
}
}
+16 -1
View File
@@ -1,4 +1,5 @@
using JY.Model.Excel;
using JY.Inspection.Common;
using JY.Model.Excel;
using JY.Utility;
using MetroFramework.Forms;
using System;
@@ -50,5 +51,19 @@ namespace JY.Inspection.Frm
MessageBox.Show(ex.ToString());
}
}
private async void btn_sendMQTTClient_Click(object sender, EventArgs e)
{
await MqttSingleton.Instance.InitializeAsync(
brokerHost: "127.0.0.1",
clientId: "EVE",
username: string.Empty,
password: string.Empty
);
string topic = "EVE/工厂编码/EQP/设备编码";
string payload = "测试数据";
await MqttSingleton.Instance.SendAsync(topic, payload);
}
}
}