概述
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//设置默认值
//服务器相关参数
txtServerAddress.Text = "127.0.0.1";
txtPort.Text = "1883";
txtAccount.Text = "admin";
txtPassword.Text = "123456";
//默认订阅主题
txtSubscribeTopic.Text = "ADD;MOD;DEL";
//默认广播的主题
txtPublishTopic.Text = "MOD";
//默认广播的消息内容
txtPublishMsg.Text = $"我们左右进攻,形成两面包夹芝士!{ Environment.NewLine }";
}
MqttClient client = null;
private void button1_Click(object sender, EventArgs e)
{
//创建客户端实例
//client = new MqttClient(IPAddress.Parse("127.0.0.1"));//IP
string ip = txtServerAddress.Text;
int port = Convert.ToInt32(txtPort.Text);
string account = txtAccount.Text;
string password = txtPassword.Text;
client = new MqttClient(ip, port, false,null,null,MqttSslProtocols.None); //IP
//MqttClient client = new MqttClient("域名地址");//域名
//注册消息接收处理事件(主题订阅成功回调函数;取消主题订阅成功回调函数;已断开服务器回调函数等委托事件)
client.MqttMsgPublishReceived += SubscribeMessageReceived;
client.MqttMsgSubscribed += SubscribeSucceed;
client.MqttMsgPublished += PublishSucceed;
client.MqttMsgUnsubscribed += UnSubscribeSucceed;
//生成客户端ID并连接服务器
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId,account, password);
LinkStart();
}
//订阅消息接收回调函数
void SubscribeMessageReceived(object sender, MqttMsgPublishEventArgs e)
{
string topic = e.Topic;
string msg = Encoding.Default.GetString(e.Message);
txtSubscribeMessage.Invoke(new EventHandler(delegate
{
txtSubscribeMessage.Text += $" 于{ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") } 收到消息:{ Environment.NewLine } 主题:{ topic + Environment.NewLine } 内容: { msg }";
}));
}
//广播成功回调函数
void PublishSucceed(object sender, MqttMsgPublishedEventArgs e)
{
txtPublishMsg.Invoke(new EventHandler(delegate
{
txtPublishMsg.Text += $" { e.MessageId } 于{ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") } 广播完毕。";
}));
}
//订阅主题成功回调函数
void SubscribeSucceed(object sender, MqttMsgSubscribedEventArgs e)
{
try
{
string subscribes = txtSubscribeTopic.Text;
txtSubscribeMessage.Invoke(new EventHandler(delegate
{
this.txtSubscribeMessage.Text += $" {subscribes} 频道订阅成功{ Environment.NewLine }";
}));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show("出现了异常,异常信息已打印在控制台。");
}
}
//取订主题成功回调函数
void UnSubscribeSucceed(object sender, MqttMsgUnsubscribedEventArgs e)
{
try
{
string subscribes = txtSubscribeTopic.Text;
txtSubscribeMessage.Invoke(new EventHandler(delegate
{
this.txtSubscribeMessage.Text += $" {subscribes} 频道取消订阅成功{ Environment.NewLine }";
}));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show("出现了异常,异常信息已打印在控制台。");
}
}
//广播消息
private void btnPublish_Click(object sender, EventArgs e)
{
//Topic Message Qos retain
string topic = txtPublishTopic.Text;
string publishMsg = txtPublishMsg.Text;
if (string.IsNullOrEmpty(topic))
{
MessageBox.Show("请填写发布消息的主题");
txtPublishTopic.Focus();
return;
}
if (string.IsNullOrEmpty(publishMsg))
{
MessageBox.Show("请填写发布消息的内容");
txtPublishTopic.Focus();
return;
}
client.Publish(topic, Encoding.Default.GetBytes(publishMsg),MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE,false);
}
//Link Start
public void LinkStart()
{
System.Media.SoundPlayer sp = new System.Media.SoundPlayer();
sp.SoundLocation = AppDomain.CurrentDomain.BaseDirectory + @"link start part.wav"; //音乐文件
sp.Play();
Thread.Sleep(6500);
txtSubscribeMessage.Invoke(new EventHandler(delegate
{
this.txtSubscribeMessage.Text += $" 已成功与服务器建立通信{ Environment.NewLine }";
}));
}
//订阅主题
private void btnSubscribe_Click(object sender, EventArgs e)
{
//订阅主题"ADD", "MOD", "DEL" 消息质量为 1
string topicsvalue = txtSubscribeTopic.Text;
string[] topics = { };
if (!string.IsNullOrEmpty(topicsvalue))
{
if (topicsvalue.IndexOf(";") > -1)
{
topics = topicsvalue.Split(';');
}
else
{
topics = new string[] { topicsvalue };
}
}
else
{
MessageBox.Show("请填写订阅主题名称");
}
byte[] modes = { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE };
client.Subscribe(topics, modes);
}
//取消订阅主题
private void btnCancel_Click(object sender, EventArgs e)
{
string topicsvalue = txtSubscribeTopic.Text;
string[] topics = { };
if (!string.IsNullOrEmpty(topicsvalue))
{
if (topicsvalue.IndexOf(";") > -1)
{
topics = topicsvalue.Split(';');
}
else
{
topics = new string[] { topicsvalue };
}
}
else
{
MessageBox.Show("请填写订阅主题名称");
}
client.Unsubscribe(topics);
}
}
}
完整代码下载地址:WinFormsApp1.rar-互联网文档类资源-CSDN下载
完整代码备用下载地址:http://129.204.119.56:10104/
最后
以上就是要减肥便当为你收集整理的.net c# 基于netcore框架,操作Mqtt。的全部内容,希望文章能够帮你解决.net c# 基于netcore框架,操作Mqtt。所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复