2017-10-15 207 views
0

我有以下代码为mqtt客户端从代理接收消息。 如果客户端与代理断开连接,则客户端尝试再次使用connect()调用与代理进行连接。我已阅读paho文档说loop_start()将处理与经纪人的重新连接。请让我知道是否正确使用connect()呼叫与经纪人重新连接,或让其自行处理loop_start()问题与mqtt客户端

import paho.mqtt.client as mqtt 
import json 
import time 

is_connect = False 

# 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("#") 

# The callback for when a PUBLISH message is received from the server. 
def on_message(client, userdata, msg): 
    print("Message received on "+msg.topic) 
    parsed_message = json.loads(msg.payload) 
    print(json.dumps(parsed_message, indent=4, sort_keys=True)) 

def on_disconnect(client, userdata, rc): 

    if rc != 0: 

     print "Unexpected disconnection." , (rc) 
     global is_connect 
     is_connect = False 
while True: 
    global is_connect 
    #If client is not connected, initiate connection again 
    if not is_connect: 
     try: 
      client = mqtt.Client(client_id='testing_purpose', clean_session=False) 
      client.loop_stop() 
      client.on_connect = on_connect 
      client.on_message = on_message 
      client.on_disconnect = on_disconnect 
      client.connect("localhost", 1883, 5) 
      is_connect = True 
      client.loop_start() 
      time.sleep(15) 
     except Exception as err: 
      is_connect = False 


# 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() 
+0

有没有缝这里是一个实际的问题。你有没有试过这段代码来看看会发生什么? – hardillb

+0

话虽如此,你可能想将'is_connect = True'移动到'on_connect'方法 – hardillb

+0

@hardillb这段代码工作正常。但我有一个疑问,我正在创建每个断开连接调用的mqtt客户端的对象。它会与mqtt经纪人产生任何问题。另外我会将is_connect = True移动到on_connect() – cgoma

回答

0

好的,所以如果您使用loop_forever功能,Paho客户端只会重新连接。

因此,如果您想要控制网络环路,不必拆除整个连接,而是可以使用reconnect功能重新连接。但最简单的方法是只使用loop_forever功能如下:

import paho.mqtt.client as mqtt 
import json 

# 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("#") 

# The callback for when a PUBLISH message is received from the server. 
def on_message(client, userdata, msg): 
    print("Message received on "+msg.topic) 
    parsed_message = json.loads(msg.payload) 
    print(json.dumps(parsed_message, indent=4, sort_keys=True)) 

def on_disconnect(client, userdata, rc): 
    if rc != 0: 
     print "Unexpected disconnection." , (rc) 

# 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 = mqtt.Client(client_id='testing_purpose', clean_session=False) 
client.on_connect = on_connect 
client.on_message = on_message 
client.on_disconnect = on_disconnect 
client.loop_forever()