You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thanks for the very awesome lib! I just wrestled with the WebSockets quickstart docs for a long time and wanted to report a solution that worked for me in case others run into this:
async def websocket_handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
async for msg in ws:
if msg[0].value == 1: # TEXT according to here: https://github.com/aio-libs/aiohttp/blob/3a65252aa4c822056b408caa883fbdbf78311fa6/aiohttp/http_websocket.py#L41
if msg[1] == 'close':
await ws.close()
else:
await ws.send_str(msg[1] + '/answer')
elif msg[0].value == 102: # ERROR according to here: https://github.com/aio-libs/aiohttp/blob/3a65252aa4c822056b408caa883fbdbf78311fa6/aiohttp/http_websocket.py#L41
print('ws connection closed with exception %s' %
ws.exception())
print('websocket connection closed')
return ws
The way the docs are currently written led me to this error, I think because the msg attributes are accessed differently than described:
if msg.type == aiohttp.WSMsgType.TEXT:
NameError: name 'aiohttp' is not defined
Expected behaviour
Actual behaviour
Steps to reproduce
Your environment
aiohttp 3.4.2- server on localhost
python 3.6
The text was updated successfully, but these errors were encountered:
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new
Long story short
Thanks for the very awesome lib! I just wrestled with the WebSockets quickstart docs for a long time and wanted to report a solution that worked for me in case others run into this:
The way the docs are currently written led me to this error, I think because the
msg
attributes are accessed differently than described:Expected behaviour
Actual behaviour
Steps to reproduce
Your environment
aiohttp 3.4.2- server on localhost
python 3.6
The text was updated successfully, but these errors were encountered: