connection is lost.
chalyyzhu opened this issue · comments
can't keep alive conection..
Will take a look on this. Do you have the code to reproduce the problem?
async def keepAlive(self, interval):
current = datetime.datetime.now()
if int(current.strftime("%S")) == self.timeLoop:
if self.websocket is not None:
await self.websocket.send('?,,')
print("KEEP ALIVE..")
greeting = await self.websocket.recv()
print(f"< {greeting}")
sec = current + datetime.timedelta(seconds = interval)
self.timeLoop = int(sec.strftime("%S"))
if not self.timeLoop:
sec = current + datetime.timedelta(seconds = interval)
self.timeLoop = int(sec.strftime("%S"))
def _start_receiver(self) -> None:
async def receiver():
while True:
await self.keepAlive(self.interval)
if not self.websocket or not self.websocket.open:
logger.debug("receiver returned: no ws/connection closed")
return
if not self.websocket.messages or self.websocket.closed:
await asyncio.sleep(0)
continue
raw_message = self.websocket.messages.pop()
try:
message = WebsocketMessage.unserialize(
raw_message, self.get_keys())
except Exception as exc: # pylint: disable=broad-except
logger.warning("Ignored error decoding message: %s", exc)
await asyncio.sleep(0)
continue
if message:
logger.debug("Received WS message with tag %s",
message.tag)
#print("MESSAGE : ", message.data)
self.messages.add(message.tag, message.data)
self.loopCon = asyncio.ensure_future(receiver())
logger.debug("Executed receiver coroutine")
i see every few seconds whatsapp pings "?,," . to keep the connection alive
What software did you use to analyze the websocket traffic? I think I am interested to use that one too.
Your keep alive code is similar https://github.com/Rhymen/go-whatsapp/blob/c58e164e05b80aeaa6c96752c0ce5f8ec5174d40/write.go#L101
I am still not sure what causes this behavior. Let me ponder it for a moment.
To fix this you must use the solution given by @chalyyzhu but also must set the ping timeout and close timeout to none on socket connection. WhatsApp dont use ping-pong to keep the connection alive.
self.websocket = await websockets.connect(constants.WEBSOCKET_URI, origin=constants.WEBSOCKET_ORIGIN, close_timeout = None,ping_interval = None)
Currently it's working for me.
Solved with #21