Server Push implementation
--- From "Netease" (Wen / Wangjinhua), "Push", after the release, the author received a number of friends, I hope to learn more about Server Push's technical information, tell the truth to this new technology The author is not too understanding, but it is aware of some detailed introduction based on the actual use of the author, I hope to help those friends who are interested in Server Push. I also hope to take this brick to jade, and ask for experts. Due to the reasons for the space, only the Server Push is a general introduction. Let's take a look at the specific implementation of Server Push: First, the work system: SERVER PUSH can not be implemented on Win9x, at least I am still This function is not implemented on the Win9X as a host. Successful operations in the post-test system: Winnt4.0, UNIX, Linux, FreeBSD, best in Linux support. Because the Linux I use is the latest version, it can easily compile the latest functional modules. Of course, there are other operating systems that can also support the Server Push feature, but the author is limited to the condition. Second, WWW server: In fact, in addition to IIS and Apache these two WWW servers, you have no better choice, it is worthy of you to support Server Push, of course, you should choose their latest version. Other WWW servers don't have to tried other children's WWW servers. Here, the author is recommended to install Apache on Linux so that the functional module can be freely used so that new extensions such as Server Push, Fastcgi, E-CGI, HPH is fully utilized. Third, script support: Although Server Push can be written in more scripts, 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 Perl5, 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 trial is 5.004. The update version of this module can download it from the Internet. Can you find it? Enter cgi.pm with search engines, how many are available. Ok, there is a tool above, 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", scarar (localtime), "n", multipart_end; SLEEP 1;} USE CGI QW /: Push -nph /; This line tells the CGI interpretation program that calls support for Server Push's functional module (CGI.pm), pre-defined three cullets in this module: multipart_init (), Multipart_Start () And Multipart_end, as long as there is a USE CGI QW /: Push -Nph / declaration in the program. These three cullets can be used directly.
Multipart_init () The role of Multipart_init () is to declare a document type, just like the "Content-Type: Text / HTML" declaration in the normal CGI program, tell the server under the HTML document, and Multipart_init () The document type of the declaration is the most fundamental method type of Server Push: Content-Type: Multipart / X-Mixed-Replace; Boundary = '- Boundary' If you have a detailed understanding of HTTP's MIME type, you know Multipart Type is a composite type. Its subtype is MIXED, and you can see a detailed MIME type description. The most critical is the Replace method, which is not stopped with newly received data. Old data. This 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 multipart / x-mixed-replace; boundary = '- Boundary' in the script, the server has established a special link 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 link. 2, Client Send a request. 3. Server response request. 4. Server disconnects to respond to the next request. By the two methods, you can see that the Server Push advantage is mainly in this uninterrupted connection between servers and clients. Make it not need to use Client Pull This method refreshes the page, not only the update is fast, but does not generate flash effect. It can be seen that if you don't need multipart_init (), you can use this statement directly in the script to achieve the same purpose: Print "Content-Type: Multipart / X-Mixed-Replace; Boundary = 'boundary'nn" The benefit is that when the system does not support multipart_init (), you can declare this. Multipart_start () This mull is specified for the server to transmit a determined document. If this all have, according to the content server according to the script Directly transmit the specified data to the client, and of course the client cannot identify this type of document to ignore without errors. However, if you specify the specific document type, such as the text / place in the routine, the client should not analyze the document to increase the processing speed. Otherwise, the client only knows that the transfer is Multipart (composite type) to re-analyze it. As for the Multipart_end, the Monte None does not require a detailed description, but the end of a PUSH process is explained.
In order to make everyone better understand these three detailed technical content, I analyze the implementation process of these three cullets to everyone: SUB MULTIPART_INIT = SELF_OR_DEFAULT (@_); My ($ boundary, @ @ @ @ @ = $ self-> @p); $ boundary = $ boundary || '------ = _AAAAAAAAAAA0'; $ Self -> {'Separetor' } = "n - $ boundaryn"; $ TYPE = Server_Push ($ boundary); Return $ Self-> Header (-nph => 1, -type => $ TYPE, (Map {split "=", $ _, 2} @others,). $ Self-> multipart_end;} Sub multipart_start {MY ($ Self, @p) = self_or_default (@_); my ($ type, @) = $ Self-> Rearrange ([TYPE ], @p); $ type = $ type || 'Text / html'; return $ self-> header (-type => $ type, (Map {split "=", $ _, 2} @other), );} SUB MULTIPART_END = SUB MULTIPART_END = Self_OR_DEFAULT (@_); Return $ Self -> {'Separetor'};} Other Some Tips When using Server Push, I can't make any more Excessive introduction, everyone can take the address I use the chat room written by Server Push, welcome friends with interest to communicate with me. Chat Room Address: http://wangjh.3322.net/chat.htm, confidential, this is a free personal page space, not letting chat room, network management knows Del.