https://qiita.com/sheep29/items/637f9f00e35cc707f681 でmosquittoのpub/subを勉強
mosquittodがlocalhostで動いている前提でmosquitto_sub -d -t helloでサブ開始
以下でトピックhello,holaに data := []byte("o") をPublish成功!
なお以下のOnメソッドはsubscribeにあたる
package main
import (
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/mqtt"
"fmt"
"time"
)
func main() {
mqttAdaptor := mqtt.NewAdaptor("localhost:1883", "pinger")
work := func() {
mqttAdaptor.On("hello", func(msg mqtt.Message) {
fmt.Println(msg)
})
mqttAdaptor.On("hola", func(msg mqtt.Message) {
fmt.Println(msg)
})
data := []byte("o")
gobot.Every(1*time.Second, func() {
mqttAdaptor.Publish("hello", data)
})
gobot.Every(5*time.Second, func() {
mqttAdaptor.Publish("hola", data)
})
}
robot := gobot.NewRobot("mqttBot",
[]gobot.Connection{mqttAdaptor},
work,
)
robot.Start()
}
0 件のコメント:
コメントを投稿