forked from jeffallen/mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (55 loc) · 1.38 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
package main
import (
"flag"
"fmt"
"net"
"os"
"time"
proto "github.com/huin/mqtt"
"github.com/jeffallen/mqtt"
)
var host = flag.String("host", "localhost:1883", "hostname of broker")
var id = flag.String("id", "", "client id")
var user = flag.String("user", "", "username")
var pass = flag.String("pass", "", "password")
var dump = flag.Bool("dump", false, "dump messages?")
var delay = flag.Duration("delay", time.Second, "delay between messages")
var who = flag.String("who", "bonnie", "who is this? (to make two instance of ticktock distinct)")
func main() {
flag.Parse()
conn, err := net.Dial("tcp", *host)
if err != nil {
fmt.Fprint(os.Stderr, "dial: ", err)
return
}
cc := mqtt.NewClientConn(conn)
cc.Dump = *dump
cc.ClientId = *id
tq := []proto.TopicQos{
{Topic: "tick", Qos: proto.QosAtMostOnce},
}
if err := cc.Connect(*user, *pass); err != nil {
fmt.Fprintf(os.Stderr, "connect: %v\n", err)
os.Exit(1)
}
cc.Subscribe(tq)
// Sender
go func() {
for {
now := time.Now()
what := fmt.Sprintf("%v at %v", *who, now)
cc.Publish(&proto.Publish{
Header: proto.Header{Retain: false},
TopicName: "tick",
Payload: proto.BytesPayload([]byte(what)),
})
time.Sleep(*delay)
}
}()
// Receiver
for m := range cc.Incoming {
fmt.Print(m.TopicName, "\t")
m.Payload.WritePayload(os.Stdout)
fmt.Println("\tr: ", m.Header.Retain)
}
}