使用 paho-mqtt python 未收到来自代理的预期消息
Not receiving expected message from the broker using paho-mqtt python
这是我的代码
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc): # The callback for when the client connects to the broker
print("Connected with result code {0}".format(str(rc))) # Print result of connection attempt
def on_message(client, userdata, message): # The callback for when a PUBLISH message is received from the server.
print("message received " ,str(message.payload.decode("utf-8")))
print("message topic=",message.topic)
print("message qos=",message.qos)
print("message retain flag=",message.retain)
#creating client instance
client = mqtt.Client(client_id="random_id_name")
print("client created ......")
client.on_connect = on_connect # Define callback function for successful connection
client.on_message = on_message # Define callback function for receipt of a message
#connecting to broker/server
hostname = "hostname_I_am_trying_to_connect" #give the host/server/broker name
portnumber = **random_port_number_as_integer_value** #give the port number
client.username_pw_set("username", "password") #give the username and password for the broker/server/host
client.connect(host= hostname ,port= portnumber)
print("client connected to- ",hostname," on port_number:",portnumber)
client.subscribe("login")
print("subscribed to the topic")
client.publish("login","some_message")
print("message published")
client.loop_forever() # Start networking daemon
这里我希望从经纪人那里收到 some_message/unique_id,即 1234/1234_qyehfj_1234_jfjfj。
相反,我收到了一些随机数。见截图:
这里有什么问题?是我的代码有问题还是我向其发送消息的代理有问题?
如果代码有问题,请告诉我如何解决这个问题。
我昨晚解决了这个问题。所以在这里我要解释发生了什么。
我订阅了错误的主题,这就是为什么我收到了其他人可能已经或可能未就该特定主题发布的所有内容。
以后,如果有人遇到同样的问题,请仔细检查您是否订阅了正确的主题。
这是我的代码
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc): # The callback for when the client connects to the broker
print("Connected with result code {0}".format(str(rc))) # Print result of connection attempt
def on_message(client, userdata, message): # The callback for when a PUBLISH message is received from the server.
print("message received " ,str(message.payload.decode("utf-8")))
print("message topic=",message.topic)
print("message qos=",message.qos)
print("message retain flag=",message.retain)
#creating client instance
client = mqtt.Client(client_id="random_id_name")
print("client created ......")
client.on_connect = on_connect # Define callback function for successful connection
client.on_message = on_message # Define callback function for receipt of a message
#connecting to broker/server
hostname = "hostname_I_am_trying_to_connect" #give the host/server/broker name
portnumber = **random_port_number_as_integer_value** #give the port number
client.username_pw_set("username", "password") #give the username and password for the broker/server/host
client.connect(host= hostname ,port= portnumber)
print("client connected to- ",hostname," on port_number:",portnumber)
client.subscribe("login")
print("subscribed to the topic")
client.publish("login","some_message")
print("message published")
client.loop_forever() # Start networking daemon
这里我希望从经纪人那里收到 some_message/unique_id,即 1234/1234_qyehfj_1234_jfjfj。
相反,我收到了一些随机数。见截图:
这里有什么问题?是我的代码有问题还是我向其发送消息的代理有问题?
如果代码有问题,请告诉我如何解决这个问题。
我昨晚解决了这个问题。所以在这里我要解释发生了什么。 我订阅了错误的主题,这就是为什么我收到了其他人可能已经或可能未就该特定主题发布的所有内容。
以后,如果有人遇到同样的问题,请仔细检查您是否订阅了正确的主题。