我在 mosquitto 上看到消息持久性和 qos=2 的消息傳遞不一致。有什么我做錯了嗎?我有一個簡單的測試應用程序,它使用 clientId="receive-client" 注冊消費主題,但立即斷開連接。然后它以 clientId="send-client" 的身份連接并發布 10 條消息,“message #1”...“message #10”。然后斷開連接,等待五秒鐘,然后在打印和計算收到的消息時再次連接以使用“receive-client”消費。結果不一致。有時我收到 6 條消息,有時是 8 條。典型的輸出是這樣的:WARN[0005] GOT A MESSAGE:message #1 WARN[0005] GOT A MESSAGE:message #2 WARN[0005] GOT A MESSAGE:message #3 WARN[0005] GOT A MESSAGE:message #4 WARN[0005] GOT A MESSAGE:message #5 WARN[0005] GOT A MESSAGE:message #6 WARN[0005] GOT A MESSAGE:message #7 WARN[0005] GOT A MESSAGE:message #8 WARN[0305] PAUSE WARN[0605] received message count=8 我的版本信息是 1.4.15。我的 mosquitto.conf 是:pid_file /var/run/mosquitto.pidpersistence truepersistence_location /var/lib/mosquitto/allow_anonymous falsepassword_file /etc/mosquitto/passwdlog_dest file /var/log/mosquitto/mosquitto.log最初 /var/lib/mosquitto/mosquitto.db 直到運行了幾次迭代才會出現。我的測試應用在這里:import ( mqtt "github.com/eclipse/paho.mqtt.golang" log "github.com/sirupsen/logrus" "time")var receivedMsg intfunc Persist() { const TOPIC = "test" const URL = "tcp://localhost:1883" const USERNAME = "myuser" const PASSWORD = "mypassword" defer printReceived() options := mqtt.NewClientOptions().AddBroker(URL).SetUsername(USERNAME).SetPassword(PASSWORD) options.SetCleanSession(false) options.SetConnectRetry(true) options.SetConnectRetryInterval(10 * time.Millisecond)
1 回答

慕碼人8056858
TA貢獻1803條經驗 獲得超6個贊
在 QOS 2 上發布是一個多步驟過程,因此最可能的原因是您在所有消息真正完成發布到代理之前斷開了發布客戶端的連接。您可能應該在循環中進行發布,并使用調用返回的令牌client.publish()來等待它完成,然后再斷開客戶端。
例如,如示例所示:
//Publish 5 messages to /go-mqtt/sample at qos 1 and wait for the receipt
//from the server after sending each message
for i := 0; i < 5; i++ {
text := fmt.Sprintf("this is msg #%d!", i)
token := c.Publish("go-mqtt/sample", 0, false, text)
token.Wait()
}
- 1 回答
- 0 關注
- 128 瀏覽
添加回答
舉報
0/150
提交
取消