step 2.3: multiple topics
This commit is contained in:
parent
1044d6224a
commit
7685c36677
|
|
@ -1,6 +1,7 @@
|
||||||
import zmq
|
import zmq
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
import random
|
||||||
|
|
||||||
port = "5556"
|
port = "5556"
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
|
|
@ -15,12 +16,17 @@ socket = context.socket(zmq.PUB)
|
||||||
# We can now send messages
|
# We can now send messages
|
||||||
socket.bind(f"tcp://*:{port}")
|
socket.bind(f"tcp://*:{port}")
|
||||||
|
|
||||||
topic = "TIME"
|
topics = ('TIME', 'RANDOM')
|
||||||
|
messages = {}
|
||||||
while True:
|
while True:
|
||||||
# Time to publish the latest time!
|
# Time to publish the latest time!
|
||||||
messagedata = time.ctime()
|
messages['TIME'] = time.ctime()
|
||||||
|
messages['RANDOM'] = random.randint(1,10)
|
||||||
# Note the use of XXX_string here;
|
# Note the use of XXX_string here;
|
||||||
# the non-_stringy methods only work with bytes.
|
# the non-_stringy methods only work with bytes.
|
||||||
socket.send_string(f"{topic};{messagedata}")
|
for topic in topics:
|
||||||
print(f"Published topic {topic}: {messagedata}")
|
message = messages.get(topic, '')
|
||||||
|
if not message: continue
|
||||||
|
socket.send_string(f"{topic};{message}")
|
||||||
|
print(f"Published topic {topic}: {message}")
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,9 @@ print(f"Collecting updates from time server at tcp://localhost:{port}")
|
||||||
socket.connect(f"tcp://{ip}:{port}")
|
socket.connect(f"tcp://{ip}:{port}")
|
||||||
|
|
||||||
# Filter by topic
|
# Filter by topic
|
||||||
topicfilter = "TIME"
|
topicfilters = ("TIME", "RANDOM")
|
||||||
socket.setsockopt_string(zmq.SUBSCRIBE, topicfilter)
|
socket.setsockopt_string(zmq.SUBSCRIBE, topicfilters[0])
|
||||||
|
socket.setsockopt_string(zmq.SUBSCRIBE, topicfilters[1])
|
||||||
|
|
||||||
# Process 5 updates
|
# Process 5 updates
|
||||||
topic_list = []
|
topic_list = []
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue