telegraf/Godeps/_workspace/src/git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golan.../samples/sim.go

36 lines
725 B
Go

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 subscribed = "#"
func main() {
opts := MQTT.NewClientOptions().AddBroker("tcp://localhost:1883")
opts.SetDefaultPublishHandler(f)
opts.SetCleanSession(true)
c := MQTT.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
if token := c.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
os.Exit(1)
}
for {
time.Sleep(1 * time.Second)
}
}