[英]Eclipse Paho MQTT Golang publish subscribe to AWS IOT returns EOF error when running as separate process on same machine
I am using Go v1.17.2 on Linux to connect to AWS MQTT using the paho.mqtt.golang library v1.4.1.我在 Linux 上使用 Go v1.17.2 使用paho.mqtt.golang库 v1.4.1 连接到 AWS MQTT。 I am basing my code on this sample from EMQX using TLS with the certificates provided by AWS IOT Core.
我的代码基于EMQX的这个示例,使用 TLS 和 AWS IOT Core 提供的证书。
When I run the publish and subscribe code in the same go program as per the above example from EMQX everything works and I can see the following output:当我按照 EMQX 的上述示例在同一go 程序中运行发布和订阅代码时,一切正常,我可以看到以下 output:
2022/08/11 19:47:42 Connected
Subscribed to topic: topic_1
2022/08/12 13:47:42 Received message: Message 0 from topic: topic_1
2022/08/11 19:47:43 Received message: Message 1 from topic: topic_1
...
2022/08/11 19:47:51 Received message: Message 9 from topic: topic_1
However, if I run the publish code in one go program (using go run) and run the subscribe in a separate program on the same machine at the same time, then the subscribe fails on each message with an EOF error:但是,如果我在一个 go 程序中运行发布代码(使用 go 运行)并同时在同一台机器上的单独程序中运行订阅,那么每条消息的订阅都会失败并出现 EOF 错误:
2022/08/11 19:54:50 Connected
2022/08/11 19:54:54 Connect lost: EOF
...
2022/08/11 19:54:59 Connected
2022/08/11 19:54:59 Connect lost: EOF
Here is the publish code这是发布代码
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
log.Fatalln(token.Error())
}
publish(client, *topic)
client.Disconnect(250)
...
func publish(client mqtt.Client, topic string) {
num := 10
for i := 0; i < num; i++ {
text := fmt.Sprintf("Message %d", i)
token := client.Publish(topic, 0, false, text)
token.Wait()
time.Sleep(time.Second)
}
}
and the subscribe code和订阅代码
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
log.Fatalln(token.Error())
}
sub(client, *topic)
time.Sleep(11 * time.Second)
client.Disconnect(250)
...
func sub(client mqtt.Client, topic string) {
token := client.Subscribe(topic, 1, nil)
token.Wait()
log.Printf("Subscribed to topic: %s", topic)
}
If I run the subscribe code only, and use the AWS IOT console to publish some message that also works如果我只运行订阅代码,并使用 AWS IOT 控制台发布一些同样有效的消息
2022/08/11 20:25:27 Connected
2022/08/11 20:25:27 Subscribed to topic: topic_1
2022/08/11 20:25:29 Received message: {
"message": "Hello from AWS IoT console"
} from topic: topic_1
2022/08/11 20:25:30 Received message: {
"message": "Hello from AWS IoT console"
} from topic: topic_1
It looks like the issue is to do with having two connections open to AWS IOT/MQTT at the same time.看起来问题与同时打开 AWS IOT/MQTT 的两个连接有关。
Here is my MQTT client options config这是我的 MQTT 客户端选项配置
opts := mqtt.NewClientOptions()
opts.AddBroker(fmt.Sprintf("tls://%s:%d", *host, 8883))
opts.SetClientID("basicPubSub")
opts.SetTLSConfig(tlsConfig)
opts.SetDefaultPublishHandler(messagePubHandler)
opts.OnConnect = connectHandler
opts.OnConnectionLost = connectLostHandler
This was due to me using the same client ID for the publish and subscribe code.这是因为我对发布和订阅代码使用了相同的客户端 ID。
The Eclipse Paho docs explain this via a link to the MQTT specifications Eclipse Paho 文档通过指向MQTT 规范的链接对此进行了解释
If validation is successful the Server performs the following steps.
如果验证成功,服务器将执行以下步骤。
1.If the ClientId represents a Client already connected to the Server then the Server MUST disconnect the existing Client [MQTT-3.1.4-2].
1.如果 ClientId 代表一个已经连接到服务器的客户端,那么服务器必须断开现有的客户端 [MQTT-3.1.4-2]。
I used a different client ID for each program and it works.我为每个程序使用了不同的客户端 ID,它可以工作。 Note: The AWS IOT policy must allow for each client ID to connect (allow iot:Connec t action)
注意: AWS IOT 策略必须允许每个客户端 ID 进行连接(允许iot:Connec t 操作)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.