From 44e3027f6c5f941618cdbde0ba9253a4d13d2159 Mon Sep 17 00:00:00 2001 From: zhiyu1998 <542716863@qq.com> Date: Tue, 1 Oct 2024 15:16:58 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=88=20pref:=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=9B=B4=E6=92=AD=E5=88=87=E7=89=87=EF=BC=8C=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E7=A8=8B=E5=BA=A6=E9=81=BF=E5=85=8D=E5=86=85=E5=AD=98=E6=B3=84?= =?UTF-8?q?=E9=9C=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/tools.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/apps/tools.js b/apps/tools.js index c4dd6fe..67ba511 100644 --- a/apps/tools.js +++ b/apps/tools.js @@ -481,11 +481,18 @@ export class tools extends plugin { * @param second */ async sendStreamSegment(e, stream_url, second = this.streamDuration) { - const outputFilePath = `${ this.getCurDownloadPath(e) }/stream_10s.flv`; + const outputFilePath = `${ this.getCurDownloadPath(e) }/stream_${second}s.flv`; await checkAndRemoveFile(outputFilePath); + // 创建一个取消令牌 + const CancelToken = axios.CancelToken; + const source = CancelToken.source(); + try { - const response = await axios.get(stream_url, { responseType: 'stream' }); + const response = await axios.get(stream_url, { + responseType: 'stream', + cancelToken: source.token, + }); logger.info("[R插件][发送直播流] 正在下载直播流..."); const file = fs.createWriteStream(outputFilePath); @@ -493,13 +500,28 @@ export class tools extends plugin { // 设置 streamDuration 秒后停止下载 setTimeout(async () => { - logger.info(`[R插件][发送直播流] 直播下载 ${ this.streamDuration } 秒钟到,停止下载!`); - response.data.destroy(); // 销毁流 + logger.info(`[R插件][发送直播流] 直播下载 ${ second } 秒钟到,停止下载!`); + // 取消请求 + source.cancel('下载时间到,停止请求'); + response.data.unpipe(file); // 取消管道连接 + file.end(); // 结束写入 await this.sendVideoToUpload(e, outputFilePath); - file.close(); }, second * 1000); + + // 监听请求被取消的情况 + response.data.on('error', (err) => { + if (axios.isCancel(err)) { + logger.info('请求已取消:', err.message); + } else { + logger.error('下载过程中发生错误:', err.message); + } + }); } catch (error) { - logger.error(`下载失败: ${ error.message }`); + if (axios.isCancel(error)) { + logger.info('请求已取消:', error.message); + } else { + logger.error(`下载失败: ${ error.message }`); + } await fs.promises.unlink(outputFilePath); // 下载失败时删除文件 } }