tgalal / yowsup

The WhatsApp lib
GNU General Public License v3.0
7.08k stars 2.23k forks source link

Connection closed (every 24h) no reconnect / no Event called #2341

Closed georg90 closed 5 years ago

georg90 commented 6 years ago

I have a problem where yowsup will disconnect each night (24h disconnect?) and won't reconnect..

I implemented a logic which will fire with the EVENT_STATE_DISCONNECTED and EVENT_STATE_DISCONNECT:

@EventCallback(YowNetworkLayer.EVENT_STATE_DISCONNECTED)
    def onStateDisconnected(self,layerEvent):
        #Stuff here

@EventCallback(YowNetworkLayer.EVENT_STATE_DISCONNECT)
    def onEvent(self,layerEvent):
        #Stuff here

But the event is not triggered.. It simply disconnects and won't run the actions in theonStateDisconneted. I also tried onEvent and onDisconnect both won't work ether.

Log file only returns this:

DEBUG:yowsup.layers.logger.layer:tx:
<iq type="get" id="1404" xmlns="w:p">
</iq>

DEBUG:yowsup.layers.network.layer:Disconnected, reason: Connection Closed
DEBUG:yowsup.layers.protocol_iq.layer:stopping ping thread
DEBUG:yowsup.layers.protocol_iq.layer:YowPingThread-1 - ping thread stopped

Any ideas? Any help is appreciated! :)

brunaoalx commented 6 years ago

Can you show us how you created your methods that try to reestablish the connection?

I have a callback method with @EventCallback (YowNetworkLayer.EVENT_STATE_DISCONNECTED)

That works perfectly.

georg90 commented 6 years ago

Thanks for replying @BRNALX What do you mean by establishing the connection? I copy pasted the relevant parts of layer.py below. Hope this helps?

layer.py

import os, subprocess, time, hvv, sys, math, pimatic
import pandas as pd
from random import randint
from yowsup.layers.interface                           import YowInterfaceLayer                                 #Reply to the message
from yowsup.layers.interface                           import ProtocolEntityCallback                    #Reply to the message
from yowsup.layers.protocol_messages.protocolentities  import TextMessageProtocolEntity                 #Body message
from yowsup.layers.protocol_presence.protocolentities  import AvailablePresenceProtocolEntity   #Online
from yowsup.layers.protocol_presence.protocolentities  import UnavailablePresenceProtocolEntity #Offline
from yowsup.layers.protocol_presence.protocolentities  import PresenceProtocolEntity                    #Name presence
from yowsup.layers.protocol_chatstate.protocolentities import OutgoingChatstateProtocolEntity   #is writing, writing pause
from yowsup.common.tools                               import Jid                                                               #is writing, writing pause
from yowsup.env import YowsupEnv
from yowsup.layers.network                           import YowNetworkLayer
from yowsup.common import YowConstants
from yowsup.layers.protocol_iq.protocolentities import *
from yowsup.layers.protocol_iq import *
from yowsup.layers import YowProtocolLayer, YowLayerEvent, EventCallback
from yowsup.layers.auth import YowAuthenticationProtocolLayer

class EchoLayer(YowInterfaceLayer):
    @ProtocolEntityCallback("message")
    def onMessage(self, messageProtocolEntity):
        if messageProtocolEntity.getType() == 'text':
            time.sleep(0.5)
            self.toLower(messageProtocolEntity.ack()) #Set received (double v)
            time.sleep(0.5)
            self.toLower(PresenceProtocolEntity(name = name)) #Set name Presence
            time.sleep(0.5)
            self.toLower(AvailablePresenceProtocolEntity()) #Set online
            time.sleep(0.5)
            self.toLower(messageProtocolEntity.ack(True)) #Set read (double v blue)
            time.sleep(0.5)
            self.toLower(OutgoingChatstateProtocolEntity(OutgoingChatstateProtocolEntity.STATE_TYPING, Jid.normalize(messageProtocolEntity.getFrom(False)) )$
            time.sleep(2)
            self.toLower(OutgoingChatstateProtocolEntity(OutgoingChatstateProtocolEntity.STATE_PAUSED, Jid.normalize(messageProtocolEntity.getFrom(False)) )$
            time.sleep(1)
            self.onTextMessage(messageProtocolEntity) #Send the answer
            time.sleep(3)
            self.toLower(UnavailablePresenceProtocolEntity()) #Set offline
    @EventCallback(YowNetworkLayer.EVENT_STATE_DISCONNECTED)
    def onStateDisconnected(self,layerEvent):
        print("Event: %s" % layerEvent.getArg("reason"))
        msg = "WhatsApp-Plugin : Disconnect reason: %s" % layerEvent.getArg("reason")
        print("WhatsApp-Plugin : Message " + msg)
        #print(msg)
        if layerEvent.getArg("reason") == 'Ping Timeout' or layerEvent.getArg("reason") == 'Connection Closed' or layerEvent.getArg("reason").find('Temporar$
        #    time.sleep(randint(5,15))
        #    print("WhatsApp-Plugin : Issueing EVENT_STATE_CONNECT")
        #    self.getStack().broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT))
        #elif layerEvent.getArg("reason") == 'Ping Timeout':
            time.sleep(randint(5,15))
            print("WhatsApp-Plugin : Issueing EVENT_STATE_DISCONNECT")
            self.getStack().broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_DISCONNECT))
            time.sleep(randint(5,15))
            print("WhatsApp-Plugin : Issueing EVENT_STATE_CONNECT")
            self.getStack().broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT))
        elif layerEvent.getName() == YowNetworkLayer.EVENT_STATE_CONNECTED:
            print("WhatsApp-Plugin : Connected")