Skip to content
This repository has been archived by the owner on Nov 2, 2023. It is now read-only.

connection is lost. #18

Closed
chalyyzhu opened this issue Dec 13, 2021 · 7 comments · Fixed by #21
Closed

connection is lost. #18

chalyyzhu opened this issue Dec 13, 2021 · 7 comments · Fixed by #21

Comments

@chalyyzhu
Copy link

can't keep alive conection..

@lc-at
Copy link
Owner

lc-at commented Dec 15, 2021

Will take a look on this. Do you have the code to reproduce the problem?

@chalyyzhu
Copy link
Author

Screenshot_20211216-193458_HttpCanary

i see every few seconds whatsapp pings "?,," . to keep the connection alive

@chalyyzhu
Copy link
Author

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")

@chalyyzhu
Copy link
Author

Screenshot_20211216-193123_Termux

still disconnected...

@lc-at
Copy link
Owner

lc-at commented Dec 16, 2021

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.

@reinier-millo
Copy link
Collaborator

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.

@reinier-millo
Copy link
Collaborator

Solved with #21

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

Successfully merging a pull request may close this issue.

3 participants