mirror of
https://github.com/actix/examples
synced 2024-11-23 14:31:07 +01:00
bug fix for websocket chat python file (#575)
This commit is contained in:
parent
0319f18995
commit
54c0bc0eb4
@ -4,15 +4,13 @@ import argparse
|
|||||||
import asyncio
|
import asyncio
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
|
async def start_client(loop, url):
|
||||||
def start_client(loop, url):
|
|
||||||
name = input('Please enter your name: ')
|
name = input('Please enter your name: ')
|
||||||
|
|
||||||
# send request
|
# 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
|
# input reader
|
||||||
def stdin_callback():
|
def stdin_callback():
|
||||||
@ -23,22 +21,21 @@ def start_client(loop, url):
|
|||||||
ws.send_str(name + ': ' + line)
|
ws.send_str(name + ': ' + line)
|
||||||
loop.add_reader(sys.stdin.fileno(), stdin_callback)
|
loop.add_reader(sys.stdin.fileno(), stdin_callback)
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def dispatch():
|
||||||
def dispatch():
|
|
||||||
while True:
|
while True:
|
||||||
msg = yield from ws.receive()
|
msg = await ws.receive()
|
||||||
|
|
||||||
if msg.type == aiohttp.WSMsgType.TEXT:
|
if msg.type == aiohttp.WSMsgType.TEXT:
|
||||||
print('Text: ', msg.data.strip())
|
print('Text: ', msg.data.strip())
|
||||||
elif msg.type == aiohttp.WSMsgType.BINARY:
|
elif msg.type == aiohttp.WSMsgType.BINARY:
|
||||||
print('Binary: ', msg.data)
|
print('Binary: ', msg.data)
|
||||||
elif msg.type == aiohttp.WSMsgType.PING:
|
elif msg.type == aiohttp.WSMsgType.PING:
|
||||||
ws.pong()
|
await ws.pong()
|
||||||
elif msg.type == aiohttp.WSMsgType.PONG:
|
elif msg.type == aiohttp.WSMsgType.PONG:
|
||||||
print('Pong received')
|
print('Pong received')
|
||||||
else:
|
else:
|
||||||
if msg.type == aiohttp.WSMsgType.CLOSE:
|
if msg.type == aiohttp.WSMsgType.CLOSE:
|
||||||
yield from ws.close()
|
await ws.close()
|
||||||
elif msg.type == aiohttp.WSMsgType.ERROR:
|
elif msg.type == aiohttp.WSMsgType.ERROR:
|
||||||
print('Error during receive %s' % ws.exception())
|
print('Error during receive %s' % ws.exception())
|
||||||
elif msg.type == aiohttp.WSMsgType.CLOSED:
|
elif msg.type == aiohttp.WSMsgType.CLOSED:
|
||||||
@ -46,7 +43,7 @@ def start_client(loop, url):
|
|||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
yield from dispatch()
|
await dispatch()
|
||||||
|
|
||||||
|
|
||||||
ARGS = argparse.ArgumentParser(
|
ARGS = argparse.ArgumentParser(
|
||||||
@ -64,9 +61,9 @@ if __name__ == '__main__':
|
|||||||
args.host, port = args.host.split(':', 1)
|
args.host, port = args.host.split(':', 1)
|
||||||
args.port = int(port)
|
args.port = int(port)
|
||||||
|
|
||||||
url = 'http://{}:{}/ws/'.format(args.host, args.port)
|
url = 'http://{}:{}/ws'.format(args.host, args.port)
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.add_signal_handler(signal.SIGINT, loop.stop)
|
loop.add_signal_handler(signal.SIGINT, loop.stop)
|
||||||
asyncio.Task(start_client(loop, url))
|
asyncio.Task(start_client(loop, url))
|
||||||
loop.run_forever()
|
loop.run_forever()
|
Loading…
Reference in New Issue
Block a user