diff --git a/kmqtt-client/src/commonMain/kotlin/io/github/davidepianca98/MQTTClient.kt b/kmqtt-client/src/commonMain/kotlin/io/github/davidepianca98/MQTTClient.kt index 6ec25e4d7f1d8cb7767d310ceeec755a20d38d8d..5492e3b023a21cb74f6c0695916c8bcd8d8967e4 100644 --- a/kmqtt-client/src/commonMain/kotlin/io/github/davidepianca98/MQTTClient.kt +++ b/kmqtt-client/src/commonMain/kotlin/io/github/davidepianca98/MQTTClient.kt @@ -60,6 +60,7 @@ import io.github.davidepianca98.socket.SocketClosedException import io.github.davidepianca98.socket.SocketInterface import io.github.davidepianca98.socket.streams.EOFException import io.github.davidepianca98.socket.tls.TLSClientSettings +import kotlinx.coroutines.CoroutineExceptionHandler /** * MQTT 3.1.1 and 5 client @@ -480,9 +481,13 @@ 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 + * @param exceptionHandler the exception handler for the coroutine scope */ - public fun runSuspend(dispatcher: CoroutineDispatcher = Dispatchers.Default) { - CoroutineScope(dispatcher).launch { + public fun runSuspend( + dispatcher: CoroutineDispatcher = Dispatchers.Default, + exceptionHandler: CoroutineExceptionHandler = CoroutineExceptionHandler { _, throwable ->} + ) { + CoroutineScope(dispatcher).launch(exceptionHandler) { run() } }