Skip to content

Instantly share code, notes, and snippets.

@GitHub30
Last active May 28, 2022 04:33
Show Gist options
  • Save GitHub30/3200edfb70a537064e8df7dce84e1375 to your computer and use it in GitHub Desktop.
Save GitHub30/3200edfb70a537064e8df7dce84e1375 to your computer and use it in GitHub Desktop.
pip install websockets
#!/usr/bin/env python
import asyncio
import websockets
connected = set()
async def broadcast(websocket):
connected.add(websocket)
async for message in websocket:
websockets.broadcast(connected, message)
async def main():
async with websockets.serve(broadcast, "localhost", 8765):
await asyncio.Future() # run forever
asyncio.run(main())
echo '#!/usr/bin/env python
import asyncio
import websockets
connected = set()
async def broadcast(websocket):
connected.add(websocket)
async for message in websocket:
websockets.broadcast(connected, message)
async def main():
async with websockets.serve(broadcast, "localhost", 8765):
await asyncio.Future() # run forever
asyncio.run(main())' | python3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment