Last active
November 7, 2017 10:16
-
-
Save highfestiva/3524da0c3b6824984b6acb22c33747e9 to your computer and use it in GitHub Desktop.
CLI-based chat client in 14 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 | |
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