PHP & JavaScript control series client data storage 1

Source: Internet
Author: User

One thing programmers like to do is to write a program that can generate programs. On the web, we face two different development environments: client (browser) and server. According to the definition of the HTTP protocol, you can write a program on the server, which outputs a program that uses another language that can be executed on the client. Let's select PHP (of course) to write the server program, and select JavaScript to write the client program. In this article, we will show you how to use this combination to store data on the client and minimize data exchange between the server and the browser for some interactive applications, such as chat rooms, news systems, or what you want.

Elements:

PHP4
JavaScript
Frames

Idea:
We will try to develop an HTTP chat room program written in PHP for the moment. For chat, HTTP is not a good protocol, but it is immune to firewalls and proxy servers. We can exploit the potential of PHP without the need for Java applets. There are two main problems with the chat room program: the first is that IE does not support the "push" method, in this way, it needs to be made into an all-pull application (that is, the client automatically refreshes), which is not very natural for a chat program. We intend to make the client refresh Time Adjustable. The server will generate a refresh time based on a function located on the server in the previous x minutes used to receive message data. The second problem is hard to solve: Because the automatic refresh method is used, the server needs to send all the information to the client every time. We estimate that a large amount of transmission will occur. A sample program that simulates a simple chat room model also shows that this is the main cause of chat latency. This article deals with the second and deeper problem.

Common Model:
By using frames, You can refresh a specific frame without re-loading other frames. This is useful for minimizing the transmission of c/s. Our model is based on the following design:


The main file used to define the frame structure.
Load the frame file.
Displays frame files.
In our design, the loading frame is automatically refreshed every "x" seconds. The idea is to save the data in the master file and allow the loading frame file to request the server for data not received by the client. We use timestamps to mark a message, news, or something that can be transmitted, and through it we can know which one needs to be passed to the client, and which one does not. We use the session function of PHP4 to save the "Last timestamp" on the client so that it is visible on the server. When the received data is loaded into the frame file, the data is stored in the main file (note that the main file may be large, but it is transmitted only once), and the display frame file is refreshed. For further optimization, we can make the display frame file as short as possible. In this frame, we only call a "display" JavaScript function, which is obviously saved in the main file, this function uses data stored in the main file to dynamically draw display frames. Let's take a look at this method:



The browser requests the master file (frame structure)
The master file is transmitted from the server. It defines the frame structure, and other frames (loaded and displayed frames) are transmitted.
The load frame file is analyzed on the server. If the client does not have the "timestamp" session variable, it will extract all the data from the service.
And generate JavaScript code to store the data in the main file. Set the "timestamp" session variable.
Load the frame file and generate JavaScript code to refresh the display frame file on the client.
This refresh causes the display frame file to call the "display" function, which generates a display frame based on the data.
Every "x" seconds we return (2)
Our analysis of this method is as follows:

We need three files:

Main file (large, including displaying code and saving variables and initial values)
Mount the frame file (SMALL, CONTAINING php code, used to retrieve data from the server and generate JavaScript code)
Display frame file (very small, only one call to the display function in the main file)


The master file is transmitted only once.
Mount and display frame files are transmitted every "x" seconds.
Loading a frame file may increase when it is called for the first time, and it becomes very short because it only retrieves data that has not been obtained by the client each time.
The same is true for displaying frame files.
Because the results are processed on the client, we can reduce the loading of server data.

Confused? Let's take a look at the example:

In this example, we have created a chat room, which cannot be used yet. It is only used to demonstrate how to implement our model, do not ask "why not add this or that feature in the chat room. If this model is useful, you can use it to create a complex chat room that meets your requirements. Remember that it is not just used to create a chat room.

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.