当前位置: 首页 > news >正文

使用M2Mqtt 接受以及发布MQTT消息

在NuGet库里面直接查找M2Mqtt就可以安装库。
使用framework4.5.2
1.配置文件操作

  public static class GModel{public static BassSetup  MainSetup { get; set; }public static void GetThisAdd(){MainSetup = new BassSetup();string IPAdd = ConfigurationManager.AppSettings["IPAdd"];string ServerPort = ConfigurationManager.AppSettings["ServerPort"];string UserName = ConfigurationManager.AppSettings["UserName"];string Pwd = ConfigurationManager.AppSettings["Pwd"];MainSetup.IPAdd = IPAdd ;MainSetup.ServerPort =int.Parse(ServerPort);MainSetup.UserName = UserName;MainSetup.Pwd = Pwd;}}

2连接服务器:

        /// <summary>/// 连接服务/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button4_Click(object sender, EventArgs e){string EMQX_CLIENT_ID = Guid.NewGuid().ToString();mqttClient = new MqttClient(GModel.MainSetup.IPAdd, GModel.MainSetup.ServerPort, false, null, null, MqttSslProtocols.None); //mqttClient = new MqttClient("http://192.168.0.197:61613");此方法不妥mqttClient.Connect(EMQX_CLIENT_ID,GModel.MainSetup.UserName,GModel.MainSetup.Pwd);//需要录入账号密码mqttClient.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;//订阅后的动作//mqttClient.Subscribe(new string[] { "123123124124512" }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });BTConnect.BackColor = Color.Green;}

3.订阅后的事件

 //这段定义了收到消息之后做什么事情private void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e){string topic = e.Topic.ToString();string message = System.Text.Encoding.Default.GetString(e.Message);//同时订阅两个或者以上主题时,分类收集收到的信息//reciveTopic.Text = topic;//reciveMessagetxt.Text = message;}

4.订阅主题

       /// <summary>/// 订阅/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button1_Click(object sender, EventArgs e){if (mqttClient != null && !string.IsNullOrEmpty(TBZhuti.Text)  ){mqttClient.Subscribe(new string[] { TBZhuti.Text}, new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });//一次订阅多个主题似乎会出错}else{MessageBox.Show("请先连接服务器,并且确认订阅主题有数据。",this.Text,MessageBoxButtons.OK,MessageBoxIcon.Information);}}

5.发布主题。

        /// <summary>/// 发布/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button2_Click(object sender, EventArgs e){if (mqttClient != null && !string.IsNullOrEmpty(TBZhuti.Text) && !string.IsNullOrEmpty(RTBContent.Text)){mqttClient.Publish(TBZhuti.Text, System.Text.Encoding.UTF8.GetBytes(RTBContent.Text), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);}}

6,解除订阅

 /// <summary>/// 解除订阅/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button3_Click(object sender, EventArgs e){mqttClient.Unsubscribe(new string[] { "AAAAAA242" });}

7.断开服务器连接

 /// <summary>///  断开连接/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void DisConnect_Click(object sender, EventArgs e){mqttClient.Disconnect();}

https://github.com/eclipse/paho.mqtt.m2mqtt

http://www.lryc.cn/news/137308.html

相关文章:

  • 【SA8295P 源码分析】33 - Android GVM USB 透传配置
  • 华为OD机试 - 过滤组合字符串 - 深度优先搜索dfs算法(Java 2023 B卷 100分)
  • 【Unity自制手册】游戏基础API大全
  • 【LVS】4、HAProxy搭建web集群
  • 【应用层】网络基础 -- HTTP协议
  • 【线性DP】模型总结(terse版)
  • conda 常用命令
  • 前端面试:【异步编程】Callback、Promise和Async/Await
  • 大数据(四):Pandas的基础应用详解
  • 计算机网络第3章(数据链路层)
  • stm32之4.时钟体系
  • RPC和HTTP协议
  • BUGFix:onnx -> TensorRT转换过程失败
  • FFMPEG小白常用命令行
  • 个性定制还是纯粹简约:探寻界面选择背后的心理宇宙
  • 【Java 高阶】一文精通 Spring MVC - 转发重定向(四)
  • 嵌入式Linux开发实操(十):ADC接口开发
  • 精进语言模型:探索LLM Training微调与奖励模型技术的新途径
  • 数据采集:selenium 提取 Cookie 自动登陆
  • [Go版]算法通关村第十三关黄金——数字数学问题之数论问题(最大公约数、素数、埃氏筛、丑数)
  • Qt双击某一文件通过自己实现的程序打开,并加载文件显示
  • 硬件产品的量产问题------硬件工程师在产线关注什么
  • Vulnhub系列靶机--- Hackadmeic.RTB1
  • redis高级----------主从复制
  • posgresql通过PL/pgSQL脚本统一修改某字段大小写
  • iPhone卫星通信SOS功能如何在灾难中拯救生命
  • NOIP真题答案 过河 数的划分
  • 图为科技-边缘计算在智慧医疗领域的作用
  • Linux配置nginx反向代理
  • 随便记录记录