惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - chengeng

让SVN可以修改提交log Python开发环境搭建 ubuntu 安装mysql Android Studio汉化备忘 DataConvert VS2022,IF条件为Flase也进入断点,设置方法 在CentOS上,查看CPU、内存和磁盘的指标命令 mysql 常用语句 Java 异步调用方法 修改Web.config,IIS启动速度提升60倍 Windows smbv1共享协议开启 windows 10 激活 c#将一个类型对象数据赋值到另一个类型对象(名字相同的情况) 根据json内容更新表的一行,字段数量不固定,但名称需要一致 Windows 服务启动共享目录 FastReport 打印弹框,无法关闭(自己特定的场景,做个备忘,请勿参考) WinForm 指定时间没有操作键盘鼠标,弹出屏幕保护 MDns C# 实现 Makaretu.Dns.Multicast WinForm截屏 C#根据json内容动态生成SQL语句,字段数量可以不一样 金蝶云星空BOS平台官网知识 .Net Framework 离线安装包
MQTT服务器订阅消息例子
chengeng · 2025-03-10 · via 博客园 - chengeng

image

//关键代码

IMqttClient mqttClient;
public async void ReceiveData()
{
    try
    {
        //不等空,断开连接
        if (mqttClient == null)
        {
            var factory = new MqttFactory();
            var client = factory.CreateMqttClient();
            var options = new MqttClientOptionsBuilder()
                .WithTcpServer("192.168.250.141",1883)
                .Build();

            client.ApplicationMessageReceivedAsync += e =>
            {
                var topic = e.ApplicationMessage.Topic;
                var playload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
                SaveData(e);
                return Task.CompletedTask;
            };
            //连接MQ
            await client.ConnectAsync(options);

            //订阅主题
            await client.SubscribeAsync(new MqttTopicFilterBuilder()
                .WithTopic("itema/#")
                .Build());

            //历史数据删除(方法内部定时删除,不会每次都删除)
            threadDeleteHistoryData = new Thread(DeleteHistoryData_Thread);
            threadDeleteHistoryData.IsBackground = true;
            threadDeleteHistoryData.Start();
        }
    }
    catch (Exception ex)
    {
        string strLog = $"MQTT订阅消息异常\n:{ex.Message}\n{ex.StackTrace}";
        Logger.WriteLog(strLog);
        Console.WriteLine(strLog);
    }
}


 private void SaveData(MqttApplicationMessageReceivedEventArgs arg)
 {
    //Do Something
 }

posted @ 2025-03-10 10:37  chengeng  阅读(61)  评论()    收藏  举报