Example #1
0
func main() {
	c := mqtt.CreateClient()

	if os.Getenv("MESSAGE") != "" {
		data := struct {
			Message string `json:"message"`
		}{
			os.Getenv("MESSAGE"),
		}
		b, err := json.Marshal(data)
		if err != nil {
			log.Panic(err)
		}
		t := c.Publish("/go-mqtt/sample", 0, false, string(b))
		t.Wait()
		if err := t.Error(); err != nil {
			log.Panic(err)
		}
		return
	}

	c.Subscribe("/go-mqtt/sample", 0, nil)

	time.Sleep(1 * time.Hour)

	c.Disconnect(250)
}
Example #2
0
func main() {
	c := mqtt.CreateClient()

	c.Subscribe("http/requests", 0, f)

	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
	log.Println("awaiting signal")
	<-sigs
	log.Println("exiting")

	c.Disconnect(250)
}
Example #3
0
func main() {
	chanMap = make(map[string]chan message.Response)
	c = mqtt.CreateClient()

	ref := c.Subscribe("http/responses", 0, HandleMqttMessage)
	ref.Wait()
	if err := ref.Error(); err != nil {
		log.Println(err)
		return
	}

	server := http.Server{
		Addr:         ":8000",
		Handler:      &myHandler{},
		ReadTimeout:  10 * time.Second,
		WriteTimeout: 10 * time.Second,
	}

	server.ListenAndServe()
}