summaryrefslogtreecommitdiff
path: root/mqtt_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'mqtt_test.go')
-rw-r--r--mqtt_test.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/mqtt_test.go b/mqtt_test.go
new file mode 100644
index 0000000..fc31002
--- /dev/null
+++ b/mqtt_test.go
@@ -0,0 +1,44 @@
1package main
2
3import (
4 "fmt"
5 "log"
6 "os"
7 "time"
8
9 "github.com/eclipse/paho.mqtt.golang"
10)
11
12var f mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
13 fmt.Printf("TOPIC: %s\n", msg.Topic())
14 fmt.Printf("MSG: %s\n", msg.Payload())
15}
16
17func main() {
18 // mqtt.DEBUG
19 mqtt.ERROR = log.New(os.Stdout, "", 0)
20
21 opts := mqtt.NewClientOptions().AddBroker("tcp://172.16.0.191:1883").SetClientID("gotrivial")
22 opts.SetDefaultPublishHandler(f)
23
24 c := mqtt.NewClient(opts)
25 if token := c.Connect(); token.Wait() && token.Error() != nil {
26 panic(token.Error())
27 }
28
29 if token := c.Subscribe("/mfi/reports", 0, nil); token.Wait() && token.Error() != nil {
30 fmt.Println(token.Error())
31 os.Exit(1)
32 }
33
34 token := c.Publish("/mfi/devices/NewOffice", 0, false, "{\"output\":3,\"state\":0}")
35 token.Wait()
36 time.Sleep(10 * time.Second)
37
38 if token := c.Unsubscribe("/mfi/reports"); token.Wait() && token.Error() != nil {
39 fmt.Println(token.Error())
40 os.Exit(1)
41 }
42
43 c.Disconnect(250)
44}