tgalal / yowsup

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

Problem when sending messages when are already listening #299

Closed clemenlg closed 10 years ago

clemenlg commented 10 years ago

Hi, I have a problem when, after start listening:

...
listener=WhatsappListenerClient(True, True)
listener.login(login, pass_encoded)
...

I send a message:

...
wa_mess = WhatsappEchoClient(phone, message, True)
wa_mess.login(login, pass_encoded)
...

The first time that i send a message, all is ok, the client have the double check (ack confirmation), but then, when the message was sended, i see this in the console:

ReaderThread:  Waiting, time to die: T-180 seconds
ReaderThread:   Socket closed, got 0 bytes!
ReaderThread:   Sending disconnected because of closed
YowsupConnectionManager:    Setting state to 0
Disconnected because closed

And i receive the messages, but didn't appears the double check, i suppose that yowsup didn't send the ack confirmation. The problem is when i send a message, because, if i only receive messages, all works perfectly (double check in mobile). I have read other posts, and the people tells about this problem, in "interactive mode" as https://github.com/tgalal/yowsup/issues/174 Another people tells about change the pong self.lastPongTime = int(time.time()); from ReaderThread.run() to ConnectionManager._writeNode() as https://github.com/tgalal/yowsup/issues/128 but i commented this line in run function, and i added it into _writeNode function (inside the exception: ConnectionClosedException), and the problem is the same. Someone can help me? Regards

clemenlg commented 10 years ago

If I send a message with:

WhatsappEchoClient(phone, message, waitForReceipt)

and

waitForReceipt = True
, this fix: https://github.com/stv0g/yowsup/commit/3646609fba2b31c7e3801b1f7ecde093d5e2c6ab didn't work, but if
waitForReceipt = False
, works with this workaround. Solved! EDIT: It didn't work... :( Re-open the issue

clemenlg commented 10 years ago

I re-open the issue because the commit 3646609 didn't work. Now, the error "Socket closed, got 0 bytes!", isn't the last message, but continues the same error, a bit before. However, i conserve the changes from this commit. This is that i saw:

[...]
WAuth:  Read stream start
WAuth:  Read features and challenge
BinTreeNodeReader:  Incoming
BinTreeNodeReader:  






WAuth:  GOT FEATURES !!!!
BinTreeNodeReader:  Incoming
BinTreeNodeReader:  

�օ��Z�������dIl
WAuth:  GOT CHALLENGE !!!!
WAuth:  Sending Response
BinTreeNodeWriter:  Outgoing
BinTreeNodeWriter:  
 
���P1��=�"`z�i�!V<+���S���'!���C��e��
WAuth:  Read success
ReaderThread:   Sending disconnected because of closed
YowsupConnectionManager:    Setting state to 0
Disconnected because closed
BinTreeNodeReader:  Incoming
BinTreeNodeReader:  

�-�Ngٌp*��)T����`
WAuth:  Login Status: success
WAuth:  Expires: 1435939704
WAuth:  Account type: free
WAuth:  Account status: active
Authed XXXXXXXXXXX
[...]

This is the tracebak:

Traceback (most recent call last):
  File "/path/src/Yowsup/connectionmanager.py", line 780, in run
    node = self.socket.reader.nextTree()
  File "/path/src/Yowsup/ConnectionIO/bintreenode.py", line 176, in nextTree
    self.readStanza();
  File "/path/src/Yowsup/ConnectionIO/bintreenode.py", line 48, in readStanza
    num = self.readInt8(self.rawIn)
  File "/path/src/Yowsup/ConnectionIO/bintreenode.py", line 76, in readInt8
    return i.read();
  File "/path/src/Yowsup/ConnectionIO/connectionengine.py", line 105, in read
    raise ConnectionClosedException("Got 0 bytes, connection closed");
ConnectionClosedException: Got 0 bytes, connection closed
ReaderThread:   Socket closed, got 0 bytes!
Disconnected because closed

In ReaderThread.run, when arrives to this line:

node = self.socket.reader.nextTree()
it creates a new node (nexTree), when reads, it didn't read nothing: in ConnectionEngine.read, because self.recv(self.readSize)!=1, and raise one exception.

P.S.: Thanks to @tgalal, @CODeRUS and @shirioko for to do all this work (yowsup).

clemenlg commented 10 years ago

I try to do it since console (to try if the problem is my code):

Same error: Disconnected because closed I edit onDisconnected (class WhatsappListenerClient function), and when it disconnected, try to reconnect:

    def onDisconnected(self, reason):
        print("Disconnected because %s" %reason)
                self.login(phone, base64.b64decode(bytes('BLABLABLA'.encode('utf-8'))))

And reconnected ok sometimes, but other times raise an exception Exception: TWO READER THREADS ON BOARD!!, it's supposed that onDisconnected function only executes when the connection is off, but if i try to check self.cm.readerThread.isAlive(), sometimes appear as live and another as dead...

mgp25 commented 10 years ago

Thank you for sharing your whatsapp credentials... (please hide it). You can NOT be logged in in two different sessions. Thats why it disconnects. You can do your own script with that features.

Venga hombre, que es fácil xD

shirioko commented 10 years ago

Lol I was gonna edit his post but then I realized

you have no power here meme _ quickmeme

clemenlg commented 10 years ago

Sorry, i forgot edit the last code (i change it directly in ListenerClient.py and...copy&paste) :cold_sweat:. @mgp25 mmm..., allways i'm trying to login in two different sessions with my own code, perhaps it's the poblem, i'll modify my script code and will try again.

mgp25 commented 10 years ago

@shirioko lol

@clemenlg te voy a dar una pista CmdClient.py (echa un vistazo a ese código y desde ahí puedes montarte tu propio script)

English: Tip, look CmdClient.py code

clemenlg commented 10 years ago

@mgp25 Yes, i'm working now again with CmdClient.py as base. Initially i worked with CmdClient.py as base for my code, but i was a problem with the connection closed issue, then i try to work with ListenerClient.py as a base (and sending with EchoClient.py), and with same issue. But after read the commit 3646609, i solved this problem with the listener, and i forgotted the CmdClient.py. I didn't think about the two connections with WhatsApp server. :disappointed_relieved: Spanish: ¡Gracias! :+1:

clemenlg commented 10 years ago

It works perfectly, i'll try another day more, and close this issue. Thanks

clemenlg commented 10 years ago

Ok, all the problem was first the issue that can solved with the commit 3646609, and then, to open 2 connections... :S