This commit is contained in:
parent
5a0e8b842c
commit
9da308c063
@ -52,6 +52,7 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.4.10"
|
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.4.10"
|
||||||
|
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0-M1'
|
||||||
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
|
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,11 +6,12 @@ class MqttSubscriber : MqttCallbackExtended {
|
|||||||
private val broker = "tcp://mqtt.k8s.kmlabz.com:1883"
|
private val broker = "tcp://mqtt.k8s.kmlabz.com:1883"
|
||||||
private val clientId = MqttClient.generateClientId()
|
private val clientId = MqttClient.generateClientId()
|
||||||
private val persistence = MemoryPersistence()
|
private val persistence = MemoryPersistence()
|
||||||
private val mqttClient = MqttClient(broker, clientId, persistence)
|
private val mqttClient = MqttAsyncClient(broker, clientId, persistence)
|
||||||
private val benchValues = HashMap<Int, Instant>()
|
private val benchValues = HashMap<Int, Instant>()
|
||||||
|
|
||||||
override fun connectionLost(arg0: Throwable) {
|
override fun connectionLost(arg0: Throwable) {
|
||||||
System.err.println("connection lost")
|
System.err.println("connection lost")
|
||||||
|
mqttClient.reconnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun deliveryComplete(arg0: IMqttDeliveryToken) {
|
override fun deliveryComplete(arg0: IMqttDeliveryToken) {
|
||||||
@ -21,7 +22,6 @@ class MqttSubscriber : MqttCallbackExtended {
|
|||||||
println("topic: $topic")
|
println("topic: $topic")
|
||||||
benchValues.put(topic.split("/")[1].toInt(),Instant.now())
|
benchValues.put(topic.split("/")[1].toInt(),Instant.now())
|
||||||
println("message: " + String(message.payload))
|
println("message: " + String(message.payload))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun connectComplete(reconnect: Boolean, serverURI: String?) {
|
override fun connectComplete(reconnect: Boolean, serverURI: String?) {
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
|
import kotlinx.coroutines.GlobalScope
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
fun main(){
|
fun main(){
|
||||||
|
GlobalScope.launch {
|
||||||
val mqtt = MqttSubscriber()
|
val mqtt = MqttSubscriber()
|
||||||
mqtt.connect()
|
mqtt.connect()
|
||||||
Thread.sleep(10000)
|
delay(10000L)
|
||||||
mqtt.disconnect()
|
mqtt.disconnect()
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user