Skip to content

Commit

Permalink
Fix websocket send command being a coroutine (#1886)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored and asvetlov committed May 13, 2017
1 parent c80c201 commit 127c451
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 78 deletions.
18 changes: 9 additions & 9 deletions demos/chat/aiohttpdemo_chat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ async def index(request):
name = (random.choice(string.ascii_uppercase) +
''.join(random.sample(string.ascii_lowercase*10, 10)))
log.info('%s joined.', name)
resp.send_str(json.dumps({'action': 'connect',
'name': name}))
await resp.send_str(json.dumps({'action': 'connect',
'name': name}))
for ws in request.app['sockets'].values():
ws.send_str(json.dumps({'action': 'join',
'name': name}))
await ws.send_str(json.dumps({'action': 'join',
'name': name}))
request.app['sockets'][name] = resp

while True:
Expand All @@ -32,17 +32,17 @@ async def index(request):
if msg.type == web.MsgType.text:
for ws in request.app['sockets'].values():
if ws is not resp:
ws.send_str(json.dumps({'action': 'sent',
'name': name,
'text': msg.data}))
await ws.send_str(json.dumps({'action': 'sent',
'name': name,
'text': msg.data}))
else:
break

del request.app['sockets'][name]
log.info('%s disconnected.', name)
for ws in request.app['sockets'].values():
ws.send_str(json.dumps({'action': 'disconnect',
'name': name}))
await ws.send_str(json.dumps({'action': 'disconnect',
'name': name}))
return resp


Expand Down
2 changes: 1 addition & 1 deletion docs/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ methods::
await ws.close()
break
else:
ws.send_str(msg.data + '/answer')
await ws.send_str(msg.data + '/answer')
elif msg.type == aiohttp.WSMsgType.CLOSED:
break
elif msg.type == aiohttp.WSMsgType.ERROR:
Expand Down
2 changes: 1 addition & 1 deletion docs/web.rst
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ with the peer::
if msg.data == 'close':
await ws.close()
else:
ws.send_str(msg.data + '/answer')
await ws.send_str(msg.data + '/answer')
elif msg.type == aiohttp.WSMsgType.ERROR:
print('ws connection closed with exception %s' %
ws.exception())
Expand Down
2 changes: 1 addition & 1 deletion examples/background_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def listen_to_redis(app):
async for msg in ch.iter(encoding='utf-8'):
# Forward message to all connected websockets:
for ws in app['websockets']:
ws.send_str('{}: {}'.format(ch.name, msg))
await ws.send_str('{}: {}'.format(ch.name, msg))
print("message in {}: {}".format(ch.name, msg))
except asyncio.CancelledError:
pass
Expand Down
6 changes: 3 additions & 3 deletions examples/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ async def wshandler(request):
try:
print('Someone joined.')
for ws in request.app['sockets']:
ws.send_str('Someone joined')
await ws.send_str('Someone joined')
request.app['sockets'].append(resp)

async for msg in resp:
if msg.type == WSMsgType.TEXT:
for ws in request.app['sockets']:
if ws is not resp:
ws.send_str(msg.data)
await ws.send_str(msg.data)
else:
return resp
return resp
Expand All @@ -39,7 +39,7 @@ async def wshandler(request):
request.app['sockets'].remove(resp)
print('Someone disconnected.')
for ws in request.app['sockets']:
ws.send_str('Someone disconnected.')
await ws.send_str('Someone disconnected.')


async def on_shutdown(app):
Expand Down
4 changes: 2 additions & 2 deletions tests/autobahn/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def client(loop, url, name):
msg = yield from ws.receive()

if msg.type == aiohttp.WSMsgType.text:
ws.send_str(msg.data)
yield from ws.send_str(msg.data)
elif msg.type == aiohttp.WSMsgType.binary:
ws.send_bytes(msg.data)
yield from ws.send_bytes(msg.data)
elif msg.type == aiohttp.WSMsgType.close:
yield from ws.close()
break
Expand Down
54 changes: 27 additions & 27 deletions tests/test_client_ws_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def handler(request):
yield from ws.prepare(request)

msg = yield from ws.receive_str()
ws.send_str(msg+'/answer')
yield from ws.send_str(msg+'/answer')
yield from ws.close()
return ws

app = web.Application()
app.router.add_route('GET', '/', handler)
client = yield from test_client(app)
resp = yield from client.ws_connect('/')
resp.send_str('ask')
yield from resp.send_str('ask')

assert resp.get_extra_info('socket') is not None

Expand All @@ -51,15 +51,15 @@ def handler(request):
yield from ws.prepare(request)

msg = yield from ws.receive_str()
ws.send_str(msg+'/answer')
yield from ws.send_str(msg+'/answer')
yield from ws.close()
return ws

app = web.Application()
app.router.add_route('GET', '/', handler)
client = yield from test_client(app)
resp = yield from client.ws_connect('/')
resp.send_str('ask')
yield from resp.send_str('ask')

with pytest.raises(TypeError):
yield from resp.receive_bytes()
Expand All @@ -75,7 +75,7 @@ def handler(request):
yield from ws.prepare(request)

msg = yield from ws.receive_bytes()
ws.send_bytes(msg+b'/answer')
yield from ws.send_bytes(msg+b'/answer')
yield from ws.close()
return ws

Expand All @@ -84,7 +84,7 @@ def handler(request):
client = yield from test_client(app)
resp = yield from client.ws_connect('/')

resp.send_bytes(b'ask')
yield from resp.send_bytes(b'ask')

data = yield from resp.receive_bytes()
assert data == b'ask/answer'
Expand All @@ -101,7 +101,7 @@ def handler(request):
yield from ws.prepare(request)

msg = yield from ws.receive_bytes()
ws.send_bytes(msg+b'/answer')
yield from ws.send_bytes(msg+b'/answer')
yield from ws.close()
return ws

Expand All @@ -110,7 +110,7 @@ def handler(request):
client = yield from test_client(app)
resp = yield from client.ws_connect('/')

resp.send_bytes(b'ask')
yield from resp.send_bytes(b'ask')

with pytest.raises(TypeError):
yield from resp.receive_str()
Expand All @@ -127,7 +127,7 @@ def handler(request):
yield from ws.prepare(request)

data = yield from ws.receive_json()
ws.send_json({'response': data['request']})
yield from ws.send_json({'response': data['request']})
yield from ws.close()
return ws

Expand Down Expand Up @@ -155,7 +155,7 @@ def handler(request):

msg = yield from ws.receive_bytes()
ws.ping()
ws.send_bytes(msg+b'/answer')
yield from ws.send_bytes(msg+b'/answer')
try:
yield from ws.close()
finally:
Expand All @@ -168,7 +168,7 @@ def handler(request):
resp = yield from client.ws_connect('/')

resp.ping()
resp.send_bytes(b'ask')
yield from resp.send_bytes(b'ask')

msg = yield from resp.receive()
assert msg.type == aiohttp.WSMsgType.BINARY
Expand All @@ -193,7 +193,7 @@ def handler(request):

msg = yield from ws.receive_bytes()
ws.ping()
ws.send_bytes(msg+b'/answer')
yield from ws.send_bytes(msg+b'/answer')
try:
yield from ws.close()
finally:
Expand All @@ -206,7 +206,7 @@ def handler(request):
resp = yield from client.ws_connect('/', autoping=False)

resp.ping()
resp.send_bytes(b'ask')
yield from resp.send_bytes(b'ask')

msg = yield from resp.receive()
assert msg.type == aiohttp.WSMsgType.PONG
Expand All @@ -233,7 +233,7 @@ def handler(request):
yield from ws.prepare(request)

yield from ws.receive_bytes()
ws.send_str('test')
yield from ws.send_str('test')

yield from ws.receive()
return ws
Expand All @@ -243,7 +243,7 @@ def handler(request):
client = yield from test_client(app)
resp = yield from client.ws_connect('/')

resp.send_bytes(b'ask')
yield from resp.send_bytes(b'ask')

closed = yield from resp.close()
assert closed
Expand All @@ -265,7 +265,7 @@ def handler(request):
yield from ws.prepare(request)

yield from ws.receive_bytes()
ws.send_str('test')
yield from ws.send_str('test')

yield from client_ws.close()

Expand All @@ -278,7 +278,7 @@ def handler(request):
client = yield from test_client(app)
ws = client_ws = yield from client.ws_connect('/')

ws.send_bytes(b'ask')
yield from ws.send_bytes(b'ask')

msg = yield from ws.receive()
assert msg.type == aiohttp.WSMsgType.CLOSING
Expand Down Expand Up @@ -310,7 +310,7 @@ def handler(request):
client = yield from test_client(app)
resp = yield from client.ws_connect('/')

resp.send_bytes(b'ask')
yield from resp.send_bytes(b'ask')

msg = yield from resp.receive()
assert msg.type == aiohttp.WSMsgType.CLOSE
Expand All @@ -333,7 +333,7 @@ def handler(request):
yield from ws.prepare(request)

yield from ws.receive_bytes()
ws.send_str('test')
yield from ws.send_str('test')

try:
yield from ws.close()
Expand All @@ -345,7 +345,7 @@ def handler(request):
app.router.add_route('GET', '/', handler)
client = yield from test_client(app)
resp = yield from client.ws_connect('/', autoclose=False)
resp.send_bytes(b'ask')
yield from resp.send_bytes(b'ask')

msg = yield from resp.receive()
assert msg.data == 'test'
Expand All @@ -369,7 +369,7 @@ def handler(request):
ws = web.WebSocketResponse()
yield from ws.prepare(request)
yield from ws.receive_bytes()
ws.send_str('test')
yield from ws.send_str('test')
yield from asyncio.sleep(1, loop=loop)
return ws

Expand All @@ -378,7 +378,7 @@ def handler(request):
client = yield from test_client(app)
resp = yield from client.ws_connect('/', timeout=0.2, autoclose=False)

resp.send_bytes(b'ask')
yield from resp.send_bytes(b'ask')

msg = yield from resp.receive()
assert msg.data == 'test'
Expand All @@ -397,15 +397,15 @@ def handler(request):
ws = web.WebSocketResponse()
yield from ws.prepare(request)
yield from ws.receive_bytes()
ws.send_str('test')
yield from ws.send_str('test')
yield from asyncio.sleep(10, loop=loop)

app = web.Application()
app.router.add_route('GET', '/', handler)
client = yield from test_client(app)
resp = yield from client.ws_connect('/', autoclose=False)

resp.send_bytes(b'ask')
yield from resp.send_bytes(b'ask')

text = yield from resp.receive()
assert text.data == 'test'
Expand Down Expand Up @@ -449,7 +449,7 @@ def handler(request):
ws = web.WebSocketResponse()
yield from ws.prepare(request)

ws.send_str('answer')
yield from ws.send_str('answer')
yield from ws.close()
return ws

Expand Down Expand Up @@ -479,7 +479,7 @@ def handler(request):
app.router.add_route('GET', '/', handler)
client = yield from test_client(app)
resp = yield from client.ws_connect('/')
resp.send_str('ask')
yield from resp.send_str('ask')

msg = yield from resp.receive()
assert msg.type == aiohttp.WSMsgType.ERROR
Expand Down Expand Up @@ -508,7 +508,7 @@ def handler(request):
app.router.add_route('GET', '/', handler)
client = yield from test_client(app)
resp = yield from client.ws_connect('/')
resp.send_str('ask')
yield from resp.send_str('ask')

with pytest.raises(asyncio.TimeoutError):
with aiohttp.Timeout(0.01, loop=app.loop):
Expand Down
Loading

0 comments on commit 127c451

Please sign in to comment.