gobot.io/x/gobot/v2@v2.1.0/examples/nats.go (about)

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