Server PUSH

zhaozj2021-02-17  70

Column: CGI Programming Article ID: 1532 Article category: Original Author: Xiao Han Xue affixed Time: 2000-11-6 13:39:57

Server Push Keywords: ASP, Perl, CGI, Linux, UNIX, NT Server Push Technology The basic idea of ​​pushing technology is to change the browser active query information to the server to actively send information. The server sends a batch of data, the browser displays this data while ensuring the connection to the server. When the server needs to send a batch of data again, the browser displays the data and keeps the connection. In the future, the server can still send bulk data, and the browser continues to display the data, and push it accordingly. Client Pull In the client drag technology, the server sends a batch of data, inserts instructions in the HTTP response or document header mark, letting the browser "or" 10 seconds in 5 seconds "or" 10 seconds Go to a URL to load data. " When the specified time is reached, the client will do it according to the instructions of the server, or refresh the current data, or transfer new data. In fact, the two technical means of Push and Pull are very different, but the purpose is almost consistent, and it is to provide the latest information to end users. In the server push technology, the HTTP connection remains until the server knows that it has ended the data and sends an end signal, or the client interrupts. In the client drag technology, the HTTP connection is not maintained. In contrast, the client is told to establish a new connection, and the establishment of the connection is what data is obtained. In the server push, the wonderful mime is MIME "Multipart / Mixed" format, which enables a packet (or HTTP response) containing a lot of data items, in the client drag, wonderful place in the HTTP response header (or Equivalent HTML element), which can inform the client to perform what actions after the specified delay time. Server push is usually more efficient than client drag efficiency because it does not have to create new connections for subsequent data. Because it is always connected, even if there is no data transfer, the server must be willing to assign these TCP / IP ports, which will be a serious problem for servers with limited TCP / IP ports. The client drag is low because this must establish a new connection for transferring data each time. But it doesn't have to always be connected. In actual conditions, establishing an HTTP connection usually requires a considerable amount of time, up to one second or more. Therefore, from performance, the server push is more attractive to end users, especially for frequent updating information. Another advantage that the server push relative to client drag is that the server push is relatively easy to control. For example, the server keeps a connection every time, but it can close any connections at any time without the need to set a special algorithm on the server. The client drags should be more troublesome in the same case, and each time it is necessary to establish a connection with the server, the server needs to use a fairly troublesome algorithm to handle the client drag request with a particular end user match. If the CGI program pushed by the server is written in the shell scripting language, there are sometimes some problems. For example, the client end user interrupts, the shell program usually cannot notice that this will make the resource useless waste, solve this problem is to write such a CGI program with Perl or C to interrupt the user. Can end run. As mentioned above, in the server push, multiple responses are always maintained, so that the server can send more data at any time. A significant benefit is that the server can fully control the time and frequency of the update data. In addition, this method is high efficiency because it is always connected. The disadvantage is that keep the connection status will waste the resources of the server. Server push is relatively easy to interrupt. Next, it is probably that the server push technology server is in response request, and HTTP uses the MIME packet format to encapsulate data. Usually an HTTP response can only contain one block.

But MIME has a mechanism to use a message (or HTTP response) to represent multiple data blocks, this mechanism is the standard MIME type of "multipart / mixed". The MULTIPART / MIXED packets are generally implemented as follows: content-type: multipart / mix; boundary = thisrandomstring - heSrandomstring Content-Type: Text / Plain's data of the first object. --Thisrandomstring Content-Type: Text / Plain's data of the second object. --Thisrandomstring - The above message includes two data blocks, the type of the two is "text / plain". The two short lines (-) after the last "thisrandomstring" indicate the end of the packet, and there is no data behind. For server push, use a "multipart / mixed" variant - Multipart / X-Mixed-Replace. Here, "X-" is a type of experiment. "Replace" means that each new data block replaces the previous data block. That is, the new data is not attached to the old data, but replaces it. The following is the "multipart / x-mixed-replace" type actually used: content-type: multipart / x-mixed-replace; boundary = thisrandomstring --Thisrandomstring Content-Type: Text / Plain's first object data - heSrandomstring Content-Type: Text / Plain The data of the second (last) object. --ThisrandomString - The key to using this technology is that the server is not pushing the entire "multipart / x-mixed-replace" message, but each time the data block is sent. The HTTP connection is always maintained, so the server can push new data according to the speed and frequency you need. The browser between the two blocks is only waiting in the current window, and the user can even go to other windows, and when the server needs to send new When data, it is just the source (ABC input method, the word * & ^ $ #) Transport pipe sends a data block, and the client's corresponding window is self-updated. In the server push technology, the message of the "Multipart / X-Mixed-Replace" type consists of the unique boundary line that divides each data block. Each data block has its own header, thus can specify the object-related content type and other information. Since the "Multipart / X-Mixed-Replace" feature is the previous data object, the latest data object is always displayed in the browser. "Multipart / X-Mixed-Replace" packet has not ended. That is, the server can always be connected and transmit the required data. If the user is no longer displayed in the browser window, or the browser to the middle of the connection between the server (for example, the user presses the "STOP" button), the server's push will be interrupted. This is a typical way that people use servers. When the browser finds the "Content-Type" header or arrival at the end of the header, the previous document in the browser window is cleared and begins to display the next document. When the next message boundary is found, it is considered that the current data block (document) has ended. In summary, the data pushed by the server consists of a set of head labels (usually including "content-type"), the data itself, and the splitter (packet boundary). When the browser sees the split, it keeps the state unchanged until the next data block arrives.

The above concept is implemented with a programming method, and the actual server push program can be obtained. For example, the following UNIX shell program will make the browser to display a list of processes on a server every 5 seconds: #! / Bin / sh echo "http / 1.1 200" echo "Content-Type: Multipart / X-Mixed-Replace; Boundary = - thisrandomstring - "echo" "echo" --thisrandomstring - "while true do echo" content-type: text / html "echo" "echo" H2Processes On this machine Updated Every 5 Seconds / H2 "Echo" TIME : "Date Echo" p "echo" PLAINTEXT "ps -el echo" --thisrandomstring-- "Sleep 5 DONE noted that the boundary settings were sent before the SLEEP statement, which ensures that the browser clears its buffer and displays the received received The latest data. NCSA HTTPD users cannot use spaces in the content type, including boundary parameters. NCSA HTTPD can only use strings without space characters as the content type. If there is a space in the content type line (except the space behind the colon), any text after the space will be deleted. The following example is correct: content-type: multipart / x-mixed-replace; boundary = thisrandomstring and the next example does not work properly because it has spaces in the middle: Content-Type: Multipart / X-Mixed-Replace; Boundary = Another advantage of thisrandomstring server push is that it can be done for a single inline image. A document including images can be updated by the server timing or level cycle. This is very simple to achieve: just point the src property of the IMG element to push the URL of a series of images. If the server pushes for a single inline image, the image in the document will be replaced by the newly pushed image again and again, and the document itself does not need to change (assuming that the document does not perform server push). In this way, limited animations in the web page can be replaced by static screens. A simple usage of the client towed client drag is to automatically overload the document by fixed cycle. For example, consider the following HTML document: Document One </ title> <h1> this is document one! </ H1> here's some text. <P> if Load it into a browser that supports dynamic documents (Netscape 1.1 or more, Internet Explorer and Mosaic also support client drag), which will overload them once every other second. Since the META element is actually analog HTTP response header in the HTML document, it can inform the browser to use your own information as an HTTP response. The Meta tag in the above example is equivalent to: Refresh: 1, in this way, is actually the HTTP header tells the browser to update a document every second.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-29551.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="29551" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.046</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'f9X3jUQjgBLAdS_2FD8Av3NuYpKmfmnVMT6iBdklLYhJ_2F_2FpBKv2ulPVJUivy3FPdMwGQ0Q8ziWGcdk9vwpduDCnw_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>