Home > Backend Development > PHP Tutorial > Minimize data transfer - store data on the client_PHP tutorial

Minimize data transfer - store data on the client_PHP tutorial

WBOY
Release: 2016-07-21 16:07:47
Original
1015 people have browsed it

Outputting programs into other languages ​​is one of the things programmers love. On the WEB we have
two different programming environments: the client (browser) and the server side. According to the definition of the HTTP protocol,
we You can write server-side programs that output other languages ​​on the client. We chose server-side language and javascript as the client-side output. In this question we will demonstrate to you how to use
this solution to store data on the client, and reach the server and client in applications such as chat rooms, news systems or other applications you want to
implement ( browser) minimal data transfer.

Requires the following support:
PHP4
JavaScript
Frames

Main idea:

We have been trying to develop a chat room based on HTTP protocol using PHP (HTTP CHAT ROOM),
Although the HTTP protocol is not a good protocol for chat, it can not be affected by firewalls or proxies.
PHP can completely implement this function without using JAVA APPLETS, for chat rooms There are two main problems:
First, because IE does not support SERVER PUSH technology, so we can only use CLIENT PULL technology (that is,
the client automatically refreshes). The second problem is deeper: because the The idea is to refresh on the client, so the server must transmit all messages each time, which means a large amount of data transmission. This is also the main reason for chat room delay. This article attempts to solve this problem. :
Using frame technology (frames) you can refresh specified pages without having to reload other pages, which can
reduce the amount of service/client (C/S) data transmission. Our model is based on this scheme.
"master" file: define the framework structure
"loader" page: import data
"display" page: display data
In this scheme, the "loder" box is automatically refreshed every "x" seconds ——Our idea is to store the data in the "master"
file, so that the "loder" page only needs to request data from the server that the client does not have. We use timestamp (timestamp)
to mark Each message determines which messages must be sent to the client and which do not need to be transmitted. We use PHP4.0's session management (session)
to store the client's last updated timestamp (last timestamp) so that the timestamp is visible to both the server and the client. When the "loader" file
receives data from the "master" file (note: the "master" file is large, but it is only transmitted once), it refreshes the display page ("diaplay")
and "display" The page simply calls a javascript function named "displaymsgs()" in the "master" file to display the message. This function dynamically displays
the data stored in the "master" file. The following is the general flow chart:
1. The browser requests the "master" page (frame), the "master" page is transmitted from the server to the client (browser), and then the "master"
file generates the frame, and the "loader" and "display" pages are transferred to 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
get all the data from the server and generate javascript code to store the data in the "master" file. Then save the "timestamp" variable as
session variable.
3. The "loder" page generates javascript code to refresh the "display" page.
4. The refresh request causes the "display" page to call the "diaplaymsgs()" javascript function to display data
5. Go back to step 2 every "x" seconds

We can think of it as follows:
========================= ===============================
"master" file: very large, defines the displaymsgs() function and storage data and initial values.
"loader" file: small, retrieves data from the server and generates javascript code
"display" file: very small, calls the "diaplaymsgs()" function of the "master" file
==== ================================================== ===
Note: The "master" file is only transferred once
"loder" and "display" files are refreshed every "x" seconds
"loder" may be very large the first time it is transferred , but it will be very small in the future
The "diaplay" file will remain unchanged

If you are still not clear about the above ideas, below we will create a chat room to explain the method in detail. This chat room Just for a simple demonstration
so it may not be very useful, but you can definitely use this idea to build more complex chat rooms, remember this idea is not just for chat rooms. :)

First, please use the mysql database form:
============================
create table testeable (
timestamp datetime,
message text
);
=========================== =
The "master" file is as follows:
===================================== ===========
<script> <br> lines=new Array(); <br> function displaymsgs() { <br> for(i=0;i<lines.length ;i++) { <br> display.document.write(lines[i]); <br> display.document.write('<BR>'); <br>                                                                                                 script> <br>  <frameset cols="1" rows="20,60,20" border="0"> 🎜>                                                                                                                                                             ================================================== <br> Note: The "form" file is a speech box that provides users with input speech boxes.<br><br> "display" file content: <br> ===================== <br> <script> <br> top.displaymsgs( );:) <br><br> "loader" file: <br> ==================== <br> <?php <br><br> session_start (); // Use Sessions here! <br><br> if(!isset($timestamp)) { <br> //If "timestamp" is not defined, define it and set it to 0 <br> $timestamp= 0;                                                                                    <br> // Find information that the client does not have <br> $query="select * from testeable where timestamp>'$timestamp'"; <br> $result=mysql_query($query,$dab); <br> $ msgs = array (); <br> <br> // In this cycle, we store the latest news/data, and set "TimestAmp" to the current maximum value <br> <br> While ($ resql_fetch_array ($ Result )) { <br> $msgs[]=$res["message"]; <br> if($res["timestamp"]>$timestamp) { <br> $timestamp=$res["timestamp"] ; <br>                                                                                                                                                                                                                            🎜>                 // here Loop we generate javascript code <br> // Store the latest data obtained from the server into the "master" page (note: use "top" to point to the top window (master) <br><br> for($i= 0;$i<$count($msgs);$i++) { <br>                                                                                             "); ?>"; <br>                                                                                                                                                                  ; <br> top.display.location.reload(); <br> </script>

Refresh every 4 seconds -->
                                                                                                                                                           ====================================

"form" page:
====================
                                                                                            )) {
$timestamp=0;
}  

        // 显示表单,产生"timestamp"变量.  
        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);  
               // 得到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>  <br>              <?php  <br>                 for($i=0;$i<$count($msgs);$i++) {  <br>            ?>  <br>                top.lines[top.lines.length]="<?print("$msgs[$i]");?>";  <br>            <?php  <br>             }  <br>              ?>  <br>            top.display.location.reload(); //刷新"display"页 <br>              </script>  
            }  
    ?>  
    

" method="post">  
    Message:  
      
    
  
    =====================================================
    注:我们使得在"form"页提交发言时,马上刷新"display"页面,这可以达到对发言人来说马上发言马上
    显示,更体现实时性。

As you can see, this chat room is very simple, this is the Minimum Customer/Service (C/S) data transfer skill, using this technology you can achieve the minimum data transfer, Again, remind you: this technology is not meant for chat rooms!
================================================== ========
The above was tested on win98+apache1.3+php4.03 platform!
If you have any suggestions or questions, please contact Feiyang Community (http://feiyschool.51.net)
Or send an email to feiyhy@sina.com
Please give me your advice, thank you!
================================     
English original author: Luis Argerich, Alejandro Mitrou
English The URL of the original text: http://www.phpbuilder.com

[The copyright of this article is jointly owned by the author and Aosuo.com. If you need to reprint, please indicate the author and source]

http://www.bkjia.com/PHPjc/315052.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315052.htmlTechArticleExporting programs to other languages ​​is one of the things programmers love. On the WEB we have two different Programming environment: client (browser) and server side, according to the definition of HTTP protocol,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template