add random string to mqtt clientID to prevent concurrent connections

This commit is contained in:
Leland Sindt
2021-03-15 21:14:59 -05:00
parent 5273955b15
commit 55d30a197e
+6 -5
View File
@@ -11,6 +11,7 @@ import (
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
"github.com/thanhpk/randstr"
)
// defining a lot of vars that are used my the MQTT MessageHandler
@@ -229,11 +230,11 @@ func TeslaMateAPICarsStatusV1(c *gin.Context) {
// create options for the MQTT client connection
opts := mqtt.NewClientOptions().AddBroker(mqttURL)
// setting generic MQTT settings in opts
opts.SetKeepAlive(2 * time.Second) // setting keepalive for client
opts.SetDefaultPublishHandler(f) // using f mqtt.MessageHandler function
opts.SetPingTimeout(1 * time.Second) // setting pingtimeout for client
opts.SetClientID("teslamateapi") // setting mqtt client id for TeslaMateApi
opts.SetCleanSession(true) // removal of all subscriptions on disconnect
opts.SetKeepAlive(2 * time.Second) // setting keepalive for client
opts.SetDefaultPublishHandler(f) // using f mqtt.MessageHandler function
opts.SetPingTimeout(1 * time.Second) // setting pingtimeout for client
opts.SetClientID("teslamateapi-" + randstr.String(4)) // setting mqtt client id for TeslaMateApi
opts.SetCleanSession(true) // removal of all subscriptions on disconnect
// creating MQTT connection with options
m := mqtt.NewClient(opts)