Reputation: 21
I was trying to understand how websocket with aiohttp works, so I run this code from docunentation https://docs.aiohttp.org/en/stable/web_quickstart.html#websockets :
async def websocket_handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
if msg.data == 'close':
await ws.close()
else:
await ws.send_str(msg.data + '/answer')
elif msg.type == aiohttp.WSMsgType.ERROR:
print('ws connection closed with exception %s' %
ws.exception())
print('websocket connection closed')
return ws
app = web.Application()
app.add_routes([web.get('/ws', websocket_handler)])
asyncio.run(web.run_app(app))
The result I examined with curl:
curl http://0.0.0.0:8080/ws
The message that I saw was:
No WebSocket UPGRADE hdr: None
Can "Upgrade" only to "WebSocket"
Firefox shows the same message.
What am I doing wrong? Maybe someone knows what is the problem?
Upvotes: 2
Views: 2175
Reputation: 904
For anyone still confused why they can't test the aiohttp websocket example given above with websocat
, it's worth noting that there is an issue with the current version (v1.11.0
) via
brew install websocat
that means that localhost
is not resolved properly.
It works fine if you substitute in 127.0.0.1
instead. For e.g:
websocat ws://127.0.0.1:8000/ws
Upvotes: 1