mirror of
https://github.com/shanmiteko/LotteryAutoScript.git
synced 2026-06-04 21:01:17 +08:00
feat(lib/HttpRequest.js): 出现不期待响应时重传
This commit is contained in:
parent
de333f0e65
commit
49949f1dfc
@ -26,8 +26,10 @@ const { request: http_request } = require('http');
|
||||
const { request: https_request } = require('https');
|
||||
const { stringify } = require('querystring');
|
||||
/**超时时间 */
|
||||
const timeout = 20000;
|
||||
/**超时尝试次数 */
|
||||
const TIMEOUT = 20000;
|
||||
/**出错等待时间 */
|
||||
const WAIT = 10000;
|
||||
/**错误尝试次数 */
|
||||
let retry = 6;
|
||||
/**
|
||||
* @description 简化HTTP请求
|
||||
@ -40,7 +42,7 @@ const HttpRequest = detail => {
|
||||
, content = formatContents(headers, contents)
|
||||
, request = thisURL.protocol === 'http:' ? http_request : https_request;
|
||||
let options = {
|
||||
timeout,
|
||||
timeout: TIMEOUT,
|
||||
method,
|
||||
host: thisURL.host,
|
||||
path: thisURL.pathname,
|
||||
@ -56,13 +58,36 @@ const HttpRequest = detail => {
|
||||
default:
|
||||
return failure(`未实现的请求方法: ${method}`);
|
||||
}
|
||||
const req = request(options, res => resDataHandler(res, success, failure));
|
||||
const req = request(options, res => {
|
||||
let protodata = '';
|
||||
const { statusCode, headers } = res;
|
||||
res.setEncoding('utf8')
|
||||
.on('data', chunk => { protodata += chunk })
|
||||
.on('error', async err => {
|
||||
console.log(err.message);
|
||||
if (retry--) {
|
||||
console.log('[不期待响应]尝试重新请求中...');
|
||||
await delay(WAIT);
|
||||
HttpRequest(detail);
|
||||
} else {
|
||||
failure(`[响应错误]${err.message} 响应数据:\n${protodata}`)
|
||||
}
|
||||
})
|
||||
.on('end', () => {
|
||||
if (statusCode < 400) {
|
||||
success({ headers: headers, body: protodata })
|
||||
} else {
|
||||
res.emit('error', new Error(`HTTP状态码: ${statusCode}`))
|
||||
}
|
||||
})
|
||||
});
|
||||
if (method === 'POST') req.write(content);
|
||||
req.on('timeout', () => { req.destroy(new Error('请求超时')) })
|
||||
.on('error', err => {
|
||||
.on('error', async err => {
|
||||
console.log(err.message);
|
||||
if (retry--) {
|
||||
console.log('[请求失败]尝试重新连接中...');
|
||||
await delay(WAIT);
|
||||
HttpRequest(detail);
|
||||
} else {
|
||||
failure(`[请求失败]: ${err.message}`);
|
||||
@ -86,23 +111,16 @@ function formatContents(headers, contents) {
|
||||
return '';
|
||||
}
|
||||
/**
|
||||
* @private
|
||||
* @param {import("http").IncomingMessage} res
|
||||
* @param {SuccessCb} success
|
||||
* @param {FailureCb} failure
|
||||
* @returns {void}
|
||||
* 延时函数
|
||||
* @param {number} time ms
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
function resDataHandler(res, success, failure) {
|
||||
let protodata = '';
|
||||
const { statusCode, headers } = res;
|
||||
res.setEncoding('utf8')
|
||||
.on('data', chunk => { protodata += chunk })
|
||||
.on('error', err => failure(`[响应错误] ${err.message}`));
|
||||
if (statusCode < 400) {
|
||||
res.on('end', () => success({ headers: headers, body: protodata }))
|
||||
} else {
|
||||
res.on('end', () => failure(`[响应错误]错误码:${statusCode} 响应数据:\n${protodata}`))
|
||||
}
|
||||
function delay(time) {
|
||||
return new Promise(resolve => {
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, time);
|
||||
});
|
||||
}
|
||||
module.exports = {
|
||||
HttpRequest
|
||||
|
||||
@ -31,7 +31,7 @@ class Monitor extends Public {
|
||||
this.tagid = await BiliAPI.checkMyPartition(); /* 检查关注分区 */
|
||||
this.attentionList = await BiliAPI.getAttentionList(GlobalVar.myUID); /* 获取关注列表 */
|
||||
const alldyid = await MyStorage.getDyid(); /* 获取储存的之前转过的所有动态 */
|
||||
if (alldyid.length < 5) {
|
||||
if (alldyid.length < 19) {
|
||||
const { allModifyDynamicResArray: AllDynamic } = (await this.checkAllDynamic(GlobalVar.myUID, 5));
|
||||
let newdyid = '';
|
||||
for (let index = 0; index < AllDynamic.length; index++) {
|
||||
|
||||
6
test/getDyid.js
Normal file
6
test/getDyid.js
Normal file
@ -0,0 +1,6 @@
|
||||
const { getDyid } = require("../lib/MyStorage");
|
||||
|
||||
(async () => {
|
||||
let alldyid = await getDyid();
|
||||
console.log(alldyid);
|
||||
})()
|
||||
16
test/httpreq.js
Normal file
16
test/httpreq.js
Normal file
@ -0,0 +1,16 @@
|
||||
const { HttpRequest } = require("../lib/HttpRequest");
|
||||
|
||||
HttpRequest({
|
||||
method: 'GET',
|
||||
url: 'https://api.bilibili.com/x/article/creative/draft/view',
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
|
||||
Accept: 'application/json, text/plain, */*',
|
||||
},
|
||||
success: res => {
|
||||
console.log(res.body);
|
||||
},
|
||||
error: err => {
|
||||
console.log(err);
|
||||
}
|
||||
})
|
||||
Loading…
Reference in New Issue
Block a user