venomous0x / WhatsAPI

Interface to WhatsApp Messenger
2.59k stars 2.13k forks source link

Best way to keep the connection open #482

Open casparbanis opened 11 years ago

casparbanis commented 11 years ago

I am using WhatsAPI/src/php/ajaxDemo. What is the best way to keep the connection open? After a refresh of the index.php I get the status 'online'. But after a while (say half an hour) I can't send nor receive messages and my status becomes 'offline'. Is it good practise to refresh the index.php to keep the connection open? I am worried about getting banned by Whatsapp because of all the logins caused by refreshing the page.

mgp25 commented 11 years ago

You can keep your session open, edit the socket.php file. And I recommend you if you play with WhatsAPI to use a virtual phone number :P

casparbanis commented 11 years ago

Thanks for the quick response! I will not use my official number.

mgp25 commented 11 years ago

You are welcome :) if you need something more just tell

ciph3rIT commented 11 years ago

Would this work for multiple connections if I just want to query the last seen time?

Currently, I am only concerned about querying the last seen time for contact by

$wa = new WhatsProt($sender, $imei, $nickname, TRUE); $wa->connect(); $wa->loginWithPassword($password); $wa->sendGetRequestLastSeen($dst);

But this creates a new connection everytime the php file is refreshed.

Is it possible that another file keeps connection open in the background and the input in accepted by a file which only calls sendGetRequestLastSeen($dst) without having to create an object and login?

Thanks...

mgp25 commented 11 years ago

I think it could be like this: a file where we are showing the information (status, last seen, profile picture…) while on the other file (script php) is working all the time, so we never close the session.

$wa = new WhatsProt($sender, $imei, $nickname, false);
$wa->connect();
$wa->loginWithPassword($password);

//you could do a loop or whatever you want so you are always checking the 'last seen'
$wa->sendGetRequestLastSeen($dst);

You cant call that function if you are not logged in :/

ciph3rIT commented 11 years ago

Thanks @mgp25

Tried it...doesn't work in my case. What I did was to use just one file, the first time it would get into a loop, the second time it will not. I am getting the error "Object dooes not exist" or timeout in protocol.class.php

The issue is I have to POST target numbers to the script at run time and not have it defined in a file or script before hand.

//file name which will be created later $my_file = '123321' . '.txt';

$wa = new WhatsProt($sender, $imei, $nickname, TRUE); $wa->connect();

//It would not jump the first time but second time will, without logging in. if (file_exists($my_file)) goto abc;

//logs in first time $wa->loginWithPassword($password);

//creates file the first time $handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); //implicitly creates file

//infinite loop to keep the session active. The method looks up the last seen time but does not print or echo it :looped $wa->looping($dst); goto looped;

//Jumps here second time without logging in :abc $wa->sendGetRequestLastSeen($dst);

:

mgp25 commented 11 years ago

i have made a test, it worked fine:

loop.php

<?php
// Just an example…
require '/loop2.php';
?>

loop2.php

$wa = new WhatsProt($sender, $imei, $nickname, true);

$wa->connect();
$wa->loginWithPassword($password);

$a=0;
while($a == 0){
echo "\n[] Request last seen $dst: ";
$wa->sendGetRequestLastSeen($dst);
sleep(5);
}

You can use contactsync, with that you get the status, target and last seen. The value obtained in last seen is in seconds

ciph3rIT commented 11 years ago

Thanks for your help @mgp25 I am running some tests...getting somewhere now :+1: Will update you soon...

shirioko commented 11 years ago

I'm a bit confused as to what

$a = 0;
while($a == 0)

is supposed to do (rather than using while(true))

ciph3rIT commented 11 years ago

yes sir, same thing...

I was trying to dissect contactsync and contacts...it doesn't need to login to fetch the status and last seen. Is that true? Does it get it by just creating a new object of WhatsAppContactSync and then calling executeSync?

If that is the case, then I don't really need to use

$wa = new WhatsProt($sender, $imei, $nickname, false); $wa->connect(); $wa->loginWithPassword($password);

and cause multiple logins....

shirioko commented 11 years ago

You can't get last seen time using contact sync

ciph3rIT commented 11 years ago

You are right. Then what is $contact['lastupdate'] in the array?

shirioko commented 11 years ago

Status update

mgp25 commented 11 years ago

My fault, true, you cant get 'last seen' time using contact sync. @shirioko is right

Use this instead:

 $msgid = time()."-1";
function onGetRequestLastSeen($username, $msgid, $seconds)
{
// function. You can format the timestamp or create a function that converts seconds to-> x days, y hours, z min
echo $seconds;
}

$w->eventManager()->bind('onGetRequestLastSeen', 'onGetRequestLastSeen');
$w->sendGetRequestLastSeen($target);
ciph3rIT commented 11 years ago

Isn't that $contact['status'] ?

shirioko commented 11 years ago

'status' is the status that you posted, 'lastupdate' is the timestamp when you posted it

ciph3rIT commented 11 years ago

@shirioko okay thanks...got it.

@mgp25 still can't get around the multiple login problem...I don't know how you are making it work for multiple POST to PHP without multiple login's. Could you please paste your full code? Thanks..

mgp25 commented 11 years ago

@ciph3rIT get my mail from my profile page on githug, send me something and then i will explain you how i have it

Fidelity88 commented 10 years ago

@mgp25 can you post it here for others to see/learn or on pastebin? Tnx!

marwanzak commented 10 years ago

how about to use long polling with handling messages in database?