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

Disconnect after some time #648

Closed
kwiiby opened this issue Feb 21, 2015 · 39 comments
Closed

Disconnect after some time #648

kwiiby opened this issue Feb 21, 2015 · 39 comments

Comments

@kwiiby
Copy link

kwiiby commented Feb 21, 2015

hey.. I am using echoclient (latest release 2.2.78) and it is disconnecting after some random time, it doesn't receive messages anymore (none event is fired onEvent)

I already have a thread to ping whatsapp every 30 seconds in my layer.. here it is:

entity = PingIqProtocolEntity(to = YowConstants.DOMAIN)
self.toLower(entity)
threading.Timer(30.0, self.pingWhats).start()

I also tried to use YowIqProtocolLayer, but it gives org.openwhatsapp.yowsup.event.network.disconnect after some time too.
So, what should I do?

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@bmutinda
Copy link

@kwiiby I am getting the same issue too. I have run my script as a background process but it still disconnects - (nohup python -u ./myscript.py > ./mylog.log &)

I am thinking of a work around like this.

Create a thread that keeps on checking a flag in a db of whether the whatsapp connection is still on in intervals of lets say 5 seconds.
If the connection is not available, I restart the connection again and update my flag.

@gautamMalu
Copy link

@mutindaz @kwiiby A simple workaround is to put the echo client in a bash script.
If it disconnects it will be started again.
#!/bin/bash
while true :
do
yowsup-cli demos --config config --echo
done

@bmutinda
Copy link

@gautammaloo I will try that.
The reason as to why I was talking about the db flag is because:

  1. I am not using the demos
  2. I have two scripts that both require connection to whatsapp - one script listens to incoming messages while the other one is supposed to broadcast messaging. If I run the broadcast script, the connection in the message listening script is terminated and vice-versa. I needed a way for the listening script to be restarted ONLY IF am done with broadcasting.

@kwiiby
Copy link
Author

kwiiby commented Feb 23, 2015

Why not everything in the same script? create a layer with a thread for broadcast..

@kwiiby
Copy link
Author

kwiiby commented Feb 23, 2015

About the issue, the ping wasn't working because YowIqProtocolLayer wasn't in the tuple below EchoLayer.. (in the stack)
Now with YowIqProtocolLayer working, Yowsup won't disconnect again.

@gisho
Copy link

gisho commented Feb 23, 2015

This is how i handled this issue. I listen for "DISCONNECTED" event state and login again

    def onEvent(self, layerEvent):       
        if layerEvent.getName() == YowNetworkLayer.EVENT_STATE_DISCONNECTED:
            self.log("Disconnected: %s" % layerEvent.getArg("reason"))
            self.connected = False
            reactor.callLater(2,self.login)       
        elif layerEvent.getName() == self.__class__.EVENT_NETWORK_CONNECTED:
            self.connected = True
        return False

@bmutinda
Copy link

Well, I don't want to dive into why I chose that approach but having YowIqProtocolLayer in your layers turple works. @gisho do I have to create the EVENT_STATE_DISCONNECTED defined in my stack because I am only getting EVENT_NETWORK_CONNECTED only in onEvent ?

@gisho
Copy link

gisho commented Feb 24, 2015

@mutindaz the even is already defined here YowNetworkLayer.EVENT_STATE_DISCONNECTED

@firstlevelIT
Copy link

@kwiiby could you explaint your solution more. How I can have the ping every 30 seconds in my script? Thanks

@kwiiby
Copy link
Author

kwiiby commented Feb 28, 2015

Just append YowIqProtocolLayer to layers = () in your stack.py (should be in the first turple..)

@firstlevelIT
Copy link

I have done it but the script disconnect randomly. Monitoring the network I can see the periodic ping but sometime it miss one and the cliente disconnect. There is a way to send ping every 30 sec instead of 120?

@gautamMalu
Copy link

@gisho I also tried it like your method, but it gets disconnected before it could listen to EVENT_STATE_DISCONNECT or EVENT_STATE_DISCONNECTED.
I put this in to my layer.py
def onEvent(self, layerEvent):
print layerEvent.getName()

