Exporting programs to other languages is one of the things programmers love, on the web we have
Two different programming environments: client (browser) and server-side, according to the HTTP protocol definition,
We can write in the client output in other languages of the server program, we chose to serve as
Business-side language, JavaScript as the client output. In this question we will show you how to use
The program stores data in the client, and in such things as: chat rooms, news systems or other you want to
The implementation of the application to reach the server side and the client (browser) the smallest data transfer.
The following support is required:
PHP4
Javascript
Frames
Main ideas:
We've been trying to develop a chat room based on HTTP protocol (HTTP CHAT ROOM) in PHP,
Although the HTTP protocol is not a good protocol for chatting, it can be unaffected by firewalls or proxies,
PHP can fully implement this feature without using Java APPLETS, there are two main problems in the chat room:
First, because IE does not support server PUSH technology, we only use the client pull technology (both
Client is automatically refreshed), the second problem goes deeper: Because the idea is refreshed at the client, so dress
The client must transmit all the messages at a time, which means a lot of data transfer, which is also the main reason for the delay of the chat room
For reasons, this article attempts to resolve the problem:
Using framework technology (frames) You can refresh the specified page without reloading another page, which
To reduce the number of service/client (c/s) data transfers. Our model is based on this scenario.
"Master" file: Defining a Frame structure
"Loader" page: Importing Data
Display page: Displaying data
In this scenario, the "Loder" box is automatically refreshed every "X" second--our idea is to store the data in "master"
file, so that the "Loder" page simply requests the client for data that is not available to the server. We use timestamp (timestamp)
Remember each message to determine which messages must be passed to the client that do not have to be transmitted. We use PHP4. Session Management for 0 (sessions)
Stores the last updated timestamp of the client (timestamp) to make the timestamp visible to both the server and the client. When the "loader" file
Refresh the Display page ("Diaplay") when you receive data from the "master" file (Note: the "master" file is large, but it is sent only once)
The "display" page is simply a JavaScript function called "displaymsgs ()" That invokes the "master" file to display a message. This function dynamically displays
Data stored in the "master" file, the following is a rough flowchart:
1. The browser requests the master page (frame), the master page is routed from the server side to the client (browser), and then "master"
File generation framework, and the "loader" and "display" pages are transferred to the client.
2. On the server side, the "loader" file will parse: If the client does not define a "timestamp" session variable, the "Loder" file will
Get all the data from the server and generate JavaScript code to deposit the data in the "master" file, and then save the "timestamp" variable as
The session variable.
3. The "Loder" page generates JavaScript code to refresh the "display" page.
4. The refresh request causes the "display" page to invoke the "diaplaymsgs ()" JavaScript function to display the data
5. Go back to step 2 every "x" second
We can think of the following:
========================================================
"Master" file: Very large, defines the DISPLAYMSGS () function and stores data and initial values.
"Loader" Files: small, retrieving data from the server, generating JavaScript code
"Display" File: Very small, calling the "diaplaymsgs ()" Function of the "master" file
=========================================================
Note: the "master" file is sent only once
"Loder" and "display" files are refreshed every "x" second
The first transmission, "Loder" may be very large, but later it will be very small
The "Diaplay" file has not changed
If you are not clear on the above ideas, we will set up a chat room to explain the method, this chat room is just for the simple demo
So it may not be very useful, but you can use the idea to build a more complex chat room, and remember that this idea is not just for chat rooms. :)
First, you should use the MySQL database form:
============================
CREATE TABLE Testeable (
Timestamp datetime,
Message text
);
============================
The "master" file is as follows:
================================================
<script>
Lines=new Array ();
function Displaymsgs () {
for (i=0;i<lines.length;i++) {
Display.document.write (Lines[i]);
Display.document.write (' <BR> ');
}
}
</script>
<frameset cols= "1" rows= "20,60,20" border= "0" >
<frame name= "loader" src= "loader.php" >
<frame name= "Display" src= "display.php" >
<frame name= "form" src= "form.php" >
</frameset>
==================================================
Note: the "form" file is a speaker box that provides user input to the speaker box.
"Display" File contents:
=====================
<script>
Top.displaymsgs ();
</script>
====================
Is the "display" file small? :)
"Loader" File:
====================
<?php
Session_Start (); Use Sessions in this!
if (!isset ($timestamp)) {
If "timestamp" is not defined, it is defined and set to 0
$timestamp = 0;
}
$dab =mysql_connect ("localhost", "User", "password"); Open Database
mysql_select_db ("Testbase", $dab);
Find information that is not in the client
$query = "SELECT * from testeable where timestamp> ' $timestamp '";
$result =mysql_query ($query, $dab);
$msgs =array ();
In this loop, we store the latest news/data and set "timestamp" to the current maximum value
In this loop we generate JavaScript code
Store the latest data from the server to the "master" page (Note: use "top" to point to the topmost window (master)
Now we're going to generate "JavaScript" code that refreshes the "display" page
?>
Top.display.location.reload ();
</script>
<!--note that the "setinterval ()" Method of JavaScript is used to make the loader page refresh every 4 seconds-->
<body onload= "Window.setinterval (' location.reload () ', 4000);" >
</body>
=======================================
"Form" page:
====================
<?php
Session_Start ();
if (!isset ($timestamp)) {
$timestamp = 0;
}
Displays the form, resulting in a "timestamp" variable.
if (Isset ($msg)) {
$dab =mysql_connect ("localhost", "root", "Seldon");
mysql_select_db ("Testbase", $dab);
$query = "INSERT into testeable (timestamp,message) VALUES (now (), ' $msg ')";
mysql_query ($query, $dab);
Get all the messages after timestamp
$query = "SELECT * from testeable where timestamp> ' $tt '";
$result =mysql_query ($query, $dab);
$msgs =array (); $i =0; $timestamp = 0;
while ($res =mysql_fetch_array ($result)) {
$msgs []= $res ["message"];
if ($res ["timestamp"]> $timestamp) {
$tt = $res ["timestamp"];
}
}
Session_register ("timestamp");
?>
<script>
<?php
for ($i =0; $i < $count ($MSGS); $i + +) {
?>
top.lines[top.lines.length]= "<?print (" $msgs [$i] ");? > ";
<?php
}
?>
Top.display.location.reload (); Refresh the Display page
</script>
<?php
}
?>
<form name= "foo" action= "<?php print (" $PHP _self ");?>" method= "Post >
Message:<input type= "text" name= "MSG" >
<input type= "Submit" Name= "newmsg" value= "Send" >
</form>
=====================================================
Note: We make a presentation on the "form" page, immediately refresh the "display" page, which can be achieved to the speaker immediately speak immediately
Display, more reflect the real-time nature.
As you can see, this chat room is very simple, which is to minimize the customer/service (c/s) data transfer skills, use this technology to achieve the most
Small data transfer, reminding you again: this technology is not used in chat rooms!
=======================================================
Above the win98+apache1.3+php4.03 platform test pass!
If you have suggestions or questions please contact the Flying Community (http://feiyschool.51.net)
or send an email to feiyhy@sina.com.
Please enlighten me, thank you!
===============================
English Original author: Luis Argerich, Alejandro Mitrou
Website of original English: http://www.phpbuilder.com
"This article copyright belongs to the author and the Osso net jointly owns, if needs to reprint, please indicate the author and the origin"
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.