I´m trying to learn how long polling work. I have read many tutorials out there and found most of them use jQuery. For me using jQuery is nice because it make thing easier. But to talk about learning javascript basic. Using jQuery is not the first choice for me to think about.
I have made my own lomg polling and have some question about it.
in javascript ( I will not have whole script because I guess people understand it very basic)
function callme() {
var tmstmp= new Date().getTime(); // sending time stamp now to php
var url="test2.php?user_id=298&tmstmp="+tmstmp;
var params="user_id=298&tmstmp="+tmstmp;
xmlHttp=GetXmlHttpObject()
xmlHttp.onreadystatechange=function () {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
if (xmlHttp.responseText!=""){
document.getElementById("result").innerHTML=xmlHttp.responseText;
}
setTimeout("callme()",1000);
}
}
In PHP
$user_id=$_POST['user_id'];
$tmstmp=$_POST['tmstmp'];
$params=$_POST['params'];
$stmt = $conn->prepare('SELECT * FROM user where recipient=:recipient ORDER BY cr_id desc');
$stmt->bindParam(':recipient', $user_id);
$r=$stmt->fetch();
$currentmodif=$r['database_timestamp'];
if ($currentmodif <= $tmstmp) { ///**HERE IS THE QUESTION !!
sleep(1);
clearstatcache();
$currentmodif =$r['database_timestamp'];
}
echo $r['message'].'-->'.$tmstmp;
My question is :
Actually in php
- if ($currentmodif <= $tmstmp) Must be While ($currentmodif <= $tmstmp)
According to the tutorials I found. But it´s not work if I use while Until I change it to be (>) WHY ?
- If I will keep on using if like this instead of using While is it wrong to do ?
Do you have any suggestion about this script I made ?
After a while (let´s say some minutes) I got message Service unavailable bla bla bla
I really wonder how the other poeple do the long polling without problem, Example facebook or other chat comet.
Thank you for any answer.