在MQTT代理中发布后,客户端断开连接

时间:2019-11-05 04:16:59

标签: python mqtt paho messagebroker mosca

我已经启动并运行了MQTT代理(Mosca)。

我正在使用paho-mqtt从Python连接到它。当我的客户端连接数大约为5-6时,一切都很好,只有几次客户端断开连接发生了,但是当客户端数量增加时,断开连接的频率也增加了。尤其是当客户端持续发布时,几次客户端在发布后都会断开连接。有人可以帮我解决这个问题吗?

请让我知道是否需要其他信息?

def on_connect(client, userdata, flags, rc):
    if rc==0:
        print("connected OK Returned code=",rc)
    else:
        print("Bad connection Returned code=",rc)

    print("Subscribing to topic","data/#")
    client.subscribe("data/#")


def on_disconnect(client, userdata, rc):
   print("Client Got Disconnected")
   if rc != 0:
       print('Unexpected MQTT disconnection. Will auto-reconnect')

   else:
       print('rc value:' + str(rc))

broker_address="ip"
port = 'port'
print("creating new instance")
client = mqtt.Client(clean_session=True) #create new instance
client.on_connect = on_connect
client.on_message = on_message #attach function to callback
client.on_disconnect = on_disconnect

print("connecting to broker")
client.connect(broker_address, port=port,) #connect to broker
client.loop_forever() #stop the loop

0 个答案:

没有答案