1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-04 22:17:39 +08:00
hybridgroup.gobot/examples/nats_driver_ping.go
Marc-Antoine Ruel 50d5869808 Fix go test ./...
Make all examples to not be built by default by adding the build tag 'example'.
Some files were automatically reformatted by goimports upon saving.
2017-03-13 11:01:39 -04:00

55 lines
963 B
Go

// +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"
"gobot.io/x/gobot/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(data interface{}) {
fmt.Println("hello")
})
holaDriver.On(nats.Data, func(data interface{}) {
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,
)
robot.Start()
}