Last active
July 9, 2024 22:24
-
-
Save BK1031/95772c0d4bd9366ed670b8f28ccaf950 to your computer and use it in GitHub Desktop.
racecar_analytics mqtt connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
mqtt "github.com/eclipse/paho.mqtt.golang" | |
) | |
func main() { | |
ConnectMQTT() | |
} | |
var Client mqtt.Client | |
var MQTTHost = "localhost" | |
var MQTTPort = "1883" | |
func ConnectMQTT() { | |
opts := mqtt.NewClientOptions() | |
opts.AddBroker(fmt.Sprintf("tcp://%s:%s", MQTTHost, MQTTPort)) | |
opts.OnConnect = connectHandler | |
client := mqtt.NewClient(opts) | |
if token := client.Connect(); token.Wait() && token.Error() != nil { | |
log.Fatal(token.Error()) | |
} | |
Client = client | |
} | |
var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) { | |
fmt.Println("[MQ] Connected to MQTT broker!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment