forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim2.go
42 lines (34 loc) · 984 Bytes
/
sim2.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
package main
import (
"fmt"
"os"
"time"
MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
)
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())
}
var onConnect MQTT.OnConnectHandler = func(client *MQTT.Client) {
fmt.Println("onConnect")
if token := client.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
os.Exit(1)
}
}
var subscribed = "#"
func main() {
// opts := MQTT.NewClientOptions().AddBroker("tcp://localhost:1883")
opts := MQTT.NewClientOptions().AddBroker("tcp://lite.mqtt.shiguredo.jp:1883")
opts.SetDefaultPublishHandler(f)
opts.SetOnConnectHandler(onConnect)
opts.SetCleanSession(true)
opts.SetUsername("shirou@github.com")
c := MQTT.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
for {
time.Sleep(1 * time.Second)
}
}