1
0
mirror of https://github.com/actix/examples synced 2024-11-28 00:12:57 +01:00
examples/websocket-chat-broker/client.py

81 lines
2.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2018-12-31 07:28:42 +01:00
"""websocket cmd client for actix/websocket-tcp-chat example."""
import argparse
import asyncio
import signal
import sys
import aiohttp
2018-12-31 07:28:42 +01:00
queue = asyncio.Queue()
2018-12-31 07:28:42 +01:00
async def start_client(url, loop):
name = input('Please enter your name: ')
2018-12-31 07:28:42 +01:00
ws = await aiohttp.ClientSession().ws_connect(url, autoclose=False, autoping=False)
def stdin_callback():
line = sys.stdin.buffer.readline().decode('utf-8')
if not line:
loop.stop()
else:
2018-12-31 07:28:42 +01:00
# Queue.put is a coroutine, so you can't call it directly.
asyncio.ensure_future(queue.put(ws.send_str(name + ': ' + line)))
2018-12-31 07:28:42 +01:00
loop.add_reader(sys.stdin, stdin_callback)
2018-12-31 07:28:42 +01:00
async def dispatch():
while True:
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:
2018-12-31 07:28:42 +01:00
await ws.pong()
elif msg.type == aiohttp.WSMsgType.PONG:
print('Pong received')
else:
if msg.type == aiohttp.WSMsgType.CLOSE:
2018-12-31 07:28:42 +01:00
await ws.close()
elif msg.type == aiohttp.WSMsgType.ERROR:
print('Error during receive %s' % ws.exception())
elif msg.type == aiohttp.WSMsgType.CLOSED:
pass
break
2018-12-31 07:28:42 +01:00
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(
description="websocket console client for wssrv.py example.")
ARGS.add_argument(
'--host', action="store", dest='host',
default='127.0.0.1', help='Host name')
ARGS.add_argument(
'--port', action="store", dest='port',
default=8080, type=int, help='Port number')
if __name__ == '__main__':
args = ARGS.parse_args()
if ':' in args.host:
args.host, port = args.host.split(':', 1)
args.port = int(port)
url = 'http://{}:{}/ws/'.format(args.host, args.port)
loop = asyncio.get_event_loop()
loop.add_signal_handler(signal.SIGINT, loop.stop)
2018-12-31 07:28:42 +01:00
asyncio.Task(main(url, loop))
loop.run_forever()