212 lines
5.9 KiB
JavaScript
212 lines
5.9 KiB
JavaScript
class HttpCli {
|
|
constructor() {
|
|
this.cachemsg = [];
|
|
this._needretry = false;
|
|
this._retrycount = 3;
|
|
this._retryms = 3000;
|
|
this._runningcount = 0;
|
|
this._maxrunning = 10;
|
|
}
|
|
|
|
httpGet(url, cbRes, cbErr) {
|
|
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, contentType, useWeb) {
|
|
if (this._runningcount < this._maxrunning) {
|
|
return this._httpsend(url, urldata, cbRes, cbErr, smethod, isretry, contentType, useWeb);
|
|
} else {
|
|
console.log('[http]running/max:' + this._runningcount + '/' + this._maxrunning);
|
|
console.log('[http]url:' + url);
|
|
if (!isretry) {
|
|
let obj = {
|
|
u: url,
|
|
v: urldata,
|
|
successcb: cbRes,
|
|
errcb: cbErr
|
|
};
|
|
this.cachemsg.push(obj);
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
_httpsend(url, urldata, cbRes, cbErr, smethod, isretry, contentType, useWeb) {
|
|
if (typeof (wx) != 'undefined' && wx.request && !useWeb) {
|
|
return this._wxRequest(url, urldata, cbRes, cbErr, smethod, isretry, contentType);
|
|
} else if (typeof (XMLHttpRequest) != 'undefined') {
|
|
return this._webRequest(url, urldata, cbRes, cbErr, smethod, isretry, contentType);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
_wxRequest(url, urldata, cbRes, cbErr, smethod, isretry, contentType) {
|
|
this._runningcount++;
|
|
let self = this;
|
|
var jsobj = urldata ? JSON.parse(urldata) : null;
|
|
var ct = contentType || 'application/json';
|
|
var xhr = wx.request({
|
|
url: url,
|
|
data: jsobj,
|
|
method: smethod,
|
|
header: {
|
|
'content-type': ct,
|
|
'Cache-Control': 'no-cache'
|
|
},
|
|
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 {
|
|
if (!isretry && self._needretry) {
|
|
let obj = {
|
|
u: url,
|
|
v: urldata,
|
|
successcb: cbRes,
|
|
errcb: cbErr
|
|
};
|
|
self.cachemsg.push(obj);
|
|
} 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;
|
|
}
|
|
|
|
_webRequest(url, urldata, cbRes, cbErr, smethod, isretry, contentType) {
|
|
this._runningcount++;
|
|
let self = this;
|
|
url += url.indexOf('?') >= 0 ? '&__t=' + new Date().getTime() : '?__t=' + Date.now();
|
|
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,
|
|
contentType: contentType
|
|
};
|
|
self.cachemsg.push(obj);
|
|
} else {
|
|
cbErr && cbErr(xhr.status, xhr.statusText);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
// xhr.onabort = function(){
|
|
// self._runningcount--;
|
|
// };
|
|
xhr.onerror = function () {
|
|
console.log('xhr.onerror');
|
|
self._runningcount--;
|
|
};
|
|
xhr.open(smethod, url, true);
|
|
(contentType) && xhr.setRequestHeader('content-type', contentType);
|
|
xhr.send(urldata);
|
|
return xhr;
|
|
}
|
|
|
|
_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.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, obj.contentType);
|
|
}
|
|
|
|
});
|
|
}
|
|
}
|
|
|
|
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 = 0;
|
|
}
|
|
}
|
|
if (!this._retry_tid) {
|
|
this._retry_tid = setInterval(this._retry.bind(this), this._retryms);
|
|
}
|
|
}
|
|
|
|
setNeedRetry(needretry) {
|
|
this._needretry = needretry;
|
|
}
|
|
}
|
|
|
|
module.exports = HttpCli;
|
|
|