Example #1
0
func main() {
	cp, err := loadCertPool("../keys/cacert.pem")
	if err != nil {
		log.Fatal("loadCertPool failed: ", err)
	}
	c, err := client.Connect(client.Param{
		Addr:      "tls://127.0.0.1:8883",
		ID:        "tlstest",
		OnPublish: onPublish,
		Options: &client.Options{
			Version:      4,
			CleanSession: true,
			KeepAlive:    60,
			TLSConfig: &tls.Config{
				RootCAs: cp,
			},
		},
	})
	if err != nil {
		log.Fatal("client.Connect failed: ", err)
	}
	defer func() {
		err := c.Disconnect(false)
		log.Print("disconnected: ", err)
	}()
	log.Print("connected")
	err = c.Subscribe([]client.Topic{
		{Filter: "#", QoS: client.AtMostOnce},
	})
	if err != nil {
		log.Print("c.Subscribe failed: ", err)
	}
	log.Print("subscribed")
	time.Sleep(time.Second * 10)
}
Example #2
0
func main() {
	c, err := client.Connect(client.Param{
		ID: "example_ping",
		Options: &client.Options{
			CleanSession: true,
			KeepAlive:    60,
			Logger:       log.New(os.Stderr, "MQTT-C", log.LstdFlags),
		},
	})
	if err != nil {
		log.Fatalf("Connect() failed: %v", err)
	}
	err = c.Ping()
	if err != nil {
		log.Printf("Ping() failed: %v", err)
	}
	err = c.Disconnect(false)
	if err != nil {
		log.Printf("Disconnect() failed: %v", err)
	}
	log.Print("DONE")
}
Example #3
0
func main() {
	c, err := client.Connect(client.Param{
		ID: "example_publish",
		Options: &client.Options{
			CleanSession: true,
			KeepAlive:    60,
			Logger:       log.New(os.Stderr, "MQTT-C", log.LstdFlags),
		},
	})
	if err != nil {
		log.Fatalf("Connect() failed: %v", err)
	}
	err = c.Publish(client.AtMostOnce, false,
		"users/123/objects/789", []byte("Hello MQTT"))
	if err != nil {
		log.Printf("Publish() failed: %v", err)
	}
	err = c.Disconnect(false)
	if err != nil {
		log.Printf("Disconnect() failed: %v", err)
	}
	log.Print("DONE")
}
Example #4
0
func main() {
	wg := new(sync.WaitGroup)
	wg.Add(1)
	c1, err := client.Connect(client.Param{
		ID: "example_subscribe_1",
		Options: &client.Options{
			CleanSession: true,
			KeepAlive:    60,
			Logger:       log.New(os.Stderr, "MQTT-C1", log.LstdFlags),
		},
	})
	if err != nil {
		log.Fatalf("c1.Connect() failed: %v", err)
	}
	err = c1.Subscribe([]client.Topic{
		{Filter: "#", QoS: client.AtMostOnce},
	})
	if err != nil {
		log.Fatalf("c1.Subscribe() failed: %v", err)
	}

	go func() {
		defer wg.Done()
		m, err := c1.Read(true)
		if err != nil {
			log.Printf("c1.Read() failed: %v", err)
			return
		}
		if m == nil {
			log.Print("c1.Read() unexpected end")
			return
		}
		log.Printf("c1.Read() topic=%s body=%s", m.Topic, string(m.Body))
	}()

	c2, err := client.Connect(client.Param{
		ID: "example_subscribe_2",
		Options: &client.Options{
			CleanSession: true,
			KeepAlive:    60,
			Logger:       log.New(os.Stderr, "MQTT-C2", log.LstdFlags),
		},
	})
	if err != nil {
		log.Fatalf("c2.Connect() failed: %v", err)
	}
	err = c2.Publish(client.AtMostOnce, false,
		"users/123/objects/789", []byte("Hello MQTT"))
	if err != nil {
		log.Fatalf("c2.Publish() failed: %v", err)
	}

	wg.Wait()

	err = c2.Disconnect(false)
	if err != nil {
		log.Printf("c2.Disconnect() failed: %v", err)
	}
	err = c1.Disconnect(false)
	if err != nil {
		log.Printf("c1.Disconnect() failed: %v", err)
	}
	log.Print("DONE")
}