diff --git a/JY.Inspection/Common/MqttSingleton.cs b/JY.Inspection/Common/MqttSingleton.cs index 4a74686..0e658fb 100644 --- a/JY.Inspection/Common/MqttSingleton.cs +++ b/JY.Inspection/Common/MqttSingleton.cs @@ -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(); diff --git a/JY.Inspection/Frm/FrmTest.Designer.cs b/JY.Inspection/Frm/FrmTest.Designer.cs index e7bd0f6..0a68fce 100644 --- a/JY.Inspection/Frm/FrmTest.Designer.cs +++ b/JY.Inspection/Frm/FrmTest.Designer.cs @@ -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; } } \ No newline at end of file diff --git a/JY.Inspection/Frm/FrmTest.cs b/JY.Inspection/Frm/FrmTest.cs index 63f1e66..0579e31 100644 --- a/JY.Inspection/Frm/FrmTest.cs +++ b/JY.Inspection/Frm/FrmTest.cs @@ -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); + } } }