You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
953 B
Go
31 lines
953 B
Go
4 months ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"fmt"
|
||
|
|
||
|
"git.runcible.io/learning/learn_mqtt_go/common"
|
||
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
flag.Parse()
|
||
|
fmt.Printf("MQTT Host: %s\nMQTT Port: %s\n", *common.BrokerHost, *common.BrokerPort)
|
||
|
|
||
|
options := mqtt.NewClientOptions()
|
||
|
options.AddBroker(fmt.Sprintf("tcp://%s:%s", *common.BrokerHost, *common.BrokerPort))
|
||
|
|
||
|
// options.SetClientID("go_mqtt_client")
|
||
|
options.SetUsername("dirp")
|
||
|
options.SetPassword("dirp")
|
||
|
options.SetDefaultPublishHandler(func(client mqtt.Client, msg mqtt.Message) {
|
||
|
fmt.Printf("Received Message: %s from topic %s\n", msg.Payload(), msg.Topic())
|
||
|
})
|
||
|
options.OnConnect = func(c mqtt.Client) { fmt.Println("Connected to Broker") }
|
||
|
options.OnConnectionLost = func(c mqtt.Client, err error) { fmt.Printf("Connection Lost: %v", err) }
|
||
|
client := mqtt.NewClient(options)
|
||
|
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
||
|
panic(token.Error())
|
||
|
}
|
||
|
}
|