/
main.go
114 lines (94 loc) · 2.71 KB
/
main.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"os"
"os/signal"
"flag"
"strings"
"time"
"github.com/yosssi/gmq/mqtt"
"github.com/yosssi/gmq/mqtt/client"
)
var host = flag.String("host", "localhost", "hostname of broker")
//var user = flag.String("user", "", "username")
//var pass = flag.String("pass", "", "password")
var retain = flag.Bool("retain", false, "retain message?")
var wait = flag.Bool("wait", false, "stay connected after publishing?")
var message = flag.String("message", "", "message")
var topic = flag.String("topic", "", "topic")
var port = flag.String("port", "1883", "port")
//var qos = flag.String("qos", string(mqtt.QoS0)[:1], "qos")
//var qos = flag.String("qos", "0", "qos") // something quirky with default of "0" results in no default
var qos = flag.String("qos", "00", "qos")
var delay = flag.Int64("delay", 5, "delay")
var Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
flag.Parse()
if (flag.NFlag() < 2) {
fmt.Println("Too few arguments")
Usage()
os.Exit(0)
}
if ((*message == "") || (*topic == "")) {
fmt.Println("Need a topic and message to publish")
Usage()
os.Exit(0)
}
fmt.Println("topic: ", *topic, "\tmessage: ", *message)
// Set up channel on which to send signal notifications.
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, os.Kill)
// Create an MQTT Client.
cli := client.New(&client.Options{
// Define the processing of the error handler.
ErrorHandler: func(err error) {
fmt.Println(err)
},
})
// Terminate the Client.
defer cli.Terminate()
s := []string{*host, *port}
address := strings.Join(s, ":")
fmt.Println("host info: ", address, "QoS option: ", *qos)
var mQoS = mqtt.QoS0
switch *qos {
case "0":
mQoS = mqtt.QoS0
case "1":
mQoS = mqtt.QoS1
case "2":
mQoS = mqtt.QoS2
default:
mQoS = mqtt.QoS0
}
// Connect to the MQTT Server.
err := cli.Connect(&client.ConnectOptions{
Network: "tcp",
Address: address,
ClientID: []byte("example-client"),
// CONNACKTimeout: 30,
// KeepAlive: 50,
})
if err != nil {
panic(err)
}
err = cli.Publish(&client.PublishOptions{
QoS: mQoS,
TopicName: []byte(*topic),
Message: []byte(*message),
})
if err != nil {
panic(err)
}
// Wait for receiving a signal.
// I cannot figure out why publish does not work without this command
// <-sigc
time.Sleep(time.Duration(*delay) * time.Millisecond)
// Disconnect the Network Connection.
if err := cli.Disconnect(); err != nil {
panic(err)
}
}