aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEthel Morgan <eth@ethulhu.co.uk>2020-06-28 23:18:58 +0100
committerEthel Morgan <eth@ethulhu.co.uk>2020-06-28 23:18:58 +0100
commit1b799eb9f8cd415edd36a232ba8cca93de7730ed (patch)
tree4dc6cec7b0fb2fce4dda1663d5447a2381f145c8
parent264ae5e425e3c6f90fbea88ad66852a0b5186428 (diff)
add a default client ID of binary_hostname_pidv0.0.5
-rw-r--r--catbus.go11
1 files changed, 11 insertions, 0 deletions
diff --git a/catbus.go b/catbus.go
index 68dee1b..2a3a120 100644
--- a/catbus.go
+++ b/catbus.go
@@ -5,7 +5,10 @@
package catbus
import (
+ "fmt"
"math/rand"
+ "os"
+ "path"
"sync"
"time"
@@ -73,6 +76,7 @@ func NewClient(brokerURI string, options ClientOptions) Client {
mqttOpts := mqtt.NewClientOptions()
mqttOpts.AddBroker(brokerURI)
mqttOpts.SetAutoReconnect(true)
+ mqttOpts.SetClientID(defaultClientID())
mqttOpts.SetOnConnectHandler(func(_ mqtt.Client) {
client.stopAllTimers()
client.startAllTimers()
@@ -185,3 +189,10 @@ func messageFromMQTTMessage(msg mqtt.Message) Message {
Topic: msg.Topic(),
}
}
+
+func defaultClientID() string {
+ binary := path.Base(os.Args[0])
+ hostname, _ := os.Hostname()
+ pid := os.Getpid()
+ return fmt.Sprintf("%s_%s_%d", binary, hostname, pid)
+}