-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (100 loc) · 2.67 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
114
115
116
117
118
119
package main
import (
"context"
"flag"
"fmt"
"log"
"net/url"
"os"
"strconv"
mqtt "github.com/srishina/mqtt.go"
)
var usageStr = `
Usage: client-pub [options] <topic> <qos> <payload>
Options:
-b, --broker <broker address> MQTTv5 broker address"
-id, --clientid <client ID> Client identifier - optional"
-k, --keepalive <keep alive> Keep alive - optional, default: 0"
-cs, --cleanstart <Clean start> Start clean - a new session is created in broker - optional, default: true"
example:
client-pub -b ws://mqtt.eclipseprojects.io:80/mqtt "TEST/GREETING" 1 "Willkommen"
`
func isFlagPassed(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}
func usage() {
fmt.Printf("%s\n", usageStr)
os.Exit(0)
}
func main() {
var (
broker string
clientID string
keepAlive int
cleanStart bool
)
flag.StringVar(&broker, "b", "", "MQTTv5 broker address")
flag.StringVar(&broker, "broker", "", "MQTTv5 broker address")
flag.StringVar(&clientID, "id", "", "Client identifier")
flag.StringVar(&clientID, "clientid", "", "Client identifier")
flag.IntVar(&keepAlive, "k", 0, "Keep alive")
flag.IntVar(&keepAlive, "keepalive", 0, "Keep alive")
flag.BoolVar(&cleanStart, "cs", true, "Start clean - a new session is created in broker")
flag.BoolVar(&cleanStart, "cleanstart", true, "Start clean - a new session is created in broker")
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) < 3 {
usage()
}
if !isFlagPassed("b") && !isFlagPassed("broker") {
log.Fatal("Error: MQTTV5 broker address is not given")
usage()
}
u, err := url.Parse(broker)
if err != nil {
log.Fatal(err)
}
var conn mqtt.Connection
switch u.Scheme {
case "ws":
fallthrough
case "wss":
conn = &mqtt.WebsocketConn{Host: broker}
case "tcp":
conn = &mqtt.TCPConn{Host: u.Host}
default:
log.Fatal("Invalid scheme name")
}
var opts []mqtt.ClientOption
opts = append(opts, mqtt.WithCleanStart(cleanStart))
opts = append(opts, mqtt.WithKeepAlive(uint16(keepAlive)))
opts = append(opts, mqtt.WithClientID(clientID))
client := mqtt.NewClient(conn, opts...)
_, err = client.Connect(context.Background())
if err != nil {
log.Fatal(err)
}
topic, qos, payload := args[0], args[1], args[2]
qosLevel, err := strconv.ParseInt(qos, 10, 64)
if err != nil {
log.Fatal(err)
}
if qosLevel > 2 {
log.Fatal("Maximum QoS must be 2")
}
// publish
err = client.Publish(context.Background(), topic, byte(qosLevel), false, []byte(payload), nil)
if err != nil {
log.Fatal(err)
}
// Disconnect from broker
client.Disconnect(context.Background(), mqtt.DisconnectReasonCodeNormal, nil)
}