Minimize data transmission-store data on the client

Source: Internet
Author: User
Tags php print

Set Program Output to other languages is one of the favorites of programmers. on the Web, we have
Two different programming environments: the client (browser) and the server, according to the HTTP Protocol definition,
We can write the program on the client to output the server programs in other languages.
The client language and JavaScript are output as the client. In this question, we will show you how to use
This solution stores data on the client and
The minimum data transmission between the server and the client (browser) is achieved.

The following requirements are required:
PhP4
Javascript
Frames

Main ideas:

We have been trying to use PHP to develop an HTTP-based chat room ),
Although HTTP is not a good protocol for chat, it can be unaffected by firewalls or proxies,
PHP can fully implement this function 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 client Pull Technology (
Automatically refresh the client), the second problem goes deeper: because the idea is to refresh the client, the server
The service end must send all the messages each time, which means a large amount of data transmission, which is also the main
This article attempts to solve the problem:
With frame technology (frames), you can refresh a specified page without re-loading other pages.
To reduce the data transmission volume of the service/client (C/S. Our model is based on this solution.
"Master" file: defines the framework structure
"Loader" Page: import data
"Display" Page: displays data
In this solution, the "Loder" box is automatically refreshed every "X" seconds -- our idea is to store data in the "master"
In the file, as long as the "Loder" page requests the data that the client does not have from the server, we can use the timestamp)
Each message is recorded to determine which messages must be sent to the client without transmission. We use PhP4. 0 sessions)
Store the last updated timestamp of the client so that the timestamp can be seen on the server and client. When the "loader" File
When the data is received from the "master" file (note: the "master" file is large, but it is transmitted only once), the display page ("diaplay") is refreshed ")
The "display" Page simply calls the JavaScript function "displaymsgs ()" in the "master" file to display messages. Dynamic Display of this function
The data stored in the "master" file is as follows:
1. The browser requests the "master" Page (framework), the "master" page is sent from the server to the client (browser), and then "master"
File generation framework, and tune the "loader" and "display" pages to the client.
2. On the server side, the "loader" file will be analyzed: if the client does not define the "timestamp" session variable, the "Loder" file will
Obtain all data from the server and generate Javascript Code Save the data to the "master" file and save the "timestamp" variable
Session variable.
3. The "Loder" Page generates JavaScript code to refresh the "display" page.
4. Refresh the request so that the "display" page calls the "diaplaymsgs ()" JavaScript function to display data
5. Return to step 2 every "X" seconds

We can consider the following:
========================================================== ======================
"Master" file: very large. It defines the displaymsgs () function and stores data and initial values.
"Loader" file: small, fetch data from the server, and generate JavaScript code
"Display" file: very small. Call the "diaplaymsgs ()" function of the "master" file.
========================================================== ========================
Note: The "master" file is transmitted only once.
"Loder" and "display" files are refreshed every "X" seconds.
"Loder" may be very large during the first transmission, but it will be very small in the future.
The "diaplay" file remains unchanged

If you are still not clear about the above ideas, we will create a chat room to illustrate this method. This chat room is just for simple demonstration.
So it may not be very useful, but you can use this idea to create more complex chat rooms. Remember that this idea is not just about chatting rooms. :)

First, 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.doc ument. Write (lines [I]);
Display.doc ument. 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 speech box. You can enter a speech box.

"Display" file content:
==================================
<SCRIPT>
Top. displaymsgs ();
</SCRIPT>
================================
Is the "display" file very small?

"Loader" file:
================================
<? PHP

Session_start (); // use sessions here!

If (! Isset ($ timestamp )){
// If "timestamp" is not defined, it is defined and set to 0
$ Timestamp = 0;
}

$ Dab = mysql_connect ("localhost", "user", "password"); // open the database
Mysql_select_db ("testbase", $ DAB );

// Search for information not available on the client
$ Query = "select * From testeable where timestamp> '$ timestamp '";
$ Result = mysql_query ($ query, $ DAB );
$ Msgs = array ();

// In this loop, we store the latest message/data and set "timestamp" to the current maximum value.

While ($ res = mysql_fetch_array ($ result )){
$ Msgs [] = $ res ["message"];
If ($ res ["timestamp"]> $ timestamp ){
$ Timestamp = $ res ["timestamp"];
}
}
Session_register ("timestamp"); // register the "timestamp" variable

Echo '<SCRIPT> ';

// In this loop, we generate JavaScript code
// Store the latest data obtained from the server to the "master" Page (Note: Use "TOP" to point to the top window (master)

For ($ I = 0; $ I <$ count ($ MSGs); $ I ++ ){
?>
Top. lines [top. lines. Length] = "<? PHP print ("$ msgs [$ I]");?> ";
<? PHP
}

// Now we will generate the "JavaScript" code to refresh the "display" Page

?>
Top. display. Location. Reload ();
</SCRIPT>

<! -- Use the "setinterval ()" method of JavaScript to refresh the "loader" page every four seconds. -->
<Body onload = "window. setinterval ('location. Reload () ', 4000);">
</Body>
========================================================

"Form" Page:
================================
<? PHP

Session_start ();

If (! Isset ($ timestamp )){
$ Timestamp = 0;
}

// display the form to generate the "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 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"];
}< BR >}< br> session_register ("timestamp");

?>

}< BR >?>

"Method =" Post ">
message:


=============================================== =========================< br> note: we will immediately refresh the "display" page when submitting a speech on the "form" page, which can be displayed immediately for the spokesman.
.

As you can see, this chat room is very simple, that is, to minimize the customer/service (C/S) data transmission skills, you can use this technology to achieve the most
Small data transmission, remind you again: this technology is not used in chat rooms!
========================================================== ====================
The above test passed on WIN98 + apache1.3 + php4.03 platform!
If you have any suggestions or questions, please contact FeiyangCommunityContact (http://feiyschool.51.net)
Or send an email to the feiyhy@sina.com.
Please give me more information. Thank you!
====================================
Author: Luis Argerich, Alejandro mitrou
Http://www.phpbuilder.com:

[This article is copyrighted by the author and osuo. If you need to reprint it, please indicate the author and its source]

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.