forked from shirou/mqttcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.go
55 lines (45 loc) · 829 Bytes
/
subscribe.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 (
"os"
"time"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
)
func subscribe(c *cli.Context) {
setDebugLevel(c)
opts, err := NewOption(c)
if err != nil {
log.Error(err)
os.Exit(1)
}
opts.SetKeepAlive(time.Second * 60)
if c.Bool("c") {
clientId := c.String("i")
if clientId == "" {
log.Warn("clean Flag does not work without client id")
}
opts.SetCleanSession(false)
}
qos := c.Int("q")
topic := c.String("t")
if topic == "" {
log.Errorf("Please specify topic")
os.Exit(1)
}
log.Infof("Topic: %s", topic)
subscribed := map[string]byte{
topic: byte(qos),
}
_, err = connect(c, opts, subscribed)
if err != nil {
log.Error(err)
os.Exit(1)
}
// loops forever
for {
if messageReceived == true {
break
}
time.Sleep(time.Second)
}
}