From cf4622392cc56d22ca1f420755ff3d3fa6b0ddcf Mon Sep 17 00:00:00 2001 From: Torma Kristof Date: Wed, 21 Oct 2020 16:57:36 +0200 Subject: [PATCH] testing --- src/main/kotlin/MQTTSubscriber.kt | 34 +++++++++++++++++++++++++++++++ src/main/kotlin/Main.kt | 2 ++ 2 files changed, 36 insertions(+) create mode 100644 src/main/kotlin/MQTTSubscriber.kt diff --git a/src/main/kotlin/MQTTSubscriber.kt b/src/main/kotlin/MQTTSubscriber.kt new file mode 100644 index 0000000..016536b --- /dev/null +++ b/src/main/kotlin/MQTTSubscriber.kt @@ -0,0 +1,34 @@ +import org.eclipse.paho.client.mqttv3.* +import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence + + +class MqttSubscriber : MqttCallbackExtended { + private val broker = "tcp://mqtt.k8s.kmlabz.com:1883" + private val clientId = MqttClient.generateClientId() + private val persistence = MemoryPersistence() + private val mqttClient = MqttAsyncClient(broker, clientId, persistence) + + override fun connectionLost(arg0: Throwable) { + System.err.println("connection lost") + } + + override fun deliveryComplete(arg0: IMqttDeliveryToken) { + } + + @Throws(Exception::class) + override fun messageArrived(topic: String, message: MqttMessage) { + println("topic: $topic") + println("message: " + String(message.payload)) + } + + override fun connectComplete(reconnect: Boolean, serverURI: String?) { + mqttClient.subscribe("#", 0) + } + + fun connect() { + val connOpts = MqttConnectOptions() + connOpts.isCleanSession = true + mqttClient.setCallback(MqttSubscriber()) + mqttClient.connect(connOpts) + } +} \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 43a3307..2786b34 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,3 +1,5 @@ fun main(){ + val mqtt = MqttSubscriber() + mqtt.connect() println("Hello World!") } \ No newline at end of file