Log exceptions that occur in the MQTT coroutine
This commit is contained in:
parent
9e3a1c35db
commit
3d2573f8aa
2 changed files with 12 additions and 2 deletions
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.kernelmaft.zanbur.network
|
||||||
|
|
||||||
|
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
val exceptionPrinter = CoroutineExceptionHandler { _ , throwable ->
|
||||||
|
throwable . printStackTrace ()
|
||||||
|
throw throwable
|
||||||
|
}
|
||||||
|
|
@ -25,7 +25,7 @@ object MqttClient {
|
||||||
this . coroutineScope = coroutineScope
|
this . coroutineScope = coroutineScope
|
||||||
val json = Json { ignoreUnknownKeys = true }
|
val json = Json { ignoreUnknownKeys = true }
|
||||||
|
|
||||||
coroutineScope . launch (IO) {
|
coroutineScope . launch ( IO + exceptionPrinter ) {
|
||||||
client = MQTTClient ( MQTT5 , MQTT_SERVER_HOST , MQTT_SERVER_PORT , null ) {
|
client = MQTTClient ( MQTT5 , MQTT_SERVER_HOST , MQTT_SERVER_PORT , null ) {
|
||||||
for ( handler in publishHandlers ) handler ( it , json )
|
for ( handler in publishHandlers ) handler ( it , json )
|
||||||
}
|
}
|
||||||
|
|
@ -38,7 +38,7 @@ object MqttClient {
|
||||||
fun addPublishHandler ( handler : MqttPublishHandler ) = publishHandlers . add (handler)
|
fun addPublishHandler ( handler : MqttPublishHandler ) = publishHandlers . add (handler)
|
||||||
|
|
||||||
fun publish ( topic : String , payload : UByteArray ) {
|
fun publish ( topic : String , payload : UByteArray ) {
|
||||||
coroutineScope !! . launch (IO) {
|
coroutineScope !! . launch ( IO + exceptionPrinter ) {
|
||||||
client !! . publish ( false , AT_MOST_ONCE , topic , payload )
|
client !! . publish ( false , AT_MOST_ONCE , topic , payload )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue