I have a MQTT Go program that is subscribed to the topic "info"
, in which I receive a JSON message. I validate that JSON message, and if the validation is successful, I want to start subscribing to a new topic "info_updates"
. Here is my subscribing code:
func Info(){
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
opts := MQTT.NewClientOptions().AddBroker("tcp://test.mosquitto.org:1883")
opts.SetDefaultPublishHandler(f)
topic := "info" //I want to be able to change this later to "info_updates"
opts.OnConnect = func(c MQTT.Client) {
if token := c.Subscribe(topic, 0, f); token.Wait() && token.Error() != nil {
panic(token.Error())
}
}
// Creating new client
client := MQTT.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
} else {
fmt.Printf("Connected to server
")
}
<-c
}
Here is the code where I validate the JSON (parts of it):
var f MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
var integrationResult string
if JSONValidate(msg.Payload())[0] == ""{ //sanity check = successful
integrationResult = "successful"
}else{ //sanity check = unsuccessful
integrationResult = "unsuccessful"
}
//do something here to tell the first function to change subscribing topic if integrationResult = "successful"
}
Note: func Info()
and MQTT.MessageHandler
are in two seperate files. I am stumped on how I can communicate with func Info()
to change the topic subscription. Thank you.