1
0
mirror of https://github.com/shirou/mqttcli.git synced 2025-04-24 13:48:57 +08:00
mqttcli/subscribe.go
2020-12-30 21:22:55 +09:00

54 lines
801 B
Go

package main
import (
"os"
"time"
log "github.com/Sirupsen/logrus"
"github.com/urfave/cli/v2"
)
func subscribe(c *cli.Context) error {
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 {
time.Sleep(time.Second)
}
return nil
}