嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
比较实用的客户端测试工具
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;
using System.Threading;
namespace StdioMQTT
{
public partial class FrmMain : Form
{
private MqttClient mqttClient;
public FrmMain()
{
InitializeComponent();
}
private void FrmMain_Load(object sender, EventArgs e)
{
this.txtClientId.Text = Guid.NewGuid().ToString();
this.txtServer.Text = "3gssubw.mqtt.iot.gz.baidubce.com";
// this.txtServer.Text = "1wizgc5.mqtt.iot.gz.baidubce.com";
txtSubscribe.Text = "/client/pub/42012";
txtPublishTopic.Text = "/client/pub/42012";
}
private void btnConnect_Click(object sender, EventArgs e)
{
if (this.mqttClient != null)
{
List<string> lst = new List<string>();
foreach(SubscribeModel model in this.lstSubscribeTopic.Items)
{
lst.Add(model.Topic);
}
// this.lstSubscribeTopic.Items.Clear();
if (lst.Count > 0)
{
ushort us = this.mqttClient.Unsubscribe(lst.ToArray());
}
this.mqttClient.Disconnect();
}
// this.mqttClient = new MqttClient(this.txtServer.Text);
mqttClient = new MqttClient(this.txtServer.Text,
1884,
true, // 开启TLS
MqttSslProtocols.TLSv1_0, // TLS版本
null,
null
);
mqttClient.ProtocolVersion = MqttProtocolVersion.Version_3_1;
// Console.WriteLine(Guid.NewGuid().ToString());
byte success = mqttClient.Connect(this.txtClientId.Text,
"3gssubw/park",//, "1wizgc5/park",
"jP92bfHnd0LFQ6Ex",// "S7bSWctKFrp1c4K3"
true, // cleanSession
60); // keepAlivePeriod
this.mqttClient.ConnectionClosed = MqttClient_ConnectionClosed;
this.mqttClient.MqttMsgPublished = MqttClient_MqttMsgPublished;
this.mqttClient.MqttMsgPublishReceived = MqttClient_MqttMsgPublishReceived;
this.mqttClient.MqttMsgSubscribed = MqttClient_MqttMsgSubscribed;
this.mqttClient.MqttMsgUnsubscribed = MqttClient_MqttMsgUnsubscribed;
// byte success = this.mqttClient.Connect(this.txtClientId.Text);
if(success != 0 && this.mqttClient.IsConnected)
{
this.MsgBox("连接失败!");
return;
}
this.btnConnect.Text = "断开连接";
}
private void MqttClient_ConnectionClosed(object sender, EventArgs e)
{
// Console.WriteLine("连接关闭!");
// Invoke => { async () => { this.MsgBox("连接关闭!")}};
// ;
this.Invoke(new EventHandler(delegate
{
this.MsgBox("连接关闭!");
}));
}
private void MqttClient_MqttMsgUnsubscribed(object sender, MqttMsgUnsubscribedEventArgs e)
{
Action action = () => {
for(int i = 0; i < this.lstSubscribeTopic.Items.Count; i )
{
SubscribeModel subscribeModel = this.lstSubscribeTopic.Items[i] as SubscribeModel;
if(subscribeModel != null && subscribeModel.UnsubscribeId == e.MessageId)
{
this.lstSubscribeTopic.Items.Remove(subscribeModel);
break;
}
}
};
this.Invoke(action);
}
private void MqttClient_MqttMsgSubscribed(object sender, MqttMsgSubscribedEventArgs e)
{
byte[] bys = e.GrantedQoSLevels;
ushort messageId = e.MessageId;
}
private void MqttClient_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
byte[] bys = e.Message;
string content = Encoding.UTF8.GetString(bys);
string topic = e.Topic;
bool dupFlag = e.DupFlag;
byte qosLevel = e.QosLevel;
bool retain = e.Retain;
Action action = () => {
this.rtbRecive.AppendText($"接收时间:{DateTime.Now}{Environment.NewLine}");
this.rtbRecive.AppendText($"主题:{topic}{Environment.NewLine}");
this.rtbRecive.AppendText($"内容:{content}{Environment.NewLine}");
this.rtbRecive.Focus();
this.rtbRecive.Select(this.rtbRecive.TextLength, 0);
this.rtbRecive.ScrollToCaret();
};
this.Invoke(action);
}
private void MqttClient_MqttMsgPublished(object sender, MqttMsgPublishedEventArgs e)
{
ushort messageId = e.MessageId;
}
private void btnAddSubscribe_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.txtSubscribe.Text))
{
for(int i = 0; i < this.lstSubscribeTopic.Items.Count; i )
{
SubscribeModel subscribeModel = this.lstSubscribeTopic.Items[i] as SubscribeModel;
if(subscribeModel.Topic == this.txtSubscribe.Text)
{
this.MsgBox("主题重复了!");
return;
}
}
if (this.mqttClient != null)
{
SubscribeModel subscribeModel = new SubscribeModel(this.txtSubscribe.Text, MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE);
subscribeModel.Subscribe(this.mqttClient);
this.lstSubscribeTopic.Items.Add(subscribeModel);
}
}
}
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
this.mqttClient?. Disconnect();
this.mqttClient.ConnectionClosed -= this.MqttClient_ConnectionClosed;
}
private void btnSendPublish_Click(object sender, EventArgs e)
{
string topic = this.txtPublishTopic.Text;
string content = this.rtbSendPublish.Text;
if(!string.IsNullOrEmpty(topic) && !string.IsNullOrEmpty(content))
{
byte[] bys = Encoding.UTF8.GetBytes(content);
ushort? publishId = this.mqttClient?.Publish(topic, bys, MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);
}
}
private void btnClear_Click(object sender, EventArgs e)
{
this.rtbRecive.Clear();
}
private void lstSubscribeTopic_MouseDoubleClick(object sender, MouseEventArgs e)
{
SubscribeModel subscribeModel = this.lstSubscribeTopic.SelectedItem as SubscribeModel;
if(subscribeModel != null)
{
subscribeModel.Unsubscribe(this.mqttClient);
}
}
}
/// <summary>
/// 订阅
/// </summary>
public class SubscribeModel
{
public string Topic { get; set; }
public byte Qos { get; set; }
public ushort MessageId { get; set; }
public ushort UnsubscribeId { get; set; }
public SubscribeModel(string topic, byte qos)
{
this.Topic = topic;
this.Qos = qos;
}
public void Subscribe(MqttClient mqttClient)
{
string[] topics = new string[] { this.Topic };
byte[] qos = new byte[] { this.Qos };
this.MessageId = mqttClient.Subscribe(topics, qos);
}
public void Unsubscribe(MqttClient mqttClient)
{
string[] topics = new string[] { this.Topic };
this.UnsubscribeId = mqttClient.Unsubscribe(topics);
}
public override string ToString()
{
return $"主题:{this.Topic},质量:{this.Qos},消息ID:{this.MessageId}";
}
}
}