gobot.io/x/gobot@v1.16.0/examples/nats_driver_ping.go (about)

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  // TO RUN:
     6  //  go run ./examples/nats_driver_ping.go <SERVER>
     7  //
     8  // EXAMPLE:
     9  //	go run ./examples/nats_driver_ping.go tls://nats.demo.io:4443
    10  //
    11  package main
    12  
    13  import (
    14  	"fmt"
    15  	"os"
    16  	"time"
    17  
    18  	"gobot.io/x/gobot"
    19  	"gobot.io/x/gobot/platforms/nats"
    20  )
    21  
    22  func main() {
    23  	natsAdaptor := nats.NewAdaptor(os.Args[1], 1234)
    24  
    25  	holaDriver := nats.NewDriver(natsAdaptor, "hola")
    26  	helloDriver := nats.NewDriver(natsAdaptor, "hello")
    27  
    28  	work := func() {
    29  		helloDriver.On(nats.Data, func(msg nats.Message) {
    30  			fmt.Println("hello")
    31  		})
    32  
    33  		holaDriver.On(nats.Data, func(msg nats.Message) {
    34  			fmt.Println("hola")
    35  		})
    36  
    37  		data := []byte("o")
    38  		gobot.Every(1*time.Second, func() {
    39  			helloDriver.Publish(data)
    40  		})
    41  
    42  		gobot.Every(5*time.Second, func() {
    43  			holaDriver.Publish(data)
    44  		})
    45  	}
    46  
    47  	robot := gobot.NewRobot("natsBot",
    48  		[]gobot.Connection{natsAdaptor},
    49  		[]gobot.Device{helloDriver, holaDriver},
    50  		work,
    51  	)
    52  
    53  	robot.Start()
    54  }