Created
February 6, 2019 21:29
-
-
Save ankitml/9e1c328c5bbe0a14e21cc5e0a32a299a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def beat1(websocket): | |
while True: | |
await asyncio.sleep(1) | |
await websocket.send('💚') | |
async def beat2(websocket): | |
while True: | |
await asyncio.sleep(2) | |
await websocket.send('💓') | |
async def active_ws_handler(websocket): | |
await asyncio.gather(beat1(websocket), beat2(websocket)) | |
async def reactive_ws_handler(websocket): | |
async for message in websocket: | |
if message.lower() == ‘ping’: | |
await websocket.send(‘pong’) | |
async def ws_handler(websocket, path): | |
await asyncio.gather( | |
active_ws_handler(websocket), | |
reactive_ws_handler(websocket), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment