


Minimize data transfer - store data on the client_PHP tutorial
Jul 21, 2016 pm 04:07 PM
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>
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"> 🎜> ==================================================
Note: The "form" file is a speech box that provides users with input speech boxes.
"display" file content:
=====================
<script>
top.displaymsgs( );:)
"loader" file:
====================
<?php
session_start (); // Use Sessions here!
if(!isset($timestamp)) {
//If "timestamp" is not defined, define it and set it to 0
$timestamp= 0;
// Find information that the client does not have
$query="select * from testeable where timestamp>'$timestamp'";
$result=mysql_query($query,$dab);
$ msgs = array ();
// In this cycle, we store the latest news/data, and set "TimestAmp" to the current maximum value
While ($ resql_fetch_array ($ Result )) {
$msgs[]=$res["message"];
if($res["timestamp"]>$timestamp) {
$timestamp=$res["timestamp"] ;
🎜> // here Loop we generate javascript code
// Store the latest data obtained from the server into the "master" page (note: use "top" to point to the top window (master)
for($i= 0;$i<$count($msgs);$i++) {
"); ?>";
;
top.display.location.reload();
</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>
<?php
for($i=0;$i<$count($msgs);$i++) {
?>
top.lines[top.lines.length]="<?print("$msgs[$i]");?>";
<?php
}
?>
top.display.location.reload(); //刷新"display"页
</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>
=====================================================
注:我们使得在"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]

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to make Google Maps the default map in iPhone

Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation!

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks

Clock app missing in iPhone: How to fix it

Slow Cellular Data Internet Speeds on iPhone: Fixes

The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times.

Alibaba 7B multi-modal document understanding large model wins new SOTA
