Last active
November 7, 2017 10:16
-
-
Save highfestiva/f4048aff880e0c952e47a68a4f690455 to your computer and use it in GitHub Desktop.
Chat server in 13 LoC
This file contains 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
#!/usr/bin/env python3 | |
import asyncio | |
import websockets | |
connections = set() | |
async def broadcast(websocket, path): | |
connections.add(websocket) | |
try: | |
async for message in websocket: | |
print(message) | |
await asyncio.wait([w.send(message) for w in connections]) | |
finally: | |
connections.remove(websocket) | |
asyncio.get_event_loop().run_until_complete(websockets.serve(broadcast, '0.0.0.0', 5001)) | |
asyncio.get_event_loop().run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment