var httpcli = function(){ // LIFE-CYCLE CALLBACKS: // onLoad () {}, // start () {}, // update (dt) {}, this.init=function(){ this.cachemsg = []; this._needretry = false; this._retrycount = 3; this._retryms = 3000; this._runningcount = 0; this._maxrunning = 10; // this._retry_tid = setInterval(this._retry.bind(this), this._retryms); }; this.httpGet=function(url, cbRes, cbErr){ // var realurl = url; // if(urldata){ // if(realurl.indexOf("?") == -1){ // realurl += "?"; // }else{ // realurl += "&"; // } // realurl += encodeURIComponent(urldata); // } return this.httpsend(url, null, cbRes, cbErr, 'GET'); }; this.httpPost=function(url, postdata, cbRes, cbErr){ return this.httpsend(url, postdata, cbRes, cbErr, 'POST'); }; this.httpsend=function(url, urldata, cbRes, cbErr, smethod, isretry){ if(this._runningcount < this._maxrunning){ return this._httpsend(url, urldata, cbRes, cbErr, smethod, isretry); }else if(!isretry){ let obj = { u: url, v: urldata, successcb: cbRes, errcb: cbErr }; this.cachemsg.push(obj); return null; } }; this._httpsend=function(url, urldata, cbRes, cbErr, smethod, isretry){ this._runningcount++; let self = this; if(typeof(XMLHttpRequest) != 'undefined'){ var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState == 4){ self._runningcount--; if(xhr.status >= 200 && xhr.status < 400) { var response = xhr.responseText; cbRes && cbRes(response); }else{ if(!isretry && self._needretry){ let obj = { u: url, v: urldata, successcb: cbRes, errcb: cbErr }; self.cachemsg.push(obj); }else{ cbErr && cbErr(xhr.status, xhr.statusText); } } } }; // xhr.onabort = function(){ // self._runningcount--; // }; xhr.onerror = function(){ self._runningcount--; }; xhr.open(smethod, url, true); xhr.send(urldata); return xhr; }else if(typeof(wx) != 'undefined'){ var jsobj = urldata? JSON.parse(urldata): null; var xhr = wx.request({ url: url, data: jsobj, method: smethod, success: function(res){ console.log("[wx]request success!" + res.statusCode); if(res.statusCode >= 200 && res.statusCode < 400) { var restext = JSON.stringify(res.data); cbRes && cbRes(restext); }else{ cbErr && cbErr(res.statusCode, res.msg); } }, fail: function(res){ console.log("[wx]request fail!"+ JSON.stringify(res)); if(!isretry && self._needretry){ let obj = { u: url, v: urldata, successcb: cbRes, errcb: cbErr }; self.cachemsg.push(obj); }else{ cbErr && cbErr(-1, res.msg); } // cbErr && cbErr(-1, res.msg); }, complete: function(){ self._runningcount--; console.log("[wx]request complete!"); } }); return xhr; } return null; }; this._retry=function(){ if(this.cachemsg.length > 0){ this.cachemsg.forEach(element => { let obj = element; if(obj.retrying){ return; } let bneedretry = false; if(!obj.retry_count){ obj.retry_count = 1; obj.passtime = 0; } obj.passtime += this._retryms; bneedretry = (obj.passtime >= obj.retry_count * this._retryms); if(bneedretry && !obj.retrying){ obj.retrying = true; let method = obj.v? 'POST': 'GET'; this.httpsend(obj.u, obj.v, (restext) => { obj.successcb && obj.successcb(restext); let idx = this.cachemsg.findIndex((element) => { return element == obj; }); if(idx != -1){ this.cachemsg.splice(idx, 1); } }, (errcode, errmsg) => { obj.retrying = false; obj.passtime = 0; obj.retry_count++; if(obj.retry_count >= this._retrycount){ obj.errcb && obj.errcb(errcode, errmsg); let idx = this.cachemsg.findIndex((element) => { return element == obj; }); if(idx != -1){ this.cachemsg.splice(idx, 1); } } }, method, true); } }); } }; this.setRetryCount=function(count){ this.retrycount = count; }; this.setRetryInterval=function(millsec){ if(millsec != this._retryms){ this._retryms = millsec; if(this._retry_tid){ clearInterval(this._retry_tid); this._retry_tid = 0; } } if(!this._retry_tid){ this._retry_tid = setInterval(this._retry.bind(this), this._retryms); } }; this.setNeedRetry=function(needretry){ this._needretry = needretry; // if(!this._needretry && this._retry_tid){ // clearInterval(this._retry_tid); // this._retry_tid = 0; // }else if(this._needretry && !this._retry_tid){ // this._retry_tid = setInterval(this._retry.bind(this), this._retryms); // } } }; module.exports = httpcli;