diff --git a/kmqtt-client/src/commonMain/kotlin/MQTTClient.kt b/kmqtt-client/src/commonMain/kotlin/MQTTClient.kt index 6e22d407727a6ab3b99de1c26cdf6f7d53a59db5..3060b3efb21735c705a8c915d66dc37074a89876 100644 --- a/kmqtt-client/src/commonMain/kotlin/MQTTClient.kt +++ b/kmqtt-client/src/commonMain/kotlin/MQTTClient.kt @@ -1,5 +1,6 @@ import kotlinx.atomicfu.AtomicBoolean import kotlinx.atomicfu.atomic +import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -354,6 +355,7 @@ public class MQTTClient( /** * Run a single iteration of the client (blocking) + * This function blocks the thread for a single iteration duration */ public fun step() { if (running.value) { @@ -363,6 +365,7 @@ public class MQTTClient( /** * Run the client (blocking) + * This function blocks the thread until the client stops */ public fun run() { while (running.value) { @@ -370,6 +373,17 @@ public class MQTTClient( } } + /** + * Run the client + * This function runs the thread on the specified dispatcher until the client stops + * @param dispatcher the dispatcher on which to run the client + */ + public fun runSuspend(dispatcher: CoroutineDispatcher = Dispatchers.Default) { + CoroutineScope(dispatcher).launch { + run() + } + } + private fun handlePacket(packet: MQTTPacket) { when (packet) { is MQTTConnack -> handleConnack(packet)