如何为PUBACK和PUBREL生成数据包,最重要的是,如何知道接收到的消息的packetId是什么?在
我希望自己能够处理PUBREL和PUBACKs的逻辑。下面的代码应该会提示我要做什么。在import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
from multiprocessing import Process, Queue
# The callback for when the client receives a CONNACK response from
the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("/test")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
def my_function(id):
client = mqtt.Client(client_id="id", clean_session=False,userdata=None, protocol=MQTTv311)
client.on_connect = on_connect
client.on_message = on_message
client.connect("127.0.0.1", 1883, 60)
msgs = [{'topic':"/test", 'payload':"ClientId: " +id }, 'QoS': 2, 'retain': False),
{'topic':"/test", 'payload':"ClientId: " +id }, 'QoS': 2, 'retain': False),
{'topic':"/test", 'payload':"ClientId: " +id }, 'QoS': 2, 'retain': False)]
publish.multiple(msgs, hostname="127.0.0.1", port=1883,client_id=id, keepalive=60,will=None, auth=None, tls=None, protocol=mqtt.MQTTv311)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
for i in range(0,10):
p = Process(target=my_function, args=(str(i))