This works! WOO

This commit is contained in:
Brandon Thomas
2021-02-18 07:18:39 -05:00
parent e4680a2caf
commit 33a2230323
2 changed files with 36 additions and 8 deletions
+22 -4
View File
@@ -64,28 +64,46 @@ print('defining server', flush=True)
async def server(websocket, path):
print('subscribing to redis pubsub', flush=True)
queue = asyncio.Queue()
mpsc = Receiver(loop=loop)
async def reader(mpsc):
async for channel, msg in mpsc.iter():
assert isinstance(channel, AbcChannel)
print("Got {!r} in channel {!r}".format(msg, channel))
#print('sending message over websocket we got from redis pubsub', flush=True)
#await websocket.send("Got message: {}".format(msg))
print('enqueuing websocket message', flush=True)
queue.put_nowait(msg)
asyncio.ensure_future(reader(mpsc))
redis_connection = await aioredis.create_connection(('localhost', 6379))
redis = await aioredis.create_redis_pool('redis://localhost')
await redis_connection.subscribe(mpsc.channel('channel:1'), mpsc.channel('channel:testing'))
await redis.subscribe(mpsc.channel('channel:1'), mpsc.channel('channel:testing'))
print('starting websocket server', flush=True)
try:
while True:
# Receive data from "the outside world"
message = await websocket.recv()
#message = await websocket.recv()
# Feed this data to the PUBLISH co-routine
#await publish_to_redis(message, path)
await asyncio.sleep(1)
print('sending ping', flush=True)
await websocket.send("Websocket ping")
if not queue.empty():
msg = queue.get_nowait() # TODO THROWS EXCEPTION
print('sending pubsub msg over websocket: {}'.format(msg), flush=True)
await websocket.send(msg)
else:
await asyncio.sleep(10)
except websockets.exceptions.ConnectionClosed:
print('Connection Closed!')