Paho-mqtt.client可以连接到digital-ocean mqtt服务器但无法订阅

时间:2015-07-27 11:55:33

标签: debian mqtt digital-ocean paho hivemq

请帮我解决这个问题。我花了两天时间没有成功。

我正在使用Paho-Mqtt python客户端示例。我的digitalocean debian服务器上也有HiveMq mqtt服务器。 python中的客户端示例代码可以连接并订阅digitalocean服务器,如果我在我的笔记本电脑或我的Linux Debian机器上运行它。但。这是但是。当我在digitalocean服务器上运行客户端(其中hivemq mqtt服务器是)时,它只能连接并且不能订阅!我使用了本地主机和服务器IP,但仍然没有运气。

但是,在digitalocean服务器上运行的相同客户端代码(以及hivemq mqtt服务器)可以成功连接和订阅外部服务器,例如m2m.eclipse.org。

我怎么知道它不订阅但连接?好吧,它在连接回调时返回RC:0,但在订阅回调时不返回任何内容。 (它应该返回'订阅:1'等)

总结: 当Clinet与服务器在同一台机器上运行时,它可以连接但无法订阅。

1 个答案:

答案 0 :(得分:0)

The problem seems to be that the sub.py example (from here: http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.python.git/tree/examples/sub.py) seems to be incorrect. It does not execute the subscribe in the on_connect callback but does it directly after connecting. My understanding is, that the connect() method does not block for the connack.

You could verify this yourself if you sleep for e.g. a second after connecting.

So just call the subscribe() in the on_connect callback.

The official Paho page shows how to do this correctly, see the getting started page here: https://eclipse.org/paho/clients/python/

The reason why this wrong code works with mosquitto and not with HiveMQ is, that mosquitto is single threaded and HiveMQ is async and multi-threaded. So the connect and subscribe can get executed in parallel, you want the sequential behaviour most likely, this is why you must use the on_connect() callback.