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
GitMate.io thinks the contributor most likely able to help you is @asvetlov.
Possibly related issues are #1814 (Close websocket connection when pong not received), #3052 (SSL with closed connections), #523 (Not all connections are closed (pending: 0)), #253 (Connection not closed when request is cancelled), and #15 (No way to close a response on a timeout).
You need to run a separate task to listen the WebSocket and handle the closing handshake.
server.py:
importasyncioimportaiohttp.webasyncdefwebsocket_handler(request):
print('Websocket connection starting')
ws=aiohttp.web.WebSocketResponse(heartbeat=1)
awaitws.prepare(request)
print('Websocket connection ready')
asyncdef_ws_listener() ->None:
asyncformsginws: # handles ping-pong and closing internally# What to do with unexpected messages?# It depends on you.passreader=asyncio.create_task(_ws_listener())
whilenotws.closed:
awaitws.send_json({'mesasge': 'test'})
awaitasyncio.sleep(1)
awaitreaderreturnwsdefmain():
app=aiohttp.web.Application()
app.router.add_route('GET', '/ws', websocket_handler)
aiohttp.web.run_app(app, host='0.0.0.0', port=8080)
if__name__=='__main__':
main()
client.py:
importasyncioimportaiohttpURL=f'http://0.0.0.0:8080/ws'asyncdefmain():
session=aiohttp.ClientSession()
asyncwithsession:
count=0max_query=2asyncwithsession.ws_connect(URL) asws:
whileTrue:
msg=awaitws.receive_json()
print(f"get from the server {msg}")
count+=1ifcount>max_query:
breakif__name__=='__main__':
asyncio.run(main())
Long story short
Cannot properly close ws connection from client side. Full description as code bellow
Expected behaviour
Good patter to close ws connection from the client side in one-sided way.
Actual behaviour
Need to use protected attribute to close ws connection
Steps to reproduce
server.py
client.py
Your environment
macOS Mojave 10.14
python3.6.1
aiohttp==3.5.0a1
The text was updated successfully, but these errors were encountered: