diff --git a/kmqtt-client/src/commonMain/kotlin/MQTTClient.kt b/kmqtt-client/src/commonMain/kotlin/MQTTClient.kt index 40cdeeb7ba1112243077b39f42167d03d7e357a3..218fd461c5347517aa8479bcd4678c88bd0fa670 100644 --- a/kmqtt-client/src/commonMain/kotlin/MQTTClient.kt +++ b/kmqtt-client/src/commonMain/kotlin/MQTTClient.kt @@ -369,34 +369,34 @@ public class MQTTClient( onDisconnected(null) throw e } - } else { - // If connack not received in a reasonable amount of time, then disconnect - val currentTime = currentTimeMillis() - val lastActive = lastActiveTimestamp.value - val isConnackReceived = connackReceived.value + } + + // If connack not received in a reasonable amount of time, then disconnect + val currentTime = currentTimeMillis() + val lastActive = lastActiveTimestamp.value + val isConnackReceived = connackReceived.value - if (!isConnackReceived && currentTime > lastActive + (connackTimeout * 1000)) { + if (!isConnackReceived && currentTime > lastActive + (connackTimeout * 1000)) { + close() + lastException = Exception("CONNACK not received in 30 seconds") + throw lastException!! + } + + val actualKeepAlive = keepAlive.value + if (actualKeepAlive != 0 && isConnackReceived) { + if (currentTime > lastActive + (actualKeepAlive * 1000)) { + // Timeout close() - lastException = Exception("CONNACK not received in 30 seconds") + lastException = MQTTException(ReasonCode.KEEP_ALIVE_TIMEOUT) throw lastException!! - } - - val actualKeepAlive = keepAlive.value - if (actualKeepAlive != 0 && isConnackReceived) { - if (currentTime > lastActive + (actualKeepAlive * 1000)) { - // Timeout - close() - lastException = MQTTException(ReasonCode.KEEP_ALIVE_TIMEOUT) - throw lastException!! - } else if (currentTime > lastActive + (actualKeepAlive * 1000 * 0.9)) { - val pingreq = if (mqttVersion == MQTTVersion.MQTT3_1_1) { - MQTT4Pingreq() - } else { - MQTT5Pingreq() - } - send(pingreq.toByteArray()) - // TODO if not receiving pingresp after a reasonable amount of time, close connection + } else if (currentTime > lastActive + (actualKeepAlive * 1000 * 0.9)) { + val pingreq = if (mqttVersion == MQTTVersion.MQTT3_1_1) { + MQTT4Pingreq() + } else { + MQTT5Pingreq() } + send(pingreq.toByteArray()) + // TODO if not receiving pingresp after a reasonable amount of time, close connection } } }