_php Example of PHP simple chat room based on HTTP long connection "Server push" technology

Source: Internet
Author: User
Tags manual flush http request
First is the home page, which contains a text input and an IFRAME that displays the chat content, and a hidden IFrame is used to submit form forms:
Copy Code code as follows:

<?php
chat.php
Header (' cache-control:private ');
Header (' content-type:text/html; Charset=utf-8 ');
?>
<script type= "Text/javascript" >
function Submitchat (obj) {
Obj.submit ();
Document.getelementsbyname (' content ') [0].value = ';
}
</script>
<iframe src= "./chat_content.php" height= "width=" 100% "></iframe>
<iframe name= "say" height= "0" width= "0" ></iframe>
<form method= "POST" target= "Say" action= "/say.php" (This) ">
<input type= "text" size= "name=" content "/> <input type=" button "value=" Say "onclick=" Submitchat (this.form ) "/>
</form>

The other is to save the user submitted chat content, I simply write the text, and did not do anything to lock, this is a simple version:
Copy Code code as follows:

<?php
$content = Trim ($_post[' content '));
if ($content) {
$fp = fopen ('./chat.txt ', ' a ');
Fwrite ($FP, $content. "\ n");
Fclose ($FP);
Clearstatcache ();
}
?>

Next look at the main HTTP long connection section, which is the chat_content.php file:
Copy Code code as follows:

<?php
Header (' cache-control:private ');
Header (' content-type:text/html; Charset=utf-8 ');
The test setting is timed out for 30 seconds and is typically set for a longer period of time.
Set_time_limit (30);
This line is to handle IE this BT
Echo str_repeat (', 256);
Ob_flush ();
Flush ();
$fp = new Splfileobject ('./chat.txt ', ' r+ ');
$line = 0;
$totalLine = 0;
while (! $fp->eof ()) {
$FP->current ();
$totalLine + +;
$FP->next ();
}
$FP->seek ($totalLine);
$i = $totalLine-1;
while (true) {
if (! $fp->eof ()) {
if ($content = Trim ($fp->current ())) {
Echo ' <div> ';
echo Htmlspecialchars ($content);
echo "</div>";
Flush ();
$FP->next ();
$i + +;
}
} else {
$FP->seek ($i-1);
$FP->next ();
}
{
Here you can add a heartbeat to exit the loop
}
Usleep (1000);
}
?>

I can explain it in a row, but it's also easier to understand:
06. Set a timeout, because to maintain the HTTP long connection, this time must be longer, it may take a few hours, the article mentioned above also shows that this HTTP long connection can only open two, due to browser restrictions. In addition, even if you set a never timeout, in fact, the server part (such as Apache) configuration files may set the HTTP request for the longest wait time, so it may not be the effect you want, the general default may be 15 minutes timeout. If you are interested you can try to modify yourself.
09. Here output a blank, mainly the manual has been explained, IE browser in the first 256 characters will not be directly output, so we casually output some white space in order to let the content output later, maybe other browsers also have other browser settings, A description of the Frush function of the PHP manual can be viewed specifically. The next 11, 12 lines are forced to leave these blanks to the browser output.
13. ~ 20. This is primarily to calculate the number of rows in the file so that you can start reading from behind this line.
The next while loop is a dead loop, is the circular output file content, each judge whether to reach the end of the file, if a user writes to the file, the current detection is definitely not the end of the file, the line read out the output, otherwise the pointer to move one line, continue to cycle, wait 1000 microseconds each time,
39. If you keep a long connection, then even if the client is disconnected, the server may not know that the client has been disconnected, so there might be some heartbeat records, such as each user to maintain a heartbeat flag, each cell a few seconds to update the final heartbeat time, when the last time the detection is not updated, Launch this dead loop and close this HTTP connection.
OK, basically the principle is this, of course, this performance is not clear, interested in their own try, welcome to exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.