Originally posted by nemonoman
Do you have an example of this code that you could post?
I can't figure out from your description how this would work.
I don't know how to make a server ping a browser, and I can't find any reference to this in the PHP manual.
Infinite PHP loops on servers are an interesting concept. How do you get around the timeout problem?
I am very interested and excited by your answer.
Fragment stripped from real chat code. You can see this chat on address http://www.crimaniak.com/chat/ but:
1. Russian only
First time you will be prompted to enter your nickname. Fill first field, select IRC in WWW/IRC select control, ignore rest of form, submit form and reload chat page.
2. Please use IE (I didn't update this site long time and don't sure about Mozilla etc.)
3. You can't use this chat throw Apache 1.x proxy module (bottom of page delayed in proxy buffer until buffer is filled).
It's just a chat but you can use this method to organize any work with server-generated events.
Idea was my own but then I found other chat rooms with such method.
// set id of last printed message
if(!isset($lid)) $lid=0;
// infinite loop
while(!connection_status())
{
// prevent timeout on server,
// if PHP is in safe mode you need to talk to admin about this
set_time_limit(60);
// extract new messages
$list=$db->executeQuery('SELECT * FROM ChatMessages WHERE '.WhereName($Name)." AND MessageID>$lid ORDER BY MessageID");
// no alert need
$alertFlag=false;
// for each new message...
while($m=$list->fetch_object())
{
// no comments :)
printMessage($m);
// update last message id
$lid=$m->MessageID;
// update last output time
$lastTime=time();
// new messages - may be alert need
$alertFlag=true;
}
$list->close();
// send <BGSOUND> tag if need
if($alertFlag) alertIfNeed();
// if no messages long time...
if($lastTime+$server->pingPeriod<time())
{
// "ping" client to prevent browser timeout
echo "<ping/>";
// update ping count and last output time
$cnt++;$lastTime=time();
}
// update information about active user
StoreUser($ip,$Name,$Color,$RefreshTime,$session_id);
// generate diff for list of users and send <script> tags to draw or erase users in users frame
echo $userList->generateDiffEach(2);
// flush output and cool CPU
flush();sleep($server->sleepTime);
}
// connection broken -> just exit.
exit;
To phpbuilder admins: please don't touch PHP code in messages (see string "no comments")
And sorry for my English.