golang paho mqtt 删除消息
golang paho mqtt dropping messages
我有一个 mosquitto 代理 运行 在防火墙后面的服务器中。所需的端口已打开,我从外部检查它是否正在使用:
mosquitto_sub -h [ip address] -t "#" -u [username] -P [password] -v
mosquitto_pub -h [ip address] -t "topic" -u [username] -P [password] -m "hello"
我可以看到消息已发布。
我想用一个小的go程序来做同样的事情,代码如下:
package main
import (
"crypto/tls"
"fmt"
"time"
"strconv"
MQTT "github.com/eclipse/paho.mqtt.golang"
)
func messageHandler(c MQTT.Client, msg MQTT.Message) {
fmt.Printf("TOPIC: %s\n", msg.Topic())
fmt.Printf("MSG: %s\n", msg.Payload())
}
func connLostHandler(c MQTT.Client, err error) {
fmt.Printf("Connection lost, reason: %v\n", err)
}
func main() {
opts := MQTT.NewClientOptions()
skipVerify := true
opts.AddBroker("tcp://[ip]:1883")
opts.SetTLSConfig(&tls.Config{InsecureSkipVerify: skipVerify})
//opts.SetTLSConfig(&tls.Config{InsecureSkipVerify: skipVerify, ClientAuth: tls.NoClientCert})
opts.SetClientID("myclientid")
opts.SetAutoReconnect(true)
//opts.SetCleanSession(true)
opts.SetDefaultPublishHandler(messageHandler)
opts.SetConnectionLostHandler(connLostHandler)
opts.OnConnect = func(c MQTT.Client) {
fmt.Printf("Client connected\n")
}
client := MQTT.NewClient(opts)
token := client.Connect()
token.Wait()
fmt.Println("connected")
if token.Error() != nil {
fmt.Println("problems with connection")
panic(token.Error())
}
for i := 0; i < 10; i++ {
str := "hello: " + strconv.Itoa(i)
token := client.Publish("topic/temperature", 0, false, str)
token.Wait()
if token.Error() != nil {
fmt.Println("problems with pub")
}
fmt.Println("published")
time.Sleep(1000 * time.Millisecond)
}
fmt.Println("finished")
client.Disconnect(1)
}
我期望的结果是发布了所有编号的问候,但只有很少的消息通过。这是我的代理配置文件,我认为它具有使其工作的最低配置:
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
log_dest file /var/log/mosquitto/mosquitto.log
log_type debug
allow_anonymous true
port 1883
listener 8883
我可以说我在 paho go 库上做错了,因为我用 python 使用 python paho mqtt 库完成了一个类似的脚本,它工作得很好:
import paho.mqtt.client as mqtt
import time
topic1 = "topic/temperature"
mqttClient = mqtt.Client()
mqttClient.connect([ip], 1883, 60)
mqttClient.loop_start()
for i in range(10000):
mqttClient.publish(topic1, "hello - " + str(i), qos= 0)
#time.sleep(1)
mqttClient.loop_stop()
mqttClient.disconnect()
我也尝试过更改QOS,但结果是相似的。我错过了什么?
根据评论 - 问题是由于另一个客户端使用相同的客户端 ID。检查这一点的最简单方法是阅读代理日志(从客户端的角度来看,连接只是在没有警告的情况下断开)。
The MQTT spec 状态:
The Server MUST process a second CONNECT Packet sent from a Client as a protocol violation and disconnect the Client [MQTT-3.1.0-2]. See section 4.8 for information about handling errors.
这是一个相当普遍的问题(也是项目自述文件 common problems 部分中提到的第一件事)。
我有一个 mosquitto 代理 运行 在防火墙后面的服务器中。所需的端口已打开,我从外部检查它是否正在使用:
mosquitto_sub -h [ip address] -t "#" -u [username] -P [password] -v
mosquitto_pub -h [ip address] -t "topic" -u [username] -P [password] -m "hello"
我可以看到消息已发布。 我想用一个小的go程序来做同样的事情,代码如下:
package main
import (
"crypto/tls"
"fmt"
"time"
"strconv"
MQTT "github.com/eclipse/paho.mqtt.golang"
)
func messageHandler(c MQTT.Client, msg MQTT.Message) {
fmt.Printf("TOPIC: %s\n", msg.Topic())
fmt.Printf("MSG: %s\n", msg.Payload())
}
func connLostHandler(c MQTT.Client, err error) {
fmt.Printf("Connection lost, reason: %v\n", err)
}
func main() {
opts := MQTT.NewClientOptions()
skipVerify := true
opts.AddBroker("tcp://[ip]:1883")
opts.SetTLSConfig(&tls.Config{InsecureSkipVerify: skipVerify})
//opts.SetTLSConfig(&tls.Config{InsecureSkipVerify: skipVerify, ClientAuth: tls.NoClientCert})
opts.SetClientID("myclientid")
opts.SetAutoReconnect(true)
//opts.SetCleanSession(true)
opts.SetDefaultPublishHandler(messageHandler)
opts.SetConnectionLostHandler(connLostHandler)
opts.OnConnect = func(c MQTT.Client) {
fmt.Printf("Client connected\n")
}
client := MQTT.NewClient(opts)
token := client.Connect()
token.Wait()
fmt.Println("connected")
if token.Error() != nil {
fmt.Println("problems with connection")
panic(token.Error())
}
for i := 0; i < 10; i++ {
str := "hello: " + strconv.Itoa(i)
token := client.Publish("topic/temperature", 0, false, str)
token.Wait()
if token.Error() != nil {
fmt.Println("problems with pub")
}
fmt.Println("published")
time.Sleep(1000 * time.Millisecond)
}
fmt.Println("finished")
client.Disconnect(1)
}
我期望的结果是发布了所有编号的问候,但只有很少的消息通过。这是我的代理配置文件,我认为它具有使其工作的最低配置:
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
log_dest file /var/log/mosquitto/mosquitto.log
log_type debug
allow_anonymous true
port 1883
listener 8883
我可以说我在 paho go 库上做错了,因为我用 python 使用 python paho mqtt 库完成了一个类似的脚本,它工作得很好:
import paho.mqtt.client as mqtt
import time
topic1 = "topic/temperature"
mqttClient = mqtt.Client()
mqttClient.connect([ip], 1883, 60)
mqttClient.loop_start()
for i in range(10000):
mqttClient.publish(topic1, "hello - " + str(i), qos= 0)
#time.sleep(1)
mqttClient.loop_stop()
mqttClient.disconnect()
我也尝试过更改QOS,但结果是相似的。我错过了什么?
根据评论 - 问题是由于另一个客户端使用相同的客户端 ID。检查这一点的最简单方法是阅读代理日志(从客户端的角度来看,连接只是在没有警告的情况下断开)。
The MQTT spec 状态:
The Server MUST process a second CONNECT Packet sent from a Client as a protocol violation and disconnect the Client [MQTT-3.1.0-2]. See section 4.8 for information about handling errors.
这是一个相当普遍的问题(也是项目自述文件 common problems 部分中提到的第一件事)。