Created
March 30, 2021 01:20
-
-
Save ejdoh1/10104e41fef89062d6a609a2dbfd35b7 to your computer and use it in GitHub Desktop.
websocket-server-sender.py
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
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