基本信息
源码名称:mqtt发布订阅示例(实现了客户端与服务端通讯)
源码大小:2.19M
文件格式:.rar
开发语言:C#
更新时间:2019-04-25
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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(); });
}
/// <summary>
/// 连接服务器
/// </summary>
/// <returns></returns>
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);
})));
}
}
/// <summary>
/// 服务器连接成功
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MqttClient_Connected(object sender, EventArgs e)
{
Invoke((new Action(() =>
{
txtReceiveMessage.AppendText("已连接到MQTT服务器!" Environment.NewLine);
})));
}
/// <summary>
/// 断开服务器连接
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MqttClient_Disconnected(object sender, EventArgs e)
{
Invoke((new Action(() =>
{
txtReceiveMessage.AppendText("已断开MQTT连接!" Environment.NewLine);
})));
}
/// <summary>
/// 接收到消息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MqttClient_ApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e)
{
Invoke((new Action(() =>
{
txtReceiveMessage.AppendText($">> {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}{Environment.NewLine}");
})));
}
/// <summary>
/// 订阅消息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnSubscribe_ClickAsync(object sender, EventArgs e)
{
string topic = txtSubTopic.Text.Trim();
if (string.IsNullOrEmpty(topic))
{
MessageBox.Show("订阅主题不能为空!");
return;
}
if (!mqttClient.IsConnected)
{
MessageBox.Show("MQTT客户端尚未连接!");
return;
}
mqttClient.SubscribeAsync(new List<TopicFilter> {
new TopicFilter(topic, MqttQualityOfServiceLevel.AtMostOnce)
});
txtReceiveMessage.AppendText($"已订阅[{topic}]主题" Environment.NewLine);
txtSubTopic.Enabled = false;
btnSubscribe.Enabled = false;
}
/// <summary>
/// 发布主题
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnPublish_Click(object sender, EventArgs e)
{
string topic = txtPubTopic.Text.Trim();
if (string.IsNullOrEmpty(topic))
{
MessageBox.Show("发布主题不能为空!");
return;
}
string inputString = txtSendMessage.Text.Trim();
var appMsg = new MqttApplicationMessage(topic, Encoding.UTF8.GetBytes(inputString), MqttQualityOfServiceLevel.AtMostOnce, false);
mqttClient.PublishAsync(appMsg);
}
private void FmMqttClient_Load(object sender, EventArgs e)
{
//Dictionary<string, string> dic = new Dictionary<string, string>();
//dic.Add("ClientId", "123");
//dic.Add("Topic", "ttt");
//dic.Add("Value", "ggyy");
//dic.Add("ServiceLevel", "1");
//TopicLogic.SaveTopic(dic);
}
}
}