2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-03-13 23:01:39 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2016-07-17 11:59:07 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/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() {
|
2017-04-22 02:46:45 +08:00
|
|
|
natsAdaptor.On("hello", func(msg nats.Message) {
|
2016-07-17 11:59:07 +08:00
|
|
|
fmt.Println("hello")
|
|
|
|
})
|
2017-04-22 02:46:45 +08:00
|
|
|
natsAdaptor.On("hola", func(msg nats.Message) {
|
2016-07-17 11:59:07 +08:00
|
|
|
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
|
|
|
}
|