it prints only these two events:
org.openwhatsapp.yowsup.event.network.connect
org.openwhatsapp.yowsup.event.auth.authed

@gisho
Copy link

gisho commented Mar 5, 2015

try @kwiiby suggestions its working for me.

i figured the the disconnected event is not propergating correctly on detached queue. you can remove ``detached = Falseat line 40 ofnetwork.layer`

self.emitEvent(YowLayerEvent(self.__class__.EVENT_STATE_DISCONNECTED, reason = reason, detached=True))

to

self.emitEvent(YowLayerEvent(self.__class__.EVENT_STATE_DISCONNECTED, reason = reason))

@Kevke
Copy link

Kevke commented Mar 19, 2015

i don't get it. I tried to use the script by #647 EliaComputers. I used the infinite while true loop, after a few minutes my pi receives no longer Messages from WhatsApp.
@kwiiby can u show me your script with the ping call?

@fridtjof
Copy link
Contributor

What did the trick for me was adding these parameters to the stack.loop call:

stack.loop(timeout = 0.5, discrete = 0.5) #this is the program mainloop

Found these in the CLI demo

@jowiemann
Copy link

Hello,

I'm using the echoclient and I'm not familar with Python. So, I would be very happy if some body can give me a working echoclient with an implemented ping. Thanks a lot.

@bmutinda
Copy link

bmutinda commented May 1, 2015

@jowiemann as @prasselpikachu pointed out, open stack.py in the echoclient demo then edit the following lines.
Change

self.stack.loop()

to

self.stack.loop( timeout = 0.5, discrete = 0.5 )

@jowiemann
Copy link

Thank you, works very well.

@bmutinda
Copy link

bmutinda commented May 2, 2015

@jowiemann welcome.

@tgalal tgalal added this to the Next milestone May 2, 2015
@shaunhp
Copy link

shaunhp commented May 3, 2015

Hmm. I've done the following:

  • added YowIqProtocolLayer in the first tuple in stack.py
  • added self.stack.loop( timeout = 0.5, discrete = 0.5 ) in stack.py

I am using the demo echoclient.

Still after those changes, the client disconnects. I can reconnect once, and still get it to echo messages properly. But, then it will disconnect again. Then I reconnect again, but this time the message queue is not being processed. :-(

After a reboot, I get two more chances like above then it stops working again....

I can reproduce this behaviour very easily.

@jowiemann
Copy link

Hello,
I have implemented the changes recommended by mutindaz. But now I've the following problem. Before the changes the echo client was terminated when sending a message. Now the echo client is still running, but echoing the message will fail. The echo client has to be stopped manualy and after restarting the message will be received again and echoed properly.

Your sincerly Joerg

@Nenyth
Copy link

Nenyth commented May 3, 2015

@jowiemann Make sure you update to the latest version of Yowsup. My problems were solved by doing so.

@jowiemann
Copy link

@Serilium thank you for your answer. I've done already an update, but the Problem is still the same.

@Rafiudo
Copy link

Rafiudo commented May 12, 2015

Don' working for me :(

@idigevian
Copy link

Not working for me as well. I cloned the latest code and added the timeout and discrete parameters in my Python main code :(

So to elaborate a little bit more: The client doesn't get disconnected, but it stops processing messages.

@abrahamreq
Copy link

I happen like @idigevian , I can´t receive messages. Help me please!!

@anuraguniyal
Copy link

I am also seeing lots of "Disconnected, reason: Connection Closed"
and sometime no disconnection but yowsup doesn't process any new messages,

@Rafiudo
Copy link

Rafiudo commented Jun 12, 2015

any news? Thks!

@msshapira
Copy link

#915 may just solve it

@ToninoTarsi
Copy link

I still have the same problem with last clone on raspberryPI
My layer are :

            layers = (
                 EchoLayer,
                (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer,YowMediaProtocolLayer, YowIqProtocolLayer, YowCallsProtocolLayer),
                YowAxolotlLayer,
                ) + YOWSUP_CORE_LAYERS

and

            stack.loop(timeout=0.5, discrete=0.5)

Any workaroud ?

@BorderBoy
Copy link

Hello,

unfortunately I still got the problem that yowsup disconnects. But it's very random.
Sometimes it works for 12 hours, but the most of the time it disconnects after 4-5 minutes.

I got YowIqProtocolLayer in my layers and I got the parameters at stack.loop:
stack.loop(timeout = 0.5, discrete = 0.5)

I tried all workarounds and solutions I could find but nothing worked so far.

@rudepython
Copy link

this isn't definitive solution for errors.
it's only a trick to prevent the application stop running.

run stack.start() in a daemon mode thread.
after error the thread restart automatically.

@carlosfelipe29
Copy link

HI, rudepython,

I am pretty new in yowsup.

This mean That I can replace the typical /disconnect from the yowsup-cli by a event_disconnect on the code using the stacks... modules.

any Idea, how put this event_disconnect on a thread code in oder to get connect and disconnect in every iteration with a time.sleep(5), for example.

I appreciate your help.

@bossonaro
Copy link

I have the same problem (Yowsup on Debian). This code bellow runs correctly, but stop running after some minutes.

How to do?

=== run.py ===

from layer                                                       import EchoLayer
from yowsup.layers                                        import YowParallelLayer
from yowsup.layers.auth                                import YowAuthenticationProtocolLayer
from yowsup.layers.protocol_messages         import YowMessagesProtocolLayer
from yowsup.layers.protocol_receipts            import YowReceiptProtocolLayer
from yowsup.layers.protocol_acks                 import YowAckProtocolLayer
from yowsup.layers.network                           import YowNetworkLayer
from yowsup.layers.coder                              import YowCoderLayer
from yowsup.stacks                                        import YowStack
from yowsup.common                                     import YowConstants
from yowsup.layers                                        import YowLayerEvent
from yowsup.stacks                                        import YowStack, YOWSUP_CORE_LAYERS
from yowsup                                                   import env
from yowsup.layers.axolotl                             import YowAxolotlLayer
from yowsup.layers.protocol_iq                     import YowIqProtocolLayer

CREDENTIALS = ("551199999999", "xxxx-password-xxxxx")
if __name__==  " __main__ ":
    layers = (
        EchoLayer,
        YowParallelLayer([YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowIqProtocolLayer]),
         YowAxolotlLayer, 
    ) + YOWSUP_CORE_LAYERS
    stack = YowStack(layers)
    stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, CREDENTIALS) 
    stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0])  
    stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN)              
    stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource())  
    stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) 
    stack.loop(timeout = 0.5, discrete = 0.5)  #this is the program mainloop

=== layer.py ===

from yowsup.layers.interface                           import YowInterfaceLayer, ProtocolEntityCallback
from yowsup.layers.protocol_messages.protocolentities  import TextMessageProtocolEntity
from yowsup.layers.protocol_receipts.protocolentities  import OutgoingReceiptProtocolEntity
from yowsup.layers.protocol_acks.protocolentities      import OutgoingAckProtocolEntity

class EchoLayer(YowInterfaceLayer):
    @ProtocolEntityCallback("message")
    def onMessage(self, messageProtocolEntity):
        if True:
            receipt = OutgoingReceiptProtocolEntity(messageProtocolEntity.getId(), messageProtocolEntity.getFrom(), 'read', messageProtocolEntity.getParticipant())
            outgoingMessageProtocolEntity = TextMessageProtocolEntity(
                messageProtocolEntity.getBody(),
                to = messageProtocolEntity.getFrom())
            self.toLower(receipt)
            self.toLower(outgoingMessageProtocolEntity)
    @ProtocolEntityCallback("receipt")
    def onReceipt(self, entity):
        ack = OutgoingAckProtocolEntity(entity.getId(), "receipt", entity.getType(), entity.getFrom())
        self.toLower(ack)

@fire829
Copy link

fire829 commented Dec 23, 2015

Have same problem 8(
Version 2.4.48
Running echo client example from git.
Got:

DEBUG:yowsup.layers.logger.layer:rx:
<stream:error>
<ack id="1900435987">
</ack>
</stream:error>

DEBUG:yowsup.layers.network.layer:Disconnected, reason: Connection Closed

I'm afraid lopped reconnects may be not best solution - whatsapp can detect it and ban account.

@ToninoTarsi
Copy link

I'm trying to solve the problem using this code but still testing

    @ProtocolEntityCallback("event")
    def onEvent(self, layerEvent):
        log("WhatsApp-Plugin : EVENT " + layerEvent.getName())
        if layerEvent.getName() == YowNetworkLayer.EVENT_STATE_DISCONNECTED:
            msg = "WhatsApp-Plugin : Disconnected reason: %s" % layerEvent.getArg("reason")
            SendMail(self.cfg, "WhatsApp-Plugin : Disconnected",msg, "")
            log(msg)
            if layerEvent.getArg("reason") == 'Connection Closed':
                time.sleep(20)
                log("WhatsApp-Plugin : Issueing EVENT_STATE_CONNECT")
                self.getStack().broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT))
            elif layerEvent.getArg("reason") == 'Ping Timeout':
                time.sleep(20)
                log("WhatsApp-Plugin : Issueing EVENT_STATE_DISCONNECT")
                self.getStack().broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_DISCONNECT)) 
                time.sleep(20)
                log("WhatsApp-Plugin : Issueing EVENT_STATE_CONNECT")
                self.getStack().broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT))
        elif layerEvent.getName() == YowNetworkLayer.EVENT_STATE_CONNECTED:
            log("WhatsApp-Plugin : Connected")

@ToninoTarsi
Copy link

With the code obove it is running now for 3 days without problems. I also set to 2.5 seconds the ping timeout.

@fire829
Copy link

fire829 commented Dec 26, 2015

ToninoTarsi, in which layer this code should be implemented?

@bossonaro
Copy link

Is this a example?
It isn't run. What is the error?
===> layer.py <===

from yowsup.layers.interface   import YowInterfaceLayer, ProtocolEntityCallback
from yowsup.layers.protocol_messages.protocolentities   import TextMessageProtocolEntity
from yowsup.layers.protocol_receipts.protocolentities   import OutgoingReceiptProtocolEntity
from yowsup.layers.protocol_acks.protocolentities   import OutgoingAckProtocolEntity
from yowsup.layers.network   import YowNetworkLayer

class EchoLayer(YowInterfaceLayer):

    @ProtocolEntityCallback("message")
    def onMessage(self, messageProtocolEntity):
        if True:
            receipt = OutgoingReceiptProtocolEntity(messageProtocolEntity.getId(), messageProtocolEntity.getFrom(), 'read', messageProtocolEntity.getParticipant())

            outgoingMessageProtocolEntity = TextMessageProtocolEntity(
                messageProtocolEntity.getBody(),
                to = messageProtocolEntity.getFrom())

            self.toLower(receipt)
            self.toLower(outgoingMessageProtocolEntity)

    @ProtocolEntityCallback("receipt")
    def onReceipt(self, entity):
        ack = OutgoingAckProtocolEntity(entity.getId(), "receipt", entity.getType(), entity.getFrom())
        self.toLower(ack)
        #self.toLower(entity.ack())

    @ProtocolEntityCallback("event")
    def onEvent(self, layerEvent):
        print("WhatsApp-Plugin : EVENT " + layerEvent.getName())
        if layerEvent.getName() == YowNetworkLayer.EVENT_STATE_DISCONNECTED:
            msg = "WhatsApp-Plugin : Disconnected reason: %s" % layerEvent.getArg("reason")
            #SendMail(self.cfg, "WhatsApp-Plugin : Disconnected",msg, "")
            print(msg)
            if layerEvent.getArg("reason") == 'Connection Closed':
                time.sleep(20)
                print("WhatsApp-Plugin : Issueing EVENT_STATE_CONNECT")
                self.getStack().broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT))
            elif layerEvent.getArg("reason") == 'Ping Timeout':
                time.sleep(20)
                print("WhatsApp-Plugin : Issueing EVENT_STATE_DISCONNECT")
                self.getStack().broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_DISCONNECT)) 
                time.sleep(20)
                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")

@tgalal tgalal modified the milestone: Next Apr 17, 2016
@tgalal tgalal closed this as completed May 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests