First, the homepage contains a text input and an IFRAME that shows the chat content, and a hidden IFRAME used to submit the form:
CopyCode The Code is as follows: <? PHP
// Chat. php
Header ('cache-control: Private ');
Header ('content-type: text/html; charset = UTF-8 ');
?>
<HTML>
<SCRIPT type = "text/JavaScript">
Function submitchat (OBJ ){
OBJ. Submit ();
Document. getelementsbyname ('content') [0]. value = '';
}
</SCRIPT>
<IFRAME src = "./chat_content.php" Height = "300" width = "100%"> </iframe>
<IFRAME name = "say" Height = "0" width = "0"> </iframe>
<Form method = "Post" target = "say" Action = "./say. php" onsubmit = "submitchat (this)">
<Input type = "text" size = "30" name = "content"/> <input type = "button" value = "say" onclick = "submitchat (this. form) "/>
</Form>
</Html>
The other is to save the chat content submitted by the user. I simply wrote the text and did not lock it. This is just a simple version:Copy codeThe Code is as follows: <? PHP
$ Content = trim ($ _ post ['content']);
If ($ content ){
$ Fp = fopen ('./chat.txt', 'A ');
Fwrite ($ FP, $ content. "\ n ");
Fclose ($ FP );
Clearstatcache ();
}
?>
next, let's take a look at the main HTTP persistent connection section, that is, the chat_content.php file: copy Code the code is as follows: header ('cache-control: Private ');
header ('content-type: text/html; charset = UTF-8 ');
// set a 30-second timeout value for the test. Generally, a relatively long time is set.
set_time_limit (30);
// This line is used to handle the Internet Explorer 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 ();
}< br> $ FP-> seek ($ totalline);
$ I = $ totalline-1;
while (true) {
If (! $ FP-> EOF () {
if ($ content = trim ($ FP-> current () {
echo '
';
echo htmlspecialchars ($ content);
echo "
";
flush ();
$ FP-> next ();
$ I ++;
}< BR >}else {
$ FP-> seek ($ I-1 );
$ FP-> next ();
}< BR >{< br> // you can add a heartbeat check to exit the loop
}< br> usleep (1000 );
}< BR >?>
I can explain it in a row, but it is also easy to understand:
06. set a time-out period. It may take several hours to maintain a long HTTP connection, in the Article mentioned above, this type of HTTP persistent connection can only open two because of browser restrictions. In addition, even if you set a configuration file that never times out, the configuration file on the server (such as APACHE) may also set the maximum waiting time for the HTTP request, therefore, the effect may not be what you think. Generally, the timeout may be 15 minutes by default. If you are interested, try to modify it.
09. A blank section is output here, which is explained in the manual. The IE browser will not directly output the first 256 characters, so we can output some blank spaces at will first, in order to let the following content output, other browsers may also have settings for other browsers. For details, refer to the description of the Frush function in the PHP manual. Next Lines 11 and 12 are forced to throw these blank characters to the browser for output.
13 .~ 20. This is mainly used to calculate the number of file lines so that the content can be read from the end of this line.
the next while loop is an endless loop, that is, the content of the output file is cyclically. Each time you determine whether the object has reached the end, if a user writes a file, the current detection is definitely not the end of the file. The row will be read and output. Otherwise, the pointer will be moved forward to a row and the loop will continue. Wait for 1000 microseconds each time.
39. if you keep persistent connections, the server may not necessarily know that the client is disconnected even if the client is disconnected. Therefore, you may need to make some heartbeat records, such as keeping a heartbeat flag for each user, the last heartbeat time is updated several seconds per cell. When the last heartbeat time is not updated for a long time, this endless loop is launched to close the HTTP connection.
OK. The principle is basically the same. Of course, the performance is unclear. If you are interested, try it yourself. Please feel free to contact us.