namibia
df0489e1eeeeab5a9bd44e1d84fce49924fe1bac
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
Request.XHRStreaming = new Class({ Extends: Request, lastTextLength: 0, read: 0, //Contain the amout of data read send: function(options) { //mootools set onreadystatechange after xhr.open. In webkit, this cause readyState 1 to be never fired if (Browser.Engine.webkit) this.xhr.onreadystatechange = this.onStateChange.bind(this); return this.parent(options); }, onStateChange: function() { if (this.xhr.readyState == 1) this.dataSent = true; else if (this.xhr.readyState == 3) this.progress(this.xhr.responseText, this.xhr.responseXML); this.parent(); }, onProgress: function(){ this.fireEvent('progress', arguments); }, progress: function(text, xml){ var length = text.length; this.read += length; text = text.substr(this.lastTextLength); this.lastTextLength = length; this.onProgress(this.processScripts(text), xml); } }); APE.Transport.XHRStreaming = new Class({ maxRequestSize: 100000, Implements: APE.Request.SSE, initialize: function(ape){ this.ape = ape; this.requestFailObserver = []; //If browser support servent sent event, switch to SSE / XHR transport if (this.SSESupport) this.ape.options.transport = 4; this.streamInfo = { timeoutObserver: null, cleanClose: false, forceClose: false, callback: null } }, send: function(queryString, options) { if (this.SSESupport && !this.eventSource) { this.initSSE(queryString, options, this.readSSE.bind(this)); if (options.requestCallback) this.streamInfo.callback = options.requestCallback; } else { if ((!this.streamRequest || !this.streamRequest.running) && !this.eventSource) { //Only one XHRstreaming request is allowed this.buffer = ''; this.request = this.doRequest(queryString, options); if (options.requestCallback) this.streamInfo.callback = options.requestCallback; } else { //Simple XHR request var request = new Request({ url: this.ape.serverUri, onFailure: this.ape.requestFail.bind(this.ape, [-2, this]), onComplete: function(resp) { $clear(this.requestFailObserver.shift()); this.request.dataSent = true;//In the case of XHRStreaming. Request are imediatly close. this.ape.parseResponse(resp, options.callback); }.bind(this) }).send(queryString); this.request = request; //set up an observer to detect request timeout this.requestFailObserver.push(this.ape.requestFail.delay(this.ape.options.pollTime + 10000, this.ape, [1, request])); } return this.request; } }, doRequest: function(queryString, options) { this.streamInfo.forceClose = false; var request = new Request.XHRStreaming({ url: this.ape.serverUri, onProgress: this.readFragment.bindWithEvent(this), onFailure: this.ape.requestFail.bind(this.ape, [-2, this]), onComplete: function(resp) { $clear(this.streamInfo.timeoutObserver); if (this.ape.status > 0) { if (this.streamInfo.cleanClose) this.ape.check(); else this.newStream(); this.streamInfo.cleanClose = false; } }.bind(this) }).send(queryString); request.id = $time(); this.streamRequest = request; //this should no longer exist //this.streamInfo.timeoutObserver = (function() { // this.streamInfo.forceClose = true; // //try to imediatly close stream // if (this.checkStream()) this.newStream(); //}).delay(1000*60, this); return request; }, readSSE: function(data) { this.ape.parseResponse(data, this.streamInfo.callback); this.streamInfo.callback = null; }, readFragment: function(text){ this.streamInfo.canClose = false; if (text == '') { this.streamInfo.canClose = true; this.streamInfo.cleanClose = true; this.ape.parseResponse(text, this.streamInfo.callback); this.streamInfo.callback = null; } else { text = this.buffer + text; var group = text.split("\n\n"); var length = group.length; // If group.length is gretter than 1 the fragment received complete last RAW or contains more than one RAW if (group.length > 1) { //Clear buffer this.buffer = ''; for (var i = 0; i < length-1; i++) { this.ape.parseResponse(group[i], this.streamInfo.callback); } if (group[length-1] !== '') { //Last group complete last received raw but it's not finish this.buffer += group[length-1]; } else { //Received fragment is complete this.streamInfo.canClose = true; if (this.checkStream()) this.newStream(); } //Delete callback this.streamInfo.callback = null; } else {//Fragement received is a part of a raw this.buffer = text; } } }, running: function() { return (this.streamRequest && this.streamRequest.running) ? true : this.eventSource ? true : false; }, checkStream: function() { return (this.streamInfo.forceClose && this.streamInfo.canClose) || (this.streamRequest && this.streamRequest.read >= this.maxRequestSize && this.streamInfo.canClose); }, newStream: function() { // this.ape.request.send('CLOSE');//This will close the stream request $clear(this.streamInfo.timeoutObserver); this.streamRequest.cancel(); this.ape.check(); }, cancel: function(){ if (this.request) this.request.cancel(); $clear(this.streamInfo.timeoutObserver); $clear(this.requestFailObserver.shift()); } }); APE.Transport.XHRStreaming.browserSupport = function() { if (Browser.Features.xhr && (Browser.Engine.webkit || Browser.Engine.gecko)) { return true; /* Not yet if (Browser.Engine.presto && ((typeof window.addEventStream) == 'function')) return true; else if (window.XDomainRequest) return true; else return Browser.Engine.trident ? 0 : true; */ } else if (Browser.Features.xhr) return 0;//No XHRStreaming support switch to long polling else return 2;//No XHR Support, switch to JSONP } |
Commits for namibiapublic/ape-source/Transport/Transport.XHRStreaming.js
Revision | Author | Commited | Message |
---|---|---|---|
df0489 ... | Mark | Fri 14 Oct, 2016 10:01:00 +0000 | initial commit |