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

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"time"
    10  
    11  	"gobot.io/x/gobot"
    12  	"gobot.io/x/gobot/platforms/nats"
    13  )
    14  
    15  func main() {
    16  	natsAdaptor := nats.NewAdaptorWithAuth("localhost:4222", 1234, "user", "pass")
    17  
    18  	work := func() {
    19  		natsAdaptor.On("hello", func(msg nats.Message) {
    20  			fmt.Println("hello")
    21  		})
    22  		natsAdaptor.On("hola", func(msg nats.Message) {
    23  			fmt.Println("hola")
    24  		})
    25  		data := []byte("o")
    26  		gobot.Every(1*time.Second, func() {
    27  			natsAdaptor.Publish("hello", data)
    28  		})
    29  		gobot.Every(5*time.Second, func() {
    30  			natsAdaptor.Publish("hola", data)
    31  		})
    32  	}
    33  
    34  	robot := gobot.NewRobot("natsBot",
    35  		[]gobot.Connection{natsAdaptor},
    36  		work,
    37  	)
    38  
    39  	robot.Start()
    40  }