From 3afe400bdf51ef922b8d0b105e63f74cefae390f Mon Sep 17 00:00:00 2001 From: Vane Date: Mon, 31 Dec 2018 07:28:42 +0100 Subject: [PATCH] Fix some issues at chat client (#71) --- websocket-chat-broker/client.py | 40 ++++++++++++++++++++------------- websocket-chat/client.py | 40 ++++++++++++++++++++------------- 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/websocket-chat-broker/client.py b/websocket-chat-broker/client.py index 8a1bd9ae..db8e241b 100755 --- a/websocket-chat-broker/client.py +++ b/websocket-chat-broker/client.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -"""websocket cmd client for wssrv.py example.""" +"""websocket cmd client for actix/websocket-tcp-chat example.""" import argparse import asyncio import signal @@ -7,46 +7,54 @@ import sys import aiohttp +queue = asyncio.Queue() -def start_client(loop, url): + +async def start_client(url, loop): name = input('Please enter your name: ') - # send request - ws = yield from aiohttp.ClientSession().ws_connect(url, autoclose=False, autoping=False) + ws = await aiohttp.ClientSession().ws_connect(url, autoclose=False, autoping=False) - # input reader def stdin_callback(): line = sys.stdin.buffer.readline().decode('utf-8') if not line: loop.stop() else: - ws.send_str(name + ': ' + line) - loop.add_reader(sys.stdin.fileno(), stdin_callback) + # Queue.put is a coroutine, so you can't call it directly. + asyncio.ensure_future(queue.put(ws.send_str(name + ': ' + line))) - @asyncio.coroutine - def dispatch(): + loop.add_reader(sys.stdin, stdin_callback) + + async def dispatch(): while True: - msg = yield from ws.receive() - + msg = await ws.receive() if msg.type == aiohttp.WSMsgType.TEXT: print('Text: ', msg.data.strip()) elif msg.type == aiohttp.WSMsgType.BINARY: print('Binary: ', msg.data) elif msg.type == aiohttp.WSMsgType.PING: - ws.pong() + await ws.pong() elif msg.type == aiohttp.WSMsgType.PONG: print('Pong received') else: if msg.type == aiohttp.WSMsgType.CLOSE: - yield from ws.close() + await ws.close() elif msg.type == aiohttp.WSMsgType.ERROR: print('Error during receive %s' % ws.exception()) elif msg.type == aiohttp.WSMsgType.CLOSED: pass - break - yield from dispatch() + await dispatch() + + +async def tick(): + while True: + await (await queue.get()) + + +async def main(url, loop): + await asyncio.wait([start_client(url, loop), tick()]) ARGS = argparse.ArgumentParser( @@ -68,5 +76,5 @@ if __name__ == '__main__': loop = asyncio.get_event_loop() loop.add_signal_handler(signal.SIGINT, loop.stop) - asyncio.Task(start_client(loop, url)) + asyncio.Task(main(url, loop)) loop.run_forever() diff --git a/websocket-chat/client.py b/websocket-chat/client.py index 8a1bd9ae..db8e241b 100755 --- a/websocket-chat/client.py +++ b/websocket-chat/client.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -"""websocket cmd client for wssrv.py example.""" +"""websocket cmd client for actix/websocket-tcp-chat example.""" import argparse import asyncio import signal @@ -7,46 +7,54 @@ import sys import aiohttp +queue = asyncio.Queue() -def start_client(loop, url): + +async def start_client(url, loop): name = input('Please enter your name: ') - # send request - ws = yield from aiohttp.ClientSession().ws_connect(url, autoclose=False, autoping=False) + ws = await aiohttp.ClientSession().ws_connect(url, autoclose=False, autoping=False) - # input reader def stdin_callback(): line = sys.stdin.buffer.readline().decode('utf-8') if not line: loop.stop() else: - ws.send_str(name + ': ' + line) - loop.add_reader(sys.stdin.fileno(), stdin_callback) + # Queue.put is a coroutine, so you can't call it directly. + asyncio.ensure_future(queue.put(ws.send_str(name + ': ' + line))) - @asyncio.coroutine - def dispatch(): + loop.add_reader(sys.stdin, stdin_callback) + + async def dispatch(): while True: - msg = yield from ws.receive() - + msg = await ws.receive() if msg.type == aiohttp.WSMsgType.TEXT: print('Text: ', msg.data.strip()) elif msg.type == aiohttp.WSMsgType.BINARY: print('Binary: ', msg.data) elif msg.type == aiohttp.WSMsgType.PING: - ws.pong() + await ws.pong() elif msg.type == aiohttp.WSMsgType.PONG: print('Pong received') else: if msg.type == aiohttp.WSMsgType.CLOSE: - yield from ws.close() + await ws.close() elif msg.type == aiohttp.WSMsgType.ERROR: print('Error during receive %s' % ws.exception()) elif msg.type == aiohttp.WSMsgType.CLOSED: pass - break - yield from dispatch() + await dispatch() + + +async def tick(): + while True: + await (await queue.get()) + + +async def main(url, loop): + await asyncio.wait([start_client(url, loop), tick()]) ARGS = argparse.ArgumentParser( @@ -68,5 +76,5 @@ if __name__ == '__main__': loop = asyncio.get_event_loop() loop.add_signal_handler(signal.SIGINT, loop.stop) - asyncio.Task(start_client(loop, url)) + asyncio.Task(main(url, loop)) loop.run_forever()