rabbitmq 为什么通配符主题(#,+)在Python paho MQTT Subscribe中不起作用?

h9vpoimq  于 2022-11-08  发布在  RabbitMQ
关注(0)|答案(1)|浏览(265)

我使用paho.mqtt,并将消息发送到主题test/data/user1
我的订阅客户端:

from paho.mqtt import client as mqtt_client

host = 'myhost.com'
port = 1883
topic = 'test/data/#'
auth = {
    'username': 'myuser',
    'password': 'mypass'
}
client_id = 'python-mqtt-1'

def connect_mqtt() -> mqtt_client:
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            print("Connected to MQTT Broker!")
        else:
            print("Failed to connect, return code %d\n", rc)

    client = mqtt_client.Client(client_id)
    client.username_pw_set(**auth)
    client.on_connect = on_connect
    client.connect(host, port)
    return client

def subscribe(client: mqtt_client):
    def on_message(client, userdata, msg):
        print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")

    client.subscribe(topic)
    client.on_message = on_message

def run():
    client = connect_mqtt()
    subscribe(client)
    client.loop_forever()

if __name__ == '__main__':
    run()

但是我用topic='test/data/#'没有得到我的消息。如果我用topic='test/data/user1',我得到我的消息。为什么通配符主题不起作用?
我的经纪人是RabbitMQ。

相关问题