I. Application scenariosA persistent connection (HTTP link) is maintained between the browser and the server, and the server pushes the latest data to the front-end in a timely manner. Typical scenario: New Mail Arrival notification.
2. Common solutions in the industryRegular round robin, long round robin, and websocket (New HTML5 capability) have good compatibility and wide application, but do not apply this technology in mobile networks.
3. persistent connection front-end code
/*** PNS model layer * @ constructs m2012.model. PNS. pnsmodel * @ extends backbone. model * @ example * New m2012.model. PNS (). startrequestpns (); */m139.namespace ("m2012.model. PNS ", {pnsmodel: backbone. model. extend ({callapi: m139.richmail. API. call, pnsurl: '/pns/poll? SID = '+ top. sid + '& comefrom = 100', // The PNS interface address is used to push messages to the frontend pnserrorinterval: 1003*1000. // The interval at which the next interface is re-called after an error is reported by the interface: pnstimer: null, // call the interface again after the interface reports an error. pnstpes is used to report the error of the Message Type mailmsg: 70} pushed by the timer ID msgtypes: {// PNS, logger: New top. m139.logger ({name: "m2012.model. PNS "}), initialize: function () {$ app. registermodel ("pnsmodel", this) ;}, startrequestpns: function () {var self = This; var options = {}; Options. method = 'get'; options. timeout = self. pnserrorinterval; options. error = function () {self. callpns (); self. logger. error ("newmailarrival callpns error") ;}; options. ontimeout = function () {self. callpns (); self. logger. error ("newmailarrival callpns timeout") ;}; options. issendclientlog = false; m139.richmail. API. call (self. pnsurl, null, function (e) {var result = E. responsedata; If (result ){ Console. log (result); If (result. errorcode) {self. callpns (); self. logger. error ("newmailarrival returndata error", "[pns/poll]", result);} else {self. pnsresulthandle (result); $ app. trigger ("pnsnewarrival", result); // leave it to extend self later. callpns (500); // self. autoreceivemail () ;}} else {self. callpns (); self. logger. error ("newmailarrival returndata error", "[pns/poll]", result) ;}, options) ;}, callpns: Function (seconds) {var self = This; // determines whether the user has logged on for timeout. If (top. $ app. issessionout () {console. Log ('logon timeout! No longer request PNS '); return;} If (self. pnstout) {cleartimeout (self. pnstimer);} self. pnstout = setTimeout (function () {self. startrequestpns () ;}, seconds | self. pnserrorinterval);},/** this function can be used for unit testing * $ app. getModel ("pnsmodel "). pnsresulthandle ({}); */pnsresulthandle: function (result) {var self = this; if (result. c> 0) {// C indicates the number of VaR msgarr = result. MSG; For (VAR I = 0, L = msgarr. length; I <L; I ++ ){ // Todo process business logic according to Message Type} else if (result. c = 0) {console. Log ('Return timeout! Autoreceivemail ');}}})});
Iv. Exception capture and Application Server(1) The client can capture the xhr error event or start the next Round Robin after 500 milliseconds after the interface times out. (2) The server interface reports an error. Do not start the next round-robin immediately. You can start round-robin after two or more minutes, because the server usually needs to recover to normal for a long time. (3) A server usually needs a special application server to maintain a large number of persistent connections, for example:
Jetty. Tomcat cannot.
V. ReferencesBooks: authoritative guide to Web performance competing products: 163 email receiving
Message push using long polling