简体   繁体   English

Eclipse Paho MQTT Golang 发布订阅 AWS IOT 在同一台机器上作为单独进程运行时返回 EOF 错误

[英]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.

相关问题 如何在AWS IOT和Azure IOT HUB中查看MQTT数据 - How to see MQTT data in AWS IOT and Azure IOT HUB MQTT.fx 无法连接到 AWS IoT (MqttException) - MQTT.fx can't connect to AWS IoT (MqttException) golang reader.Read()可以到达EOF,但是reader.ReadAt()以死循环方式读取相同的数据,没有到达EOF? - golang reader.Read() can arrived EOF, but reader.ReadAt() read same data in dead loop way, not arrived EOF? golang aws CreateChangeSet 验证错误 - golang aws CreateChangeSet validation error 带有 esp8266 的 AWS 物联网 - AWS iot with esp8266 AWS IoT SDK - 主 L#206 订阅错误:-28(C 代码) - AWS IoT SDK - main L#206 Error subscribing : -28 (C code) 使用无服务器框架创建 AWS IoT 证书时出现无效资源配置错误 - Invalid resource configuration error while creating AWS IoT Certificate using serverless framework 使用 Eclipse AWS 插件时如何解决丢失的工件错误? - How to resolve missing artifact error when using Eclipse AWS plugin? 当 SSH 在本地机器上终止时,让 jupyter lab notebook 保持运行? - Keep jupyter lab notebook running when SSH is terminated on local machine? 这些行代码在golang中是否相同? - Are these line codes the same in golang?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM