Skip to content

Instantly share code, notes, and snippets.

@highfestiva
Last active November 7, 2017 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save highfestiva/3524da0c3b6824984b6acb22c33747e9 to your computer and use it in GitHub Desktop.
Save highfestiva/3524da0c3b6824984b6acb22c33747e9 to your computer and use it in GitHub Desktop.
CLI-based chat client in 14 LoC
#!/usr/bin/env python3
import asyncio
from threading import Thread
import websockets
loop = asyncio.get_event_loop()
def user_input(websocket):
while True:
message = input('>')
loop.call_soon_threadsafe(lambda: asyncio.ensure_future(websocket.send(message)))
async def chat_client(uri):
async with websockets.connect(uri) as websocket:
Thread(target=user_input, args=(websocket,)).start()
while True:
print(await websocket.recv())
loop.run_until_complete(chat_client('ws://localhost:5001/apa/bepa'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment