Add Random ClientID string
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"strconv"
|
"crypto/rand"
|
||||||
|
|
||||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
@@ -52,7 +52,7 @@ func main() {
|
|||||||
opts.AddBroker(fmt.Sprintf("tcp://%s:%s", mqttHost, mqttPort))
|
opts.AddBroker(fmt.Sprintf("tcp://%s:%s", mqttHost, mqttPort))
|
||||||
opts.SetUsername(mqttUser)
|
opts.SetUsername(mqttUser)
|
||||||
opts.SetPassword(mqttPass)
|
opts.SetPassword(mqttPass)
|
||||||
opts.SetClientID("mqtt_presence_redis_go")
|
opts.SetClientID(fmt.Sprintf("mqtt_presence_redis_go_%s", randomString()))
|
||||||
|
|
||||||
// Handlers
|
// Handlers
|
||||||
opts.SetDefaultPublishHandler(func(client mqtt.Client, msg mqtt.Message) {
|
opts.SetDefaultPublishHandler(func(client mqtt.Client, msg mqtt.Message) {
|
||||||
@@ -114,10 +114,11 @@ func getEnv(key, defaultVal string) string {
|
|||||||
return defaultVal
|
return defaultVal
|
||||||
}
|
}
|
||||||
|
|
||||||
func atoi(s string) int {
|
func randomString() string {
|
||||||
i, err := strconv.Atoi(s)
|
b := make([]byte, 4)
|
||||||
if err != nil {
|
if _, err := rand.Read(b); err != nil {
|
||||||
return 0
|
log.Printf("Failed to generate random string: %v", err)
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
return i
|
return hex.EncodeToString(b)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user