breaststroke
Programmer
Hello!
I am trying to use long polling on a chat based on Ajax.
I have found some examples on the web but it isn't really clear to me.
I have to make a query to my database in order to check whether there is some (new)messages or not. I understand th elogic of the long polling, but don't know how to create the script properly.
something like this (the PHP page, which connects to the server):
How can I do a long polling in there?
My try:
I know what I have made is a disaster .I would appreciate any help.
Regards
enjoy practicing languages at:
I am trying to use long polling on a chat based on Ajax.
I have found some examples on the web but it isn't really clear to me.
I have to make a query to my database in order to check whether there is some (new)messages or not. I understand th elogic of the long polling, but don't know how to create the script properly.
something like this (the PHP page, which connects to the server):
Code:
...
..
//my usual query:
include('conexioninclude.php');
mysql_set_charset('utf8');
$registros = mysql_query("SELECT*FROM chat WHERE (password1='$password1' AND password2='$password2')AND(date>($time-300))ORDER BY code DESC LIMIT 1",$conexion)
or die("Error en el select :".mysql_error());
if($reg=mysql_fetch_array($registros))
{
...
How can I do a long polling in there?
My try:
Code:
...
..
$start_time = time();
//to let the server rest:
while ((time() - $start_time) < 30)
{
//mysql query to find a new message:
include('conexioninclude.php');
mysql_set_charset('utf8');
$registros = mysql_query("SELECT*FROM chat WHERE (password1='$password1' AND password2='$password2')AND(date>($time-300))ORDER BY codigo DESC LIMIT 1",$conexion)
or die("Error en el select :".mysql_error());
if($reg!=mysql_fetch_array($registros))
{
//I set a frecuence:
usleep(1000);
$registros = mysql_query("SELECT*FROM chat WHERE (password1='$password1' AND password2='$password2')AND(date>($time-300))ORDER BY codigo DESC LIMIT 1",$conexion)
or die("Error en el select :".mysql_error());
if($reg=mysql_fetch_array($registros))
{
$response=$reg['myvariable'];
}
}
echo $response;
exit();
}
..
Regards
enjoy practicing languages at: