为什么MQTT连接不上服务器?
Why does MQTT not connect to the server?
我的目的是在我的 DJango 模型发送 postsave 信号时实现 MQTT 消息发布。我已经注册了 on_connect、断开连接时 on_publish 回调以检查相关进程何时执行。但是,none 我注册的回调有效。这是我的 post_save 信号代码。
有人可以给我提示我哪里做错了吗?
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("connecting")
print("client:"+str(client))
print("userdata:"+str(userdata))
print("flags:"+str(flags))
print("result:"+str(rc))
global flag_connected
flag_connected=1
def on_publish(client, userdata, mid):
print("message published")
def on_connect_fail(client, userdata, flags, rc):
print("failed to connect")
print("client:"+str(client))
print("userdata:"+str(userdata))
print("flags:"+str(flags))
print("result:"+str(rc))
global flag_connected
flag_connected=0
def on_disconnect(client, userdata, rc):
global flag_connected
flag_connected = 0
def notify_windowsApp(sender,instance, **kwargs):
new_truck_registered={
"identity":str(instance.truck_identity),
"doorName":str(instance.registered_door.door_name),
"doorNumber":str(instance.registered_door.door_number),
"entryTag":str(instance.entry_tag),
#"depatureTag":str(instance.departure_tag),
"entrytime":instance.entry_time.strftime('%Y-%m-%d::%H-%M'),
}
sendToClient(new_truck_registered)
def sendToClient(payloadTobeSent):
client=mqtt.Client(client_id="django_post_save_signal",
clean_session=True, )
mqttBroker="broker.hivemq.com"
client.on_connect=on_connect
client.on_publish=on_publish
client.on_connect_fail=on_connect_fail
client.on_disconnect=on_disconnect
client.connect(mqttBroker)
client.publish('baydoor/truckentrance',payload=str(payloadTobeSent),qos=1, retain=True)
首先,docs 中没有 on_connect_fail
回调,你从哪里得到的?
更重要的是,您还没有启动客户端 network loop 因此 none 的回调将永远被调用。
我的目的是在我的 DJango 模型发送 postsave 信号时实现 MQTT 消息发布。我已经注册了 on_connect、断开连接时 on_publish 回调以检查相关进程何时执行。但是,none 我注册的回调有效。这是我的 post_save 信号代码。 有人可以给我提示我哪里做错了吗?
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("connecting")
print("client:"+str(client))
print("userdata:"+str(userdata))
print("flags:"+str(flags))
print("result:"+str(rc))
global flag_connected
flag_connected=1
def on_publish(client, userdata, mid):
print("message published")
def on_connect_fail(client, userdata, flags, rc):
print("failed to connect")
print("client:"+str(client))
print("userdata:"+str(userdata))
print("flags:"+str(flags))
print("result:"+str(rc))
global flag_connected
flag_connected=0
def on_disconnect(client, userdata, rc):
global flag_connected
flag_connected = 0
def notify_windowsApp(sender,instance, **kwargs):
new_truck_registered={
"identity":str(instance.truck_identity),
"doorName":str(instance.registered_door.door_name),
"doorNumber":str(instance.registered_door.door_number),
"entryTag":str(instance.entry_tag),
#"depatureTag":str(instance.departure_tag),
"entrytime":instance.entry_time.strftime('%Y-%m-%d::%H-%M'),
}
sendToClient(new_truck_registered)
def sendToClient(payloadTobeSent):
client=mqtt.Client(client_id="django_post_save_signal",
clean_session=True, )
mqttBroker="broker.hivemq.com"
client.on_connect=on_connect
client.on_publish=on_publish
client.on_connect_fail=on_connect_fail
client.on_disconnect=on_disconnect
client.connect(mqttBroker)
client.publish('baydoor/truckentrance',payload=str(payloadTobeSent),qos=1, retain=True)
首先,docs 中没有 on_connect_fail
回调,你从哪里得到的?
更重要的是,您还没有启动客户端 network loop 因此 none 的回调将永远被调用。