-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzeromq.py
35 lines (25 loc) · 978 Bytes
/
zeromq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/home/debain/gpslib/env/bin/python
import asyncio
import websockets
import zmq
import zmq.asyncio
from config import IPADDRESS, PORT
async def handler(websocket):
await websocket.send('test reply')
class ZeroPubSub:
def __init__(self, subscribe_address):
self.context = zmq.asyncio.Context()
self.subscribe_socket = self.context.socket(zmq.SUB)
self.subscribe_socket.bind(subscribe_address)
self.subscribe_socket.setsockopt(zmq.SUBSCRIBE, b"")
async def start(self):
while True:
with await self.subscribe_socket.recv() as message:
print(message)
# await self.websocketserver.send(message)
if __name__ == "__main__":
pubsub = ZeroPubSub(f"tcp://{IPADDRESS}:{PORT}")
asyncio.run(pubsub.start())
start_server = websockets.serve(handler, "localhost", 8000)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()