Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't detect disconnection on wifi websocket client #2368

Open
deunix-educ opened this issue Oct 22, 2017 · 6 comments
Open

Can't detect disconnection on wifi websocket client #2368

deunix-educ opened this issue Oct 22, 2017 · 6 comments
Labels

Comments

@deunix-educ
Copy link

Long story short

I use several esp8266 (a wifi IOT) as websocket client with aiohttp 2.3.1 websocket server .
Works perfectly but i can't detect disconnection.

Expected behaviour

I want to detect disconnection when an esp8266 disappear.

Actual behaviour

I detect connections and play with all clients ESP8266. The method WebSocketResponse::_pong_not_received detecst all disconnections at heartbeat delay but do nothing.
I there any solution?

Steps to reproduce

A background loop to send a ping to all client esp8266.
WebSocketResponse instance with heartbeat param set.

Your environment

Debian machine + Python 3.5 + aiohttp v 2.3.1

@fafhrd91
Copy link
Member

this seems like a bug.

@deunix-educ
Copy link
Author

Probably by sending a message from WebSocketResponse._pong_not_received (after test) to WebSocketResponse.receive loop. I don't know to do how to do.

@deunix-educ
Copy link
Author

deunix-educ commented Oct 25, 2017

I do not know if it's a good solution, but here is the code that allows me to detect the disconnection of all esp8266. :

import asyncio
from aiohttp import web

class WebSocketResponse(web.WebSocketResponse):
    def _pong_not_received(self):
        if self._req is not None and self._req.transport is not None:
            self._req.app.sockets.remove_ws(self) #  code that do the job

            self._closed = True
            self._close_code = 1006
            self._exception = asyncio.TimeoutError()
            self._req.transport.close()

@junqed
Copy link

junqed commented Jan 29, 2018

Have you tried like in close method

if self._waiting is not None and not self._closed:
?

I have the same thing — the pong callback doesn't stop reading loop :(

@junqed
Copy link

junqed commented Jan 29, 2018

BTW it happens not in a wifi network

@anatoly-kussul
Copy link

Had same problem on wired network, adding receive_timeout to ws_connect solved this problem for me.
But i think this works only if you have infinite receiving loop (you will get asyncio.TimeoutError on await ws.receive())
async with session.ws_connect(url, proxy=proxy_url, heartbeat=1, receive_timeout=5) as ws:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants