forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nats_driver_ping.go
50 lines (40 loc) · 913 Bytes
/
nats_driver_ping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 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()
}