60 lines
1.0 KiB
Go
60 lines
1.0 KiB
Go
//go:build example
|
|
// +build example
|
|
|
|
//
|
|
// Do not build by default.
|
|
|
|
// TO RUN:
|
|
//
|
|
// go run ./examples/nats_driver_ping.go <SERVER>
|
|
//
|
|
// EXAMPLE:
|
|
//
|
|
// go run ./examples/nats_driver_ping.go tls://nats.demo.io:4443
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"gobot.io/x/gobot/v2"
|
|
"gobot.io/x/gobot/v2/platforms/nats"
|
|
)
|
|
|
|
func main() {
|
|
natsAdaptor := nats.NewAdaptor(os.Args[1], 1234)
|
|
|
|
holaDriver := nats.NewDriver(natsAdaptor, "hola")
|
|
helloDriver := nats.NewDriver(natsAdaptor, "hello")
|
|
|
|
work := func() {
|
|
_ = helloDriver.On(nats.Data, func(msg nats.Message) {
|
|
fmt.Println("hello")
|
|
})
|
|
|
|
_ = holaDriver.On(nats.Data, func(msg nats.Message) {
|
|
fmt.Println("hola")
|
|
})
|
|
|
|
data := []byte("o")
|
|
gobot.Every(1*time.Second, func() {
|
|
helloDriver.Publish(data)
|
|
})
|
|
|
|
gobot.Every(5*time.Second, func() {
|
|
holaDriver.Publish(data)
|
|
})
|
|
}
|
|
|
|
robot := gobot.NewRobot("natsBot",
|
|
[]gobot.Connection{natsAdaptor},
|
|
[]gobot.Device{helloDriver, holaDriver},
|
|
work,
|
|
)
|
|
|
|
if err := robot.Start(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|