yowsup icon indicating copy to clipboard operation
yowsup copied to clipboard

Disconnect after some time

Open kwiiby opened this issue 11 years ago • 39 comments

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.

kwiiby avatar Feb 21 '15 16:02 kwiiby

@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.

bmutinda avatar Feb 23 '15 06:02 bmutinda

@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

gautamMalu avatar Feb 23 '15 11:02 gautamMalu

@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.

bmutinda avatar Feb 23 '15 17:02 bmutinda

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

kwiiby avatar Feb 23 '15 19:02 kwiiby

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.

kwiiby avatar Feb 23 '15 19:02 kwiiby

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

gisho avatar Feb 23 '15 21:02 gisho

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 ?

bmutinda avatar Feb 24 '15 03:02 bmutinda

@mutindaz the even is already defined here YowNetworkLayer.EVENT_STATE_DISCONNECTED

gisho avatar Feb 24 '15 09:02 gisho

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

firstlevelIT avatar Feb 26 '15 20:02 firstlevelIT

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

kwiiby avatar Feb 28 '15 00:02 kwiiby

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?

firstlevelIT avatar Feb 28 '15 07:02 firstlevelIT

@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

gautamMalu avatar Mar 04 '15 14:03 gautamMalu

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

gisho avatar Mar 05 '15 10:03 gisho

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?

Kevke avatar Mar 19 '15 22:03 Kevke

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

fridtjof avatar Apr 25 '15 15:04 fridtjof

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.

jowiemann avatar Apr 30 '15 18:04 jowiemann

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

bmutinda avatar May 01 '15 05:05 bmutinda

Thank you, works very well.

jowiemann avatar May 01 '15 15:05 jowiemann

@jowiemann welcome.

bmutinda avatar May 02 '15 05:05 bmutinda

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.

shaunhp avatar May 03 '15 12:05 shaunhp