From 49949f1dfcf8e271785a026d4e0fbd2ddc8f9161 Mon Sep 17 00:00:00 2001 From: shanmite <1971513084@qq.com> Date: Sat, 13 Feb 2021 11:14:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(lib/HttpRequest.js):=20=E5=87=BA=E7=8E=B0?= =?UTF-8?q?=E4=B8=8D=E6=9C=9F=E5=BE=85=E5=93=8D=E5=BA=94=E6=97=B6=E9=87=8D?= =?UTF-8?q?=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/HttpRequest.js | 60 ++++++++++++++++++++++++++++++---------------- lib/Monitor.js | 2 +- test/getDyid.js | 6 +++++ test/httpreq.js | 16 +++++++++++++ 4 files changed, 62 insertions(+), 22 deletions(-) create mode 100644 test/getDyid.js create mode 100644 test/httpreq.js diff --git a/lib/HttpRequest.js b/lib/HttpRequest.js index f0bca8f..b7e7ab3 100644 --- a/lib/HttpRequest.js +++ b/lib/HttpRequest.js @@ -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} */ -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 diff --git a/lib/Monitor.js b/lib/Monitor.js index d18f5e7..d0b4b82 100644 --- a/lib/Monitor.js +++ b/lib/Monitor.js @@ -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++) { diff --git a/test/getDyid.js b/test/getDyid.js new file mode 100644 index 0000000..556f6d1 --- /dev/null +++ b/test/getDyid.js @@ -0,0 +1,6 @@ +const { getDyid } = require("../lib/MyStorage"); + +(async () => { + let alldyid = await getDyid(); + console.log(alldyid); +})() \ No newline at end of file diff --git a/test/httpreq.js b/test/httpreq.js new file mode 100644 index 0000000..e52f1f5 --- /dev/null +++ b/test/httpreq.js @@ -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); + } +}) \ No newline at end of file