2015-05-19 79 views
0

我訂閱郵件主題上的「OK /話題」對QoS = 2,但有時我沒有得到消息後的Qos = 2發佈,下面我訂閱的代碼:缺少訂閱的消息通過服務質量發佈= 2

def on_connect(client, userdata, flags, rc): 
    print("Connected with result code "+str(rc)) 
    client.subscribe("OK/Topic",2) 

def on_message(client, userdata, msg): 
    print(" Topic : "+str(msg.topic)+" and Message is : "+str(msg.payload)) 

def on_subscribe(client, userdata,mid, granted_qos): 
    print "userdata : " +str(userdata) 


strs="my-message-to-publish" 
mqttc = mqtt.Client("Python-MQTT-Pub-Sub") 
mqttc.on_connect = on_connect 
mqttc.on_message = on_message 
mqttc.on_subscribe = on_subscribe 
mqttc.connect("IP", PORT, 60) 
mqttc.start_loop() 
while True: 
    mqttc.publish("New/Topic",str(strs)) 
    print "publish message " + str(strs) 
    time.sleep(1) 

而且在我的主題發行代碼「OK /話題」如下:

def on_publish(client, userdata, mid): 
    client.disconnect() 

mqttc = mqtt.Client() 
mqttc.on_publish = on_publish 
mqttc.publish("OK/Topic","my_message",2) 

如果我發佈10消息,我會收到訂戶7-8消息,但不是10 有誰知道爲什麼我丟失了一些消息。

謝謝.. !!!

回答

1

調用on_publish方法中的斷開連接很可能會在完整的QOS2過程完成之前關閉連接。

而不是使用發佈和on_publish斷開嘗試使用單一的方法做全面的連接,發佈,清理在一個同步方法:

import paho.mqtt.publish as publish 

publish.single(topic, payload=None, qos=0, retain=False, hostname="localhost", port=1883, client_id="", keepalive=60, will=None, auth=None, tls=None,protocol=mqtt.MQTTv311) 
+0

感謝hardlib .. !!我嘗試通過更改協議版本「protocol = mqtt.MQTTv31」以上單一方法,因爲我的mosquitto是一個MQTT v3.1代理,但同樣我缺少一些發佈消息。任何線索.. –

+0

在客戶端mosquitto_sub -t'行/主題'我看到所有發佈消息在經紀人端收集,但訂閱者沒有訂閱所有消息... –

相關問題