当前位置:网站首页>MQTT断开重连
MQTT断开重连
2022-06-26 10:19:00 【放大的EZ】
问题
最近开发项目需要用到MQTT,开发完成测试的时候发现经常连接断开。
由于没有报错信息,在万能的百度搜索看看有没有遇到同样问题的网友,结果发现可能是客户端ID的问题,我每次启动都是一样的客户端ID,之前的客户端ID在MQTT服务器上还保存着没有清理掉,再次启动客户端就连接不上。
解决方法
使用随机数生存客户端ID,重新启动,问题得到解决,不会一直连接断开。
项目运行了几天发现又出现一个新问题,长时间没有操作连接还是会断开,无法发送和接收消息。
在网上查阅MQTT相关文章,详细了解MQTT,发现MQTT有个自动重连功能。有两种方式可以实现自动重连。
1、MQTT连接配置类MqttConnectOption类可以设置自动重连。如下图:

设置自动重连后,callback要实现MqttCallbackExtended,这个接口里有个连接完成的回调方法connectComplete(boolean reconnect,String serverURI),连接完成后可以做订阅主题。如下图:

2、自定义重连
如果MQTT连接配置类MqttConnectOption类没有配置自动重连,还可以做自定义重连。MqttCallback接口里有个connectionLost(Throwable arg0)接口,在这个接口的实现方法里可以自定义重连。如下图:

总结
以上就是关于MQTT连接断开以及重连的一些问题,希望能给大家带来帮助。
文章转载:
边栏推荐
- Common regular expressions - tool classes (mobile number, email, QQ, fax)
- 一键部署ceph脚本
- 2、 Linear table
- laravel中使用group by分组并查询数量
- [difficult and miscellaneous diseases] @transitional failure summary
- Redis knowledge mind map
- Detailed explanation of MySQL fuzzy query
- Notes - simple but adequate series_ KVM quick start
- 4、 Stacks and queues
- [software project management] sorting out knowledge points for final review
猜你喜欢
![[Beiyou orchard microprocessor design] 10 serial communication serial communication notes](/img/61/b4cfb0500bbe39ed6a371bb8672a2f.png)
[Beiyou orchard microprocessor design] 10 serial communication serial communication notes

JWT (SSO scheme) + three ways of identity authentication

Svn installation configuration

PC QQ大廳 上傳更新 修改versionInfo

代码规范 & 详细解释 husky、prettier、eslint、lint-staged 的作用和使用

Machine learning PCA - Experimental Report

Vscode environment setup: synchronous configuration

【北邮果园微处理器设计】10 Serial Communication 串口通信笔记

4、 Stacks and queues

【软件项目管理】期末复习知识点整理
随机推荐
目前为止最全的Kubernetes最新版核心命令
DataBinding使用与原理分析
机器学习线性回归——实验报告
JWT certification agreement -- I opened a Yihong hospital
2021 Q3-Q4 Kotlin Multiplatform 使用现状 | 调查报告
4、 Stacks and queues
Grain Mall - distributed Foundation
Search engine advanced search method records
openresty 概述
工作汇报(3)
[echart] i. how to learn echart and its characteristic document reading notes
c语言 --- 运算符和表达式
QT connection MySQL data query failed
Server single and two-way adjustable one key mutual trust script!
最牛X的CMDB系统
DD command tests the read and write speed of Huawei Kunpeng & Hongshan solid state storage disk
Wangeditor uploading local video modification
Matrix fast power notes
ACK攻击是什么意思?ACK攻击怎么防御?
Implementing MySQL master-slave replication in docker