Server Push specific implementation (turn)

Source: Internet
Author: User
Tags current time header mixed split client advantage linux
After the server flops "push" to publish faster, I have received a lot of friends letter, I hope to know more about the SERVER push technical information, to tell the truth about this new technology is not too understanding of the author, but in accordance with the idea of good faith, here according to the author's actual use of the situation to make some detailed introduction, Want to give a little help and reference to friends who are interested in the SERVER push. Also hope to be able to use this to get the feel free of the master.

"Push" for space reasons, only a general introduction to the server push, let's look at the specific implementation of the server push process:
First, the operating system:
Currently, SERVER push is not implemented on Win9x, at least I have not yet implemented this feature on Win9x as a host. Through the author's successful operating system has: WINNT4.0,UNIX,LINUX,FREEBSD, with LINUX support the best. Because I am using Linux is the latest version, you can easily compile the latest functional modules. Of course, there are other operating systems that can also support the SERVER push function well, but the author is limited to the condition has not been tested.
Second, www server:
In fact, in addition to IIS and Apache two WWW servers, you do not have a better choice, it is noteworthy that they are very good support for the server PUSH, of course, you should choose their latest version. The other kids who live in a house like the WWW server don't have to try. Here, I highly recommend the installation of Apache on Linux, so that you can freely compile the function module, so that you can fully use the server PUSH, FASTCGI, e-cgi, HPH and other new extension features.
Third, script support:
Although SERVER push can be written with more scripting, it is highly recommended that you write CGI scripts in Perl or C, because Perl is the de facto standard for CGI scripting. And in PERL5, you can call the C subroutine directly without any declaration. No matter what language you use to write a CGI script, the interpreter should have CGI.PM module, the author of the trial version is 5.004, this module of the updated version you can download from the Internet, could not find? Use the search engine to enter cgi.pm on the line, how many are there.
Well, with the above tools, let's write a SERVER push program to illustrate its implementation:
#!/usr/bin/perl
Use CGI qw/:p ush-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;
}
Use CGI qw/:p ush-nph/; this line tells the CGI interpreter to invoke the function module (CGI) that supports server push. PM), in this module has predefined three number of Han: Multipart_init (), Multipart_start () and Multipart_end, as long as in the program has a use CGI qw/:p ush-nph/declaration. You can use these three numbers directly.
The function of the Multipart_init () is to declare a document type, like the "content-type:text/html" declaration in a common CGI program, telling the server that the HTML document is being routed, and Multipart_init () The file type declared by this number is the most fundamental type of method to implement the server push: Content-type:multipart/x-mixed-replace; Boundary= '-- Boundary ' If you have a detailed understanding of the MIME type of HTTP, you know the multipart type is a composite type, its subtype is mixed, and the subtype's x parameter you can refer to the detailed MIME type description, where the most critical is the Replace method, which is constantly updated with the newly received data The old data. This is the biggest technical implication 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 fully understand this, as long as the Multipart/x-mixed-replace in the script program declared; Boundary= '--boundary ' is a document type, and the server establishes a special link with the client. The server continuously pushes the data requested by the client to the client. The traditional client Pull method is: 1, client and server to establish a link. 2, the client sends the request. 3, the server responds to requests. 4. The server disconnects to respond to the next request. By comparing the two methods, you can see that the advantage of the server push is primarily the way the server and the client are connected. So that it does not need to use the client pull <meta http-equiv=refresh content= "n" > This method to refresh the page, not only faster update, and will not produce flashing effect.
As you can see, if you don't use Multipart_init (), you can do the same thing directly in a scripting program:
Print "content-type:multipart/x-mixed-replace;boundary= ' boundary ' nn"
The advantage of this is that when the system cannot support Multipart_init (), it can be declared directly.
The Multipart_start () is a specification for the server to send a certain document, if not this number, the content server based on the script can directly transfer the specified data to the client, of course, the client does not recognize this type of document can be ignored without generating errors. But if you indicate specific document types, such as text/plain in routines (plain text types), the client does not analyze the document to improve processing speed. Otherwise, the client only knows that the multipart (compound type) is being sent over to analyze it again.
As for the Multipart_end culvert number does not need to be explained in detail, only explained the end of a push process.

In order to enable us to better understand the detailed technical content of these three numbers, I have analyzed the realization process of these three numbers to everyone:
Sub Multipart_init {
My ($self, @p) = Self_or_default (@_);
My ($boundary, @other) = $self->rearrange ([boundary],@p);
$boundary = $boundary | | '-------=_aaaaaaaaaa0 ';
$self->{' separator '} = "n--$boundaryn";
$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 '};
}
Others in the use of the server push some of the skills, limited to space I can not do too much introduction, you can go to the following address I use the server push write chat room, welcome interested friends and I exchange.
Chat room Address: http://wangjh.3322.net/chat.htm, confidential Oh, this is a free personal homepage space, do not let chat room, network management know will be Del.

Original Author: Wangjinhua
Source: http://bj.netease.com



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.