-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_pub_ex1.go
50 lines (39 loc) · 1.12 KB
/
mqtt_pub_ex1.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
package main
import (
"fmt"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
fmt.Printf("%s\n%s\n\n", msg.Topic(), msg.Payload())
}
var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
fmt.Println("Connected")
}
var connectionLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
fmt.Printf("Connection Lost: %s\n", err.Error())
}
func main() {
var broker = "tcp://test.mosquitto.org:1883"
options := mqtt.NewClientOptions()
options.AddBroker(broker)
options.SetClientID("go_mqtt_pub_example")
options.SetDefaultPublishHandler(messagePubHandler)
options.OnConnect = connectHandler
options.OnConnectionLost = connectionLostHandler
client := mqtt.NewClient(options)
token := client.Connect()
if token.Wait() && token.Error() != nil {
panic(token.Error())
}
topic := "mytopic/mymsg"
num := 10
for i := 0; i < num; i++ {
fmt.Println(i)
text := fmt.Sprintf("%d", i)
token = client.Publish(topic, 0, false, text)
token.Wait()
time.Sleep(time.Second)
}
fmt.Println("bye")
}