1

我正在尝试将 paho mqtt 客户端连接到运行 ubuntu 的 virtualbox 上的 vernemq 消息代理。我无法向代理发布消息并随后通过订阅同一主题来阅读该消息。当我尝试运行程序时,我没有收到任何错误或“与结果代码 rc 连接”的确认。我在本地 ubuntu 机器上尝试过同样的事情,它可以工作。下面是我的 vernemq.conf 文件和 client.py 文件,用于发布和订阅。

accept_eula = yes
allow_anonymous = on
allow_register_during_netsplit = off
allow_publish_during_netsplit = off
allow_subscribe_during_netsplit = off
allow_unsubscribe_during_netsplit = off
allow_multiple_sessions = off
coordinate_registrations = on
max_inflight_messages = 20
max_online_messages = 1000
max_offline_messages = 1000
max_message_size = 0
upgrade_outgoing_qos = off
listener.max_connections = 10000
listener.nr_of_acceptors = 10
listener.tcp.default = 127.0.0.1:1883
listener.vmq.clustering = 0.0.0.0:44053
listener.http.default = 127.0.0.1:8888
systree_enabled = on
systree_interval = 20000
graphite_enabled = off
graphite_host = localhost
graphite_port = 2003
graphite_interval = 20000
shared_subscription_policy = prefer_local
plugins.vmq_passwd = off
plugins.vmq_acl = on
plugins.vmq_diversity = off
plugins.vmq_webhooks = off
plugins.vmq_bridge = off
metadata_plugin = vmq_plumtree
vmq_acl.acl_file = /etc/vernemq/vmq.acl
vmq_acl.acl_reload_interval = 10
vmq_passwd.password_file = /etc/vernemq/vmq.passwd
vmq_passwd.password_reload_interval = 10
vmq_diversity.script_dir = /usr/share/vernemq/lua
vmq_diversity.auth_postgres.enabled = off
vmq_diversity.postgres.ssl = off
vmq_diversity.postgres.password_hash_method = crypt
vmq_diversity.auth_cockroachdb.enabled = off
vmq_diversity.cockroachdb.ssl = on
vmq_diversity.cockroachdb.password_hash_method = bcrypt
vmq_diversity.auth_mysql.enabled = off
vmq_diversity.mysql.password_hash_method = password
vmq_diversity.auth_mongodb.enabled = off
vmq_diversity.mongodb.ssl = off
vmq_diversity.auth_redis.enabled = off
vmq_bcrypt.pool_size = 1
log.console = file
log.console.level = info
log.console.file = /var/log/vernemq/console.log
log.error.file = /var/log/vernemq/error.log
log.syslog = off
log.crash = on
log.crash.file = /var/log/vernemq/crash.log
log.crash.maximum_message_size = 64KB
log.crash.size = 10MB
log.crash.rotation = $D0
log.crash.rotation.keep = 5
nodename = VerneMQ@127.0.0.1
distributed_cookie = vmq
erlang.async_threads = 64
erlang.max_ports = 262144
leveldb.maximum_memory.percent = 70
vmq_bridge.tcp.br0 = a--------------.iot.eu-east-1.amazonaws.com:8883
vmq_bridge.tcp.br0.try_private = off
vmq_bridge.tcp.br0.topic.1 = /mytopic/+ both 0
vmq_bridge.tcp.br0.cleansession = on
vmq_bridge.ssl.br0.cafile = /etc/vernemq/keys/ca.pem
vmq_bridge.ssl.br0.certfile = /etc/vernemq/keys/cert.crt
vmq_bridge.ssl.br0.keyfile =  /etc/vernemq/keys/private.pem
vmq_bridge.ssl.br0.insecure = off

客户端.py

import paho.mqtt.client as mqtt
import time

def on_connect(client, userdata, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("mytopic/image")

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))


client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("127.0.0.1", 1883, 60)

while True:
    client.publish("mytopic/image", "hellotest", qos=0) 
    time.sleep(60)

client.loop_forever()

有人可以指出我在这里做错了什么。我的最终目标是将这个 vernemq 代理与 AWS IoT 连接起来。谢谢!

4

2 回答 2

1

你永远不会到达, client.loop_forever()因为你永远不会while (True)在它之前退出循环。

更改client.loop_forever()client.start_loop()并将其移动到while (True)循环之前。

于 2020-01-28T22:19:16.303 回答
0

如果您在 VirtualBox 中运行并且脚本在主机中运行,您将无法访问 127.0.0.1 上的 VerneMQ,您需要获取 VM 的 IP 并通过以下方式连接到该 IP:

VBoxManage guestproperty get <vmname> "/VirtualBox/GuestInfo/Net/0/V4/IP"

参考https://www.virtualbox.org/manual/ch08.html

于 2020-05-06T06:59:57.947 回答