2017-01-05 183 views
1

我正在使用AWS上託管的mosquitto broker。我的客戶已經連接到經紀人,但一段時間後,它斷開連接。它發生很多次。MQTT:蚊子斷開連接

我傳遞QOS = 2,保留=真實,乾淨的會話=假,沒有用戶名和密碼,LWT =無,端口= 1883年,IP =服務器託管在AWS上,客戶端ID =時間標記(始終是唯一的)

這裏是我的代碼 -

import time 
import paho.mqtt.client as mqtt 

class MqttCommunication(object): 

    def __init__(self): 

     self.current_module = "Mqtt Communication class" 
     self.clientID = clientId 
     self.name = thread_name 

     self.DEBUG = True 
     self.MQTT_HOST = "" 
     self.MQTT_PORT = 1883 
     self.MQTT_USERNAME = "" 
     self.MQTT_PASSWORD = "" 
     self.MQTT_CLIENT_ID = self.clientID 
     self.MQTT_TOPIC = "" 
     self.MQTT_QOS = 0 
     self.MQTT_RETAIN = None 
     self.MQTT_CLEAN_SESSION = None 
     self.MQTT_LWT = "" 

     self.client = mqtt.Client(self.MQTT_CLIENT_ID,clean_session=self.MQTT_CLEAN_SESSION) 
     self.on_connect = None 
     self.on_disconnect = None 
     self.on_message = None 
     self.on_subscribe = None 
     self.on_unsubscribe = None 
     self.on_publish = None 
     self.client.on_connect = self.mqtt_on_connect 
     #self.client.on_message = self.mqtt_on_message 
     self.client.on_disconnect = self.mqtt_on_disconnect 
     self.client.on_subscribe = self.mqtt_on_subscribe 
     self.client.on_unsubscribe = self.mqtt_on_unsubscribe 
     self.client.on_publish = self.mqtt_on_publish 



    def connectHost(self,mqtt_host,mqtt_port,mqtt_username,mqtt_password): 
     self.MQTT_USERNAME = mqtt_username 
     self.MQTT_PASSWORD = mqtt_password 
     self.MQTT_HOST = mqtt_host 
     self.MQTT_PORT = mqtt_port 

     try: 
      self.client.username_pw_set(self.MQTT_USERNAME, self.MQTT_PASSWORD) 
      self.client.connect(self.MQTT_HOST,self.MQTT_PORT,60) 
      print self.MQTT_HOST 
      self.client.loop_start() 

     except Exception, e: 
      print "Error connecting to %s:%d: %s" % (mqtt_host, mqtt_port, str(e)) 

     return True 

    def disconnectHost(self): 
     self.client.disconnect() 
     return True 

    def mqttSettings(self,qos,mqtt_retain,mqtt_clean_session,mqtt_lwt): 
     self.MQTT_QOS = qos 
     self.MQTT_RETAIN = mqtt_retain 
     self.MQTT_CLEAN_SESSION = mqtt_clean_session 
     self.MQTT_LWT = mqtt_lwt 
     return True 

    def subscribeTopic(self,topic): 
     self.MQTT_TOPIC = topic 
     self.client.subscribe(self.MQTT_TOPIC, qos=self.MQTT_QOS) 
     return True 

    def unsubscribeTopic(self,topic): 
     self.client.unsubscribe(self.MQTT_TOPIC) 
     return True 

    def setClientId(self,clientID): 
     self.MQTT_CLIENT_ID= clientID 
     return True 

    def getClientId(self): 
     return self.MQTT_CLIENT_ID 

    def publishData(self,topic,message,qos): 
     self.client.publish(topic,message,qos) 
     return True 


    # The callback for when the client receives a CONNACK response from the server. 
    def mqtt_on_connect(self,client, userdata, flags, rc): 
     if rc == 0: 
      print "Connected to %s:%s" % (self.MQTT_HOST, self.MQTT_PORT) 
      time.sleep(3) 

     elif rc == 1: 
      print "Connection refused - unacceptable protocol version" 
     elif rc == 2: 
      print "Connection refused - identifier rejected" 
     elif rc == 3: 
      print "Connection refused - server unavailable" 
     elif rc == 4: 
      print "Connection refused - bad user name or password" 
     elif rc == 5: 
      print "Connection refused - not authorised" 
     else: 
      print "Connection failed - result code %d" % (rc) 



    # The callback for when a PUBLISH message is received from the server. 
    def mqtt_on_message(self , client, userdata, msg): 
     #print msg 
     print(msg.topic+" : "+str(msg.payload)) 

    def mqtt_on_disconnect(self, client, userdata, rc): 
     if rc != 0: 
      print("Unexpected disconnection.") 
     else: 
      print('hello from disconnect') 

    def mqtt_on_publish(self, client, userdata, mid): 
     """ 
     What to do when a message is published 
     """ 
     #print "publish" 


    def mqtt_on_subscribe(self,client, userdata, mid, granted_qos): 
     """ 
     What to do in the event of subscribing to a topic" 
     """ 
     #logging.debug("Subscribe with mid " + str(mid) + " received.") 


    def mqtt_on_unsubscribe(self, client, userdata, mid): 
     """ 
     What to do in the event of unsubscribing from a topic 
     """ 
     #logging.debug("Unsubscribe with mid " + str(mid) + " received.") 

分享一些日誌---

1483617475: New connection from xx.xx.xx.xx on port 1883. 
1483617475: New client connected from xx.xx.xx.xx as mqttjs_bd875699 (c1, k10$ 
1483617718: Saving in-memory database to /var/lib/mosquitto/mosquitto.db. 
1483618131: Client 2017-01-05 17:27:18.963994 has exceeded timeout, disconnecti$ 
1483618131: Socket error on client 2017-01-05 17:27:18.963994, disconnecting. 
1483618810: Socket error on client mqttjs_bd875699, disconnecting. 
1483618854: New connection from xx.xx.xx.xx on port 1883. 
1483618854: New client connected from xx.xx.xx.xx as mqttjs_7aa97fd9 (c1, k10$ 
1483618865: Socket error on client mqttjs_7aa97fd9, disconnecting. 
1483618866: New connection from xx.xx.xx.xx on port 1883. 
1483618866: New client connected from xx.xx.xx.xx as mqttjs_25e2f297 (c1, k10$ 
1483618886: New connection from xx.xx.xx.xx on port 1883. 
1483618886: New client connected from xx.xx.xx.xx as 2017-01-05 17:51:23.51980$ 
1483619018: Socket error on client mqttjs_25e2f297, disconnecting. 
1483619019: New connection from xx.xx.xx.xx on port 1883. 
1483619019: New client connected from xx.xx.xx.xx as mqttjs_1c8ec6dd (c1, k10$ 
1483619023: Socket error on client mqttjs_1c8ec6dd, disconnecting. 
1483619024: New connection from xx.xx.xx.xx on port 1883. 
+0

這些日誌只能從Python代碼顯示一個斷開,其餘都來自什麼看起來像一個過程的NodeJS – hardillb

回答

2

您需要啓動MQTT客戶端網絡環路,因此它可以處理保持活動ping數據包。

您可以通過添加下面一行在後臺啓動循環:

... 
def connectHost(self,mqtt_host,mqtt_port,mqtt_username,mqtt_password): 
    self.MQTT_USERNAME = mqtt_username 
    self.MQTT_PASSWORD = mqtt_password 
    self.MQTT_HOST = mqtt_host 
    self.MQTT_PORT = mqtt_port 

    try: 
     self.client.username_pw_set(self.MQTT_USERNAME, self.MQTT_PASSWORD) 
     print self.client.connect(self.MQTT_HOST,self.MQTT_PORT,60) 
     self.client.loop_start() # This line 
     print self.MQTT_HOST 

    except Exception, e: 
     print "Error connecting to %s:%d: %s" % (mqtt_host, mqtt_port, str(e)) 

    return True 

您可以通過以下停止循環:

def disconnectHost(self): 
    self.client.disconnect() 
    self.client.loop_stop() # This line 
    return True 
+0

對不起,我忘了添加代碼。我正在嘗試使用loop_start(),但我又遇到了同樣的問題..如果你幫我看看日誌說了什麼,爲什麼它會斷開連接 –

+0

它正在斷開連接,因爲客戶端在keepalive時段內沒有發送數據包。正如我所說這是因爲網絡循環沒有運行 – hardillb

0

你不顯示整個代碼。 尤其是,您不會顯示您要進入loop_forever的哪個位置,也不會顯示阻止您的代碼的其他方式。 當我補充說,我上mosquitto -v輸出(我的clientId是PAHO):

1483629832: New connection from 192.9.200.14 on port 1883. 
1483629832: New client connected from 192.9.200.14 as PAHO (c0, k60). 
1483629832: Sending CONNACK to PAHO (0, 0) 
1483629895: Received PINGREQ from PAHO 
1483629895: Sending PINGRESP to PAHO 
+0

你不需要阻止,'client.start_loop()'會啓動一個單獨的線程來運行循環 – hardillb