This commit is contained in:
yumoqing 2021-11-25 10:13:32 +08:00
parent cc0504c44d
commit d886515b28
2 changed files with 6 additions and 1 deletions

View File

@ -67,6 +67,7 @@ class TopicPublisher:
self.url = "tcp://{}:{}".format(address, port) self.url = "tcp://{}:{}".format(address, port)
self.pub = self.context.socket(zmq.PUB) self.pub = self.context.socket(zmq.PUB)
self.pub.connect(self.url) self.pub.connect(self.url)
time.sleep(0.5)
def send(self, message): def send(self, message):
self.pub.send_multipart([self._topic, message.encode('utf-8')]) self.pub.send_multipart([self._topic, message.encode('utf-8')])
@ -95,7 +96,11 @@ class TopicSubscriber:
self.sub = self.context.socket(zmq.SUB) self.sub = self.context.socket(zmq.SUB)
self.sub.connect(self.url) self.sub.connect(self.url)
# subscribe to topic 'en' or 'jp' # subscribe to topic 'en' or 'jp'
self.sub.setsockopt(zmq.SUBSCRIBE, self._topic) if isinstance(self.topic, []):
for t in self.topic:
self.sub.setsockopt(zmq.SUBSCRIBE, t.encode('utf-8'))
else:
self.sub.setsockopt(zmq.SUBSCRIBE, self.topic.encode('utf-8')
def run(self): def run(self):

Binary file not shown.