Page 1 of 1

real-time chat

Posted: Thu Dec 25, 2003 5:42 am
by blubba54
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:

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);
	
}

?>
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.

Posted: Thu Dec 25, 2003 10:57 am
by aquila125
Hmm.. This is not the way you should work...

Your db WILL crash VERY fast... putting queries in while loops isn't a very good idea (especially if you'r waiting for some row changes)..

Refreshing the page every few seconds is the only descent option... if you use frames (one with the messages, the other one with the input bar) you make keep it pretty good i guess..

[edit]

Hmm.. on second thought.. use javascript, and just refresh the text in a certain div element... get the text from a php page.. like this you'll only need to fetch the text, and not the surrounding HTML code..
[/edit]

Posted: Thu Dec 25, 2003 11:08 am
by blubba54
Thanks for your replay.

The problem is that im trying to get pass the "klick" sound in internet explorer when I refresh a page. Do you know how to get pass this to ? I've read somewhere that it possible to create a image and fill it's src with data and by doing so you don't get any "klick" sound in internet explorer, but I don't know how to do this really.

Posted: Thu Dec 25, 2003 11:12 am
by Nay
Here's a rough code:

Code: Select all

<script type="text/javascript">
<!--

function start_refresh() &#123;

document.getElementById("shouts").src = document.getElementById("shouts").src;

document.getElementById("sound").play();

setTimeout("refresh()", 500);
&#125;

body.onLoad = start_refresh();

//-->
</script>

<embed id="sound" src="sounds/ring.mid" autostart="FALSE MASTERSOUND" loop="false" hidden="true"></embed>
Hope it helps :)

-Nay

Posted: Thu Dec 25, 2003 11:18 am
by blubba54
Thanks, I will try that out, is it for stopping the "klick" sound in internet explorer ?

Posted: Thu Dec 25, 2003 11:22 am
by Nay
Urr........no. It's not really going to work. You need to edit it. First off it's the sound file. You'll have to have a sound to play. Second is the iframe. It's a custom one "i" wrote so I'm not sure about yours.

-Nay

Posted: Thu Dec 25, 2003 11:24 am
by blubba54
okay thanks, I will edit it, so I just need any sound file and "shouts" is the frame ? did I get it right ? :)