-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_sub_ex2_json_print.go
55 lines (43 loc) · 1.25 KB
/
mqtt_sub_ex2_json_print.go
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"fmt"
"bytes"
"time"
"encoding/json"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
var out bytes.Buffer
json.Indent(&out, msg.Payload(), "", " ")
fmt.Printf("%s\n%s\n\n", msg.Topic(), string(out.Bytes()))
}
var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
fmt.Println("Connected")
}
var connectionLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
fmt.Printf("Connection Lost: %s\n", err.Error())
}
func main() {
var broker = "tcp://test.mosquitto.org:1883"
options := mqtt.NewClientOptions()
options.AddBroker(broker)
options.SetClientID("go_mqtt_sub_example")
//options.SetUsername("admin")
//options.SetPassword("12345")
options.SetDefaultPublishHandler(messagePubHandler)
options.OnConnect = connectHandler
options.OnConnectionLost = connectionLostHandler
client := mqtt.NewClient(options)
token := client.Connect()
if token.Wait() && token.Error() != nil {
panic(token.Error())
}
topic := "#"
token = client.Subscribe(topic, 1, nil)
token.Wait()
fmt.Printf("Subscribed to topic %s\n", topic)
for true {
time.Sleep(time.Second)
}
fmt.Println("bye")
}