forked from vert-x3/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.kt
30 lines (26 loc) · 870 Bytes
/
Client.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package io.vertx.example.mqtt.simple
import io.netty.handler.codec.mqtt.MqttQoS
import io.vertx.core.buffer.Buffer
import io.vertx.mqtt.MqttClient
class Client : io.vertx.core.AbstractVerticle() {
var MQTT_MESSAGE = "Hello Vert.x MQTT Client"
var BROKER_HOST = "localhost"
var BROKER_PORT = 1883
var MQTT_TOPIC = "/my_topic"
override fun start() {
var mqttClient = MqttClient.create(vertx)
mqttClient.connect(BROKER_PORT, BROKER_HOST, { ch ->
if (ch.succeeded()) {
println("Connected to a server")
mqttClient.publish(MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE), MqttQoS.AT_MOST_ONCE, false, false, { s ->
mqttClient.disconnect({ d ->
println("Disconnected from server")
})
})
} else {
println("Failed to connect to a server")
println(ch.cause())
}
})
}
}