How to implement Server Push under Linux
Blue forest http://www.lslnet.com, December 5, 2000 20:15
Due to the openness of Linux, the programmer's creativity is greatly stimulated, so that new technologies for Linux will continue to appear. Server Push is one of them. It can make the website achieve the best performance on the basis of completely no cost. This article will be described in detail herein.
First, environmental configuration
In order to more fully appreciate the function of Server Push under Linux, we need some options in the operating environment.
1.www server
First, you must install Apache on Linux so that you can freely compile the functional module so that new extensions such as Server Push, Fastcgi, Eperl, and PHP.
2. Script support
The author recommends using Perl or C language to write CGI scripts because Perl is already the fact that the CGI script is actually standard. Moreover, in Perl 5, you can also call the C subroutine directly without any claim. No matter what language you use to write the CGI script, there should be a CGI.pm module in the interpreter. The version of the author is 5.004. This module updates can be downloaded from the Internet.
Second, the program example and function analysis
Ok, with the above tool, let's write a Server Push program to explain its implementation process:
#! / Usr / bin / perl USE CGI QW /: Push -Nph /; $ | = 1; Print Multipart-Init (-boundary => '- Boundary'); While (1) {Print Multipart-Start (- Type => 'TEXT / Plain'), "The Current Time IS", Scalar (Localtime), "/ N", Multipart-End; Sleep 1;
The second line of the program tells the CGI interpretation program to call the function module (CGI.pm) supporting Server Push, pre-defined three functions in this module: Multipart-init (), multipart-start () and multipart-end, as long as There are use cgi Qw /: Push -nph / declaration in the program, you can use these three functions directly.
The role of the multipart-init () function is to declare a document type, just like the "Content-Type: Text / HTML" declaration in a normal CGI program, tell the server under the HTML document, and Multipart-init () File types of function declaration are the most fundamental ways to implement Server Push:
Content-type: multipart / x-mixed-replace; boundary = '- boundary'
If you have a detailed understanding of HTTP's MIME type, you know that the multipart type is a composite type. Its subtype is a mixed, subtype X parameter, you can see the detailed MIME type description, the most critical thing here is The Replace method, which does not stop the old data with the newly received data, which is the largest technical connotation of Server Push. Boundary is just a boundary value, telling the browser to start from here, using the Server Push method. In fact, you can understand this, as long as you declare a document type such as Multipart / X-Mixed-Replace in the script, the server has established a special connection with the client. The server continually pushes the data source of the client to continue to the client. The traditional Client Pull method is: 1.Client and Server establish a connection
2.Client Send Request to Server
3. Server response request
4. Server disconnects to respond to the next request.
By comparing these two methods, you can see that Server Push advantages are mainly in this uninterrupted connection between servers and clients, making it no need to use Client Pull This method refreshes the page, not only the update is fast, but does not generate flash effect.
If you don't have to use multiple-init (), you can use the following statement directly in the script: Print "Content-Type: Multipart / X-Mixed-Replace; Boundary = 'Boundary' / N / N". When the system does not support the multipart-init () function, you can declare this.
Multipart-start () This function is to specify a determined document for the server. If this function is not used, according to the content server according to the script, you can transfer the specified data to the client, and of course the client cannot identify this type of document. Ignore without incorrectly. But if you specify the specific document type, such as the text / place in the routine (plain text type), the client will no longer analyze the document, thereby improving the processing speed, otherwise the client only knows that the transfer is Multipart. (Composite) and re-analyzes it.
As for the Multipart-End function, it does not need to be described in detail, but the end of a PUSH process is explained.
In order to make everyone better understand the detailed technical content of these three functions, the author introduces the implementation process of these three functions to everyone:
Sub multipart-init {MY ($ Self, @P) = self-or-default (@ -); My ($ boundary, @) = $ Self-> RearRange ([Boundary], @ P); $ boundary = $ Boundary || '------- = -Aaaaaaaaaa0'; $ Self -> {'Separetor'} = "/ n - $ boundary / n"; $ type = server-push ($ boundary); Return $ Self-> Header (-NPH => 1, -type => $ TYPE, (Map {split "=", $ -, 2} @other),) $ self-> multipart-end;} Sub multipart- Start {MY ($ Self, @P) = self-or-default (@ -); My ($ type, @) = $ Self- >RRANGE ([Type], @ P); $ TY = $ TYPE | "TEXT / HTML '; RETURN $ Self-> Header (-type => $ TYPE, (Map {split" = ", $ -, 2} @other,);}
SUB MULTIPART-END {MY ($ Self, @p) = self-or-default (@ -); return $ self -> {'separator'};
Third, application skills
Other techniques using Server Push, limited to space, the author no longer introduces too much. However, because the client does not have a request to have a request for the server for a while, if there is no new content to the browser in a period of time, a connection timeout may occur, and the following program will be handled well:
$ Message = 1;
$ Whilecount = 1;
Do {
Main program;
$ Message ;
}
IF ($ whilecount == 30)
{