Skip to content

Instantly share code, notes, and snippets.

@ejdoh1
Created March 30, 2021 01:20
Show Gist options
  • Save ejdoh1/10104e41fef89062d6a609a2dbfd35b7 to your computer and use it in GitHub Desktop.
Save ejdoh1/10104e41fef89062d6a609a2dbfd35b7 to your computer and use it in GitHub Desktop.
websocket-server-sender.py
import asyncio
import websockets
import time
import json
async def sender(websocket, path):
print("Client connected on path:", path)
while True:
time.sleep(5)
m = json.dumps({'hello': 'world'})
print("sending message:", m)
await websocket.send(m)
start_server = websockets.serve(sender, "localhost", 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
# To run, first run: pip3 install websockets
# On the client, brew install websocat and run
# websocat ws://localhost:8765/hello/world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment