(loop, url)
| 7 | import aiohttp |
| 8 | |
| 9 | async def start_client(loop, url): |
| 10 | name = input('Please enter your name: ') |
| 11 | |
| 12 | # send request |
| 13 | ws = await aiohttp.ClientSession().ws_connect(url, autoclose=False, autoping=False) |
| 14 | |
| 15 | # input reader |
| 16 | def stdin_callback(): |
| 17 | line = sys.stdin.buffer.readline().decode('utf-8') |
| 18 | if not line: |
| 19 | loop.stop() |
| 20 | else: |
| 21 | ws.send_str(name + ': ' + line) |
| 22 | loop.add_reader(sys.stdin.fileno(), stdin_callback) |
| 23 | |
| 24 | async def dispatch(): |
| 25 | while True: |
| 26 | msg = await ws.receive() |
| 27 | |
| 28 | if msg.type == aiohttp.WSMsgType.TEXT: |
| 29 | print('Text: ', msg.data.strip()) |
| 30 | elif msg.type == aiohttp.WSMsgType.BINARY: |
| 31 | print('Binary: ', msg.data) |
| 32 | elif msg.type == aiohttp.WSMsgType.PING: |
| 33 | await ws.pong() |
| 34 | elif msg.type == aiohttp.WSMsgType.PONG: |
| 35 | print('Pong received') |
| 36 | else: |
| 37 | if msg.type == aiohttp.WSMsgType.CLOSE: |
| 38 | await ws.close() |
| 39 | elif msg.type == aiohttp.WSMsgType.ERROR: |
| 40 | print('Error during receive %s' % ws.exception()) |
| 41 | elif msg.type == aiohttp.WSMsgType.CLOSED: |
| 42 | pass |
| 43 | |
| 44 | break |
| 45 | |
| 46 | await dispatch() |
| 47 | |
| 48 | |
| 49 | ARGS = argparse.ArgumentParser( |
no test coverage detected