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