简体   繁体   中英

Paho MQTT “failed to receive on socket: [Errno 32] Broken pipe”

I have an Paho MQTT client set up to receive messages, which looks like the code below...

class PrimaryListener:

    def __init__(self):
        self.client = mqtt.Client("paho-test-subscriber", False)
        self.client.on_connect= self.on_connect 
        self.client.on_message= self.on_message   
        self.client.on_disconnect = self.on_disconnect     

        self.client.connect(msg_defn.broker_ip, msg_defn.broker_port)
        self.client.subscribe("test-topic")

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

    def on_message(self, client, userdata, message):
        msg_str = message.payload.decode("utf-8")
        print("message received : " , str(msg_str))
        print("message topic : ", message.topic)

    def on_disconnect(self, client, userdata,rc=0):
        self.client.loop_stop()


    def subscribe(self): 
        self.client.loop_forever()

if __name__ == "__main__":
    primaryListener = PrimaryListener()
    primaryListener.subscribe()

each of the publishers look like this, trying to send messages every ten seconds...

class Publisher:

    def __init__(self):
        self.client = mqtt.Client("paho-test-publisher", False)
        self.client.on_log = self.on_log  
        self.client.on_connect = self.on_connect 
        self.client.connect(msg_defn.broker_ip, msg_defn.broker_port)
        self.publishing_increment = 7 

    def on_log(self,client, userdata, level, buf):
        print("log: ", buf)

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

    def send_message(self, log_str):
        file_dict = json.loads(log_str)
        for item in file_dict: 
            self.client.publish("test-topic", json.dumps(item))   
            time.sleep(self.publishing_increment)

    def publishFromFile(self,file_name):
        with open(file_name, "r") as jsonfile:
            file_str = jsonfile.read()
        file_dict = json.loads(file_str)
        numStr = str(randint(0, 10))
        while True:
            for item in file_dict: 
                self.client.publish("test-topic", numStr)  #json.dumps(item))   
                time.sleep(self.publishing_increment)
            time.sleep(10)

if __name__ == "__main__":
    publisher = Publisher()
    publisher.publishFromFile("disconnected_test.txt")

I am trying to test these out. When I run the PrimaryListener file and run one Publisher in another terminal window, it runs fine. When I try to run a second Publisher, the first Publisher logs the line of output....

log:  failed to receive on socket: [Errno 32] Broken pipe

And then discontinues to send messages. What am I doing wrong here?

MQTT requires each client to have a unique client ID.

The exact behavior for when a second client connects with the same ID is left up to the broker. What you're seeing is a common one: the broker kicks off the first one and accept the connection from the second one.

You're trying to connect both publishers with the same ID: "paho-test-publisher".

If you don't care about the specific ID name, you can connect with a blank ID, "", and the broker will assign a random name for your client.

This is exactly the problem I had and then fix made all the difference. Do not use 'client.connect()'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM