简体   繁体   English

Paho MQTT “无法在套接字上接收:[Errno 32] Broken pipe”

[英]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...我设置了一个 Paho MQTT 客户端来接收消息,如下所示...

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.当我运行 PrimaryListener 文件并在另一个终端 window 中运行一个 Publisher 时,它运行良好。 When I try to run a second Publisher, the first Publisher logs the line of output....当我尝试运行第二个 Publisher 时,第一个 Publisher 记录了 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. MQTT 要求每个客户端都有一个唯一的客户端 ID。

The exact behavior for when a second client connects with the same ID is left up to the broker.当第二个客户端使用相同 ID 连接时的确切行为由代理决定。 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".您正在尝试使用相同的 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.如果您不关心具体的ID名称,您可以使用空白ID“”连接,并且经纪人将为您的客户分配一个随机名称。

This is exactly the problem I had and then fix made all the difference.这正是我遇到的问题,然后修复了一切。 Do not use 'client.connect()'不要使用'client.connect()'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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