// Learn cc.Class: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html // Learn Attribute: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html var httpcli = cc.Class({ // LIFE-CYCLE CALLBACKS: // onLoad () {}, // start () {}, // update (dt) {}, __ctor__(){ this.cachemsg = []; this._needretry = false; this._retrycount = 3; this._retryms = 3000; // this._retry_tid = setInterval(this._retry.bind(this), this._retryms); }, httpGet(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'); }, httpPost(url, postdata, cbRes, cbErr){ return this.httpsend(url, postdata, cbRes, cbErr, 'POST'); }, httpsend(url, urldata, cbRes, cbErr, smethod, isretry){ if(cc.sys.platform == cc.sys.WECHAT_GAME){ var jsobj = urldata? JSON.parse(urldata): null; var nowtime = new Date().getTime() 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); // } if(cc.canreport){ var reqtime = Number(new Date().getTime() - nowtime) wx.reportPerformance(1002, reqtime) //console.log(reqtime+":"+url); } }, fail: function(res){ console.log("[wx]request fail!"+ JSON.stringify(res)); cbErr && cbErr(-1, res.msg); // cbErr && cbErr(-1, res.msg); }, complete: function(){ console.log("[wx]request complete!"); } }); return xhr; } else if(typeof(XMLHttpRequest) != 'undefined'){ var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState == 4){ if(xhr.status >= 200 && xhr.status < 400) { var response = xhr.responseText; cbRes && cbRes(response); } // }else{ // cbErr && cbErr(xhr.status, xhr.statusText); // } } }; xhr.onerror= function(e) { cbErr() }; xhr.open(smethod, url, true); xhr.send(urldata); return xhr; } return null; }, _retry(){ 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.retrycount * 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); } }); } }, setRetryCount(count){ this.retrycount = count; }, setRetryInterval(millsec){ if(millsec != this._retryms){ this._retryms = millsec; if(this._retry_tid){ clearInterval(this._retry_tid); } this._retry_tid = setInterval(this._retry.bind(this), this._retryms); } }, setNeedRetry(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;