/
mqtt_pub_forever.go
92 lines (64 loc) · 1.86 KB
/
mqtt_pub_forever.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
package main
import (
"fmt"
//import the Paho Go MQTT library
//MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
//"os"
"time"
"math/rand"
"encoding/json"
)
//define a function for the default message handler
var f MQTT.MessageHandler = func(client *MQTT.Client, msg MQTT.Message) {
fmt.Printf("TOPIC: %s\n", msg.Topic())
fmt.Printf("MSG: %s\n", msg.Payload())
}
func main() {
//create a ClientOptions struct setting the broker address, clientid, turn
//off trace output and set the default message handler
opts := MQTT.NewClientOptions().AddBroker("tcp://nava.work:1883")
opts.SetClientID("go-simple")
opts.SetDefaultPublishHandler(f)
//create and start a client using the above ClientOptions
c := MQTT.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
//subscribe to the topic /go-mqtt/sample and request messages to be delivered
//at a maximum qos of zero, wait for the receipt to confirm the subscription
//if token := c.Subscribe("go-mqtt/sample", 0, nil); token.Wait() && token.Error() != nil {
//fmt.Println(token.Error())
//os.Exit(1)
//}
//Publish 5 messages to /go-mqtt/sample at qos 1 and wait for the receipt
//from the server after sending each message
type payload struct {
Vendorid int
Value int
Jobid int
Siteid int
Cardid int
Timestamp string
}
for {
p := payload{
Cardid : rand.Intn(100),
Jobid : rand.Intn(5),
Siteid : rand.Intn(100),
Value : rand.Intn(100)*10,
Vendorid : rand.Intn(2000),
Timestamp :time.Now().Format(time.RFC3339),
}
ret ,_ := json.Marshal(p)
val := string(ret)
text := val
fmt.Println(text)
token := c.Publish("paybox", 0, false, text)
token.Wait()
//os.Exit(0)
time.Sleep(10 * time.Second)
}
//unsubscribe from /go-mqtt/sample
c.Disconnect(250)
}