2017-10-15 231 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()