3 from eventlet.green import socket, zmq
4 from eventlet.hubs import use_hub
7 ADDR = 'ipc:///tmp/chat'
15 socket = ctx.socket(zmq.SUB)
17 socket.setsockopt(zmq.SUBSCRIBE, "")
22 msg = socket.recv_pyobj()
23 str_msg = "%s: %s" % msg
31 def read_chat_forever(reader, pub_socket):
33 line = reader.readline()
36 print("Chat:", line.strip())
37 if line.startswith('name:'):
38 who = line.split(':')[-1].strip()
41 pub_socket.send_pyobj((who, line))
42 except socket.error as e:
43 # ignore broken pipes, they just mean the participant
44 # closed its connection already
47 line = reader.readline()
48 print("Participant left chat.")
51 print("ChatServer starting up on port %s" % PORT)
52 server = eventlet.listen(('0.0.0.0', PORT))
53 pub_socket = ctx.socket(zmq.PUB)
55 eventlet.spawn_n(publish,
58 new_connection, address = server.accept()
60 print("Participant joined chat.")
61 eventlet.spawn_n(publish,
62 new_connection.makefile('w'))
63 eventlet.spawn_n(read_chat_forever,
64 new_connection.makefile('r'),
66 except (KeyboardInterrupt, SystemExit):
67 print("ChatServer exiting.")