Skip to content

Commit

Permalink
✨ mica-mqtt-client 重连不管服务端是否存在 session 都发送订阅
Browse files Browse the repository at this point in the history
  • Loading branch information
li-xunhuan committed Jun 6, 2024
1 parent ab89e16 commit b94f660
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,8 @@ public void processConAck(ChannelContext context, MqttConnAckMessage message) {
}
// 2. 发布连接通知
publishConnectEvent(context);
// 3. 如果 session 不存在重连时发送重新订阅,更改 ip、端口之后需要重新发送订阅
if (!connAckVariableHeader.isSessionPresent() || MqttClient.isNeedReSub(context)) {
reSendSubscription(context);
}
// 3. 发送订阅,不管服务端是否存在 session 都发送
reSendSubscription(context);
break;
case CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD:
case CONNECTION_REFUSED_IDENTIFIER_REJECTED:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@
*/
public final class MqttClient {
private static final Logger logger = LoggerFactory.getLogger(MqttClient.class);
/**
* 是否需要重新订阅
*/
private static final String MQTT_NEED_RE_SUB = "MQTT_NEED_RE_SUB";
private final TioClient tioClient;
private final MqttClientCreator config;
private final TioClientConfig clientTioConfig;
Expand Down Expand Up @@ -517,24 +513,13 @@ public boolean reconnect(Node serverNode) {
}
try {
this.context = tioClient.connect(serverNode, config.getTimeout());
this.context.set(MQTT_NEED_RE_SUB, (byte) 1);
return true;
} catch (Exception e) {
logger.error("mqtt client reconnect error", e);
}
return false;
}

/**
* 是否需要重新订阅
*
* @param context ChannelContext
* @return 是否需要重新订阅
*/
public static boolean isNeedReSub(ChannelContext context) {
return context.getAndRemove(MQTT_NEED_RE_SUB) != null;
}

/**
* 断开 mqtt 连接
*
Expand Down

0 comments on commit b94f660

Please sign in to comment.