emqtt 同id被挤掉 不断掉线重连,连不上
作者:互联网
原文链接:https://blog.csdn.net/whb3299065/article/details/79290668
public void connect() throws MqttException {
//判断拦截状态,这里注意一下,如果没有这个判断,连接失败
if (!mqttClient.isConnected()) {
mqttClient.connect(options);
log.info("连接成功");
}else {
//这里的逻辑是如果连接不成功就重新连接
mqttClient.disconnect(); //不这样就重连不上
mqttClient.connect(options);
log.info("连接成功");
}
}
public synchronized void reconnect() {
while (true) {
try {
log.info("尝试重连...");
//如果没有发生异常说明连接成功,如果发生异常,则死循环
Thread.sleep(2000);
connect();
break;
}catch (Exception e){
continue;
}
}
}
标签:info,mqttClient,emqtt,log,掉线,连不上,connect,连接 来源: https://blog.csdn.net/zhan107876/article/details/100935779