forked from vert-x3/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.groovy
More file actions
24 lines (22 loc) · 743 Bytes
/
client.groovy
File metadata and controls
24 lines (22 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import groovy.transform.Field
import io.vertx.mqtt.MqttClient
import io.vertx.core.buffer.Buffer
import io.netty.handler.codec.mqtt.MqttQoS
@Field def MQTT_MESSAGE = "Hello Vert.x MQTT Client"
@Field def BROKER_HOST = "localhost"
@Field def BROKER_PORT = 1883
@Field def MQTT_TOPIC = "/my_topic"
def 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())
}
})