无法将 paho mqtt 连接到在 oracle virtualbox 上运行的 vernemq



我正在尝试将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

client.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 联系起来。谢谢!

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

client.loop_forever()更改为client.start_loop(),并在while (True)循环之前移动它。

如果您在 VirtualBox 和主机中的脚本中运行,您将无法访问 127.0.0.1 上的 VerneMQ,您需要获取虚拟机的 IP 并使用以下命令连接到该 IP:

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

请参阅 https://www.virtualbox.org/manual/ch08.html

相关内容

  • 没有找到相关文章

最新更新