2016-07-17 11:59:07 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/platforms/nats"
|
2016-07-17 11:59:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-10-03 22:58:43 +08:00
|
|
|
natsAdaptor := nats.NewAdaptorWithAuth("localhost:4222", 1234, "user", "pass")
|
2016-07-17 11:59:07 +08:00
|
|
|
|
|
|
|
work := func() {
|
|
|
|
natsAdaptor.On("hello", func(data []byte) {
|
|
|
|
fmt.Println("hello")
|
|
|
|
})
|
|
|
|
natsAdaptor.On("hola", func(data []byte) {
|
|
|
|
fmt.Println("hola")
|
|
|
|
})
|
|
|
|
data := []byte("o")
|
|
|
|
gobot.Every(1*time.Second, func() {
|
|
|
|
natsAdaptor.Publish("hello", data)
|
|
|
|
})
|
|
|
|
gobot.Every(5*time.Second, func() {
|
|
|
|
natsAdaptor.Publish("hola", data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("natsBot",
|
|
|
|
[]gobot.Connection{natsAdaptor},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2016-10-19 03:08:25 +08:00
|
|
|
robot.Start()
|
2016-07-17 11:59:07 +08:00
|
|
|
}
|