当前位置:网站首页>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连接断开以及重连的一些问题,希望能给大家带来帮助。
文章转载:
边栏推荐
- C language -- operators and expressions
- Qixia housing and Urban Rural Development Bureau and fire rescue brigade carried out fire safety training
- consul微服务治理中心踩坑
- 机器学习PCA——实验报告
- [work details] March 18, 2020
- Expand and collapse too high div
- Character sets and comparison rules
- 工作汇报(2)
- 指南针软件买股票进行交易安全吗?怎么开户买股票
- 【北邮果园微处理器设计】10 Serial Communication 串口通信笔记
猜你喜欢

携程机票 App KMM 跨端 KV 存储库 MMKV-Kotlin | 开源
![Installing MySQL under Linux [details]](/img/38/77be56c3ef3923ce4c4e5df4a96f41.png)
Installing MySQL under Linux [details]

Easyx-----c语言实现2048

Nacos2.x.x start error creating bean with name 'grpcclusterserver';

Win10 start FTP service and set login authentication

Nuxt. JS - learning notes

10 years' experience in programmer career - for you who are confused

2021 Q3-Q4 Kotlin Multiplatform 使用现状 | 调查报告

滑动窗口

PC qq Hall upload Update Modifying versioninfo
随机推荐
Laravel writes native SQL statements
一键部署ceph脚本
Machine learning LDA - Experimental Report
Plookup table in appliedzkp zkevm (8)
互联网对抗神器之漏洞扫描与反渗透
[software project management] sorting out knowledge points for final review
【深度学习理论】(6) 循环神经网络 RNN
10年程序员职业生涯感悟—写给正在迷茫的你
Machine learning deep neural network -- Experimental Report
c语言 --- 运算符和表达式
Easyexcel - Excel read / write tool
2、 Linear table
[deep learning theory] (7) long and short term memory network LSTM
QT connection MySQL data query failed
Vscode environment setup: synchronous configuration
Laravel admin obtains non auto increment ID and submits hidden forms
Which PHP open source works deserve attention
Easyx----- C language implementation 2048
MySQL模糊查询详解
Code specification & explain in detail the functions and uses of husky, prettier, eslint and lint staged