hybridgroup.gobot/examples/nats.go

45 lines
771 B
Go
Raw Permalink Normal View History

//go:build example
// +build example
//
// Do not build by default.
package main
import (
"fmt"
"time"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/platforms/nats"
)
func main() {
natsAdaptor := nats.NewAdaptorWithAuth("localhost:4222", 1234, "user", "pass")
work := func() {
natsAdaptor.On("hello", func(msg nats.Message) {
fmt.Println("hello")
})
natsAdaptor.On("hola", func(msg nats.Message) {
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,
)
if err := robot.Start(); err != nil {
panic(err)
}
}