Implement ServerPush in Linux

Source: Internet
Author: User
In Linux, implement ServerPush-Linux Enterprise Application-Linux server application information. The following is a detailed description. Due to the openness of Linux, the creativity of programmers is greatly stimulated, and new technologies that enable Linux as the job platform are constantly emerging. Server Push is an example. It can achieve the best performance without increasing the cost. This article will introduce it in detail.
  
     I. Environment Configuration
  
In order to fully appreciate the Server Push function in Linux, we need to make some choices in the runtime environment.
  
1. WWW Server
  
First, install Apache on Linux so that the functional modules can be compiled freely. In this way, new extended functions such as Server Push, FastCGI, EPerl, and PHP can be fully used.
  
2. Script support
  
I recommend using Perl or C to write CGI scripts, because Perl is already the de facto standard of CGI script programs. In addition, you can directly call the C subroutine in Perl 5 without any declaration. No matter what language you use to write CGI scripts, the interpreter should contain the CGI. PM module. The version I tried is 5.004. You can download the latest version of this module from the Internet.
  
     Ii. program instance and Function Analysis

Well, with the above tool, we will write a Server Push program to illustrate 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 interpreter to call the function module that supports Server Push (CGI. in this module, three functions are pre-defined: multipart-init (), multipart-start (), and multipart-end, as long as there is use CGI qw/in the program /: push-nph/Declaration, you can directly use these three functions.
  
The multipart-init () function declares a document Type, just like the "Content-Type: text/html" statement in a common CGI program, tell the Server that the following is an HTML document, while the file type declared by the multipart-init () function is the most fundamental method type to implement Server Push:
  
Content-Type: multipart/x-mixed-replace; boundary = '-- boundary ′
  
If you have a detailed understanding of the http mime type, you will know that the multipart type is a composite type, its child type is mixed, and its Child type is X parameter, you can refer to the detailed description of MIME types. The most important here is the replace method, which constantly updates old data with newly received data, which is the biggest technical connotation of Server Push. Boundary is just a boundary value. It tells the browser to use the Server Push method from here. In fact, you can fully understand this. As long as the file type such as multipart/x-mixed-replace is declared in the script program, the server establishes a special connection with the client. The server constantly pushes the data requested by the client to the client. The traditional Client PULL method is:
  
1. establish a connection between the Client and the Server
  
2. The Client sends a request to the Server.
  
3. The Server responds to the request.
  
4. The Server is disconnected to respond to the next request.
  
By comparing the two methods, we can see that the advantage of Server Push lies in the uninterrupted connection between the Server and the client, so that it does not need to use the Client PULL's <META http-equiv = refresh content = "n"> method to refresh the page, not only fast updates, but also no flashing effect.
  
If you do not need multipart-init () and directly use the following statement in the script program, you can achieve the same purpose: print "Content-Type: multipart/x-mixed-replace; boundary = 'boundary '\ n \ n ″. When the system does not support the multipart-init () function, you can directly declare it like this.

The multipart-start () function is used to transmit a specific document to the server. If this function is not used, the content server of the script program can directly transmit the specified data to the client, of course, the client cannot identify this type of document and can ignore it without generating errors. However, if you specify a specific document type, such as text/plain in the routine, the client no longer analyzes the document, thus improving the processing speed, otherwise, the client only knows that the multipart is transmitted and needs to analyze it again.
  
The multipart-end function does not need to be described in detail, but only describes the end of a Push process.
  
To help you better understand the detailed technical content of these three functions, I will introduce the implementation process of these three functions to you:
  
Sub multipart-init {
  
My ($ self, @ p) = self-or-default (@-);
  
My ($ boundary, @ other) = $ self-> rearrange ([BOUNDARY], @ p );
  
$ Boundary = $ boundary | '------- =-aaaaaaaaaa0 ′;
  
$ Self-> {'separator'} = "\ 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, @ other) = $ self-> rearrange ([TYPE], @ p );
  
$ Type = $ type | 'text/html ′;
  
Return $ self-> header (
  
-Type => $ type,
  
(Map {split "=", $-, 2} @ other ),
  
);
  
}
  
Sub multipart-end {
  
My ($ self, @ p) = self-or-default (@-);
  
Return $ self-> {'separator ′};
  
}
  
     Iii. Application Skills
  
Other Tips for using Server Push are limited in length and I will not introduce it too much. However, sometimes the client does not request the server for a period of time, that is, if no new content is input to the browser within a period of time, connection timeout may occur, the following program can be used to solve the problem:
  
$ Message = 1;
  
$ Whilecount = 1;
  
Do {
  
Main program;
  
$ Message ++;
  
}
  
If ($ whilecount = 30)
  
{
  
Print "SCRIPT >$ message </SCRIPT> \ 015 \ 012 ″;
  
$ Whilecount = 0;
  
}
  
$ Whilecount ++;
  
Sleep 1;
  
As you can see, just like the Loop of many FTP software, there is an empty signal when you are idle, so that the program will not be waiting. Every 30 seconds, this program outputs a line of <SCRIPT >$ message </SCRIPT> to the client, while $ message is a number of 1, 2, and 3, this line does not display any content during browser browsing, so that the viewer does not feel it.
Related Article

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.