real-time chat
Posted: Thu Dec 25, 2003 5:42 am
Hi.
I am trying to make a real-time chat with php and mysql, and the problem I have is when Im trying to get the messages. I don't want to refresh the page with a HTTP-EQUIV="refresh" so i've done a loop where I check for messages and print them out whenever there is new messages.
The thing is that my chat is going to support serveral chat rooms, and whenver I try to start two chat rooms one of them work but the other page that is containing a new chat hangs. But if I close one of the chats everything works just fine. Why can't I run more then one php session at once when it runs a while loop ?
Here is some code from my chat where the messages is retrieved from the mysql server:
So if I try to open the page that contains this loop it works fine, but if I open one more after it the second page just hangs until I quit the first page. It seemes that I can't run more then one while-loop like this, but why ?
Currently I'm using IIS 5.1 and latest PHP (4.3.4), but I've try this chat under linux with apache (1.3.29) and the latest PHP but it's the same result there.
Can somebody please help me out
thanks in advance, and sorry for my bad english.
I am trying to make a real-time chat with php and mysql, and the problem I have is when Im trying to get the messages. I don't want to refresh the page with a HTTP-EQUIV="refresh" so i've done a loop where I check for messages and print them out whenever there is new messages.
The thing is that my chat is going to support serveral chat rooms, and whenver I try to start two chat rooms one of them work but the other page that is containing a new chat hangs. But if I close one of the chats everything works just fine. Why can't I run more then one php session at once when it runs a while loop ?
Here is some code from my chat where the messages is retrieved from the mysql server:
Code: Select all
<?php
session_start();
set_time_limit(0);
ignore_user_abort(false);
while(!connection_aborted())
{
$result = mysql_query("SELECT * FROM messages where room_n='$room' and id>($lastId)", $db);
$antalres = mysql_query("SELECT COUNT(*) FROM messages where room_n='$room' and id>$lastId", $db);
$antal = mysql_result($antalres, 0);
for($i = 0; $i < $antal; $i++)
{
$time = mysql_result($result, $i, "when_n");
$who = mysql_result($result, $i, "who_n");
$what = mysql_result($result, $i, "what_n");
print "<script> parent.frames['tempb'].document.body.innerHTML = parent.frames['tempb'].document.body.innerHTML+'$who <font style="font-size: 10px; font-family: verdana;">[</font>$time<font style="font-size: 10px; font-family: verdana;">]</font><font style="font-size: 10px; font-family: verdana;">:</font> $what<br>';</script>";
//print "$who [$time]: $what<br>\n";
print "<script> parent.frames['tempb'].scrollTo(0, 40000);</script>";
}
if($antal > 0)
{
$lastId = mysql_result($result, $antal-1, "id");
}
echo "<!-- -->";
flush();
sleep(1);
$currentTimestamp = mktime();
mysql_query("UPDATE room_users SET timestamp='$currentTimestamp' where username='$userid' and room='$room'", $db);
}
?>Currently I'm using IIS 5.1 and latest PHP (4.3.4), but I've try this chat under linux with apache (1.3.29) and the latest PHP but it's the same result there.
Can somebody please help me out
thanks in advance, and sorry for my bad english.