gdfgdffsdxc 2023-11-29 14:36 采纳率: 0%
浏览 1
已结题

获取aria2下载结果的问题。



//获取任务信息
const getTasks = async () => {

  try {
    const activeResponse = await axios.post('http://localhost:6800/jsonrpc', {
      jsonrpc: '2.0',
      id: '1',
      method: 'aria2.tellActive',
      params: [],
    });

    const activeTasks = activeResponse.data.result.map((task) => ({
      gid: task.gid,
      name: task.files[0].path.split('/').pop().split('.').slice(0, -1).join('.'),
      completedLength: formatBytes(parseFloat(task.completedLength)),
      totalLength: formatBytes(parseFloat(task.totalLength)),
      downloadSpeed: formatBytes(parseFloat(task.downloadSpeed)) + '/s',
      downloadLink: task.files[0].uris.map((uri) => uri.uri),
      progress: isNaN(parseInt(Math.round((parseFloat(task.completedLength) / parseFloat(task.totalLength)) * 100))) ? 0 : parseInt(Math.round((parseFloat(task.completedLength) / parseFloat(task.totalLength)) * 100)),
      status: task.status,
    }));

    const allTasks = activeTasks.concat(waitingTasks);
    tasks.value = allTasks;
    allTasks.forEach(task => {
      DownloadCP(task)
    });
    return tasks;
  } catch (error) {
    console.error(error);
  }
};

setInterval(getTasks, 1000)
//任务下载完成后操做
const DownloadCP = async (task) => {
  try {
    const status = await axios.post('http://localhost:6800/jsonrpc', {
      jsonrpc: '2.0',
      id: '1',
      method: 'aria2.tellStatus',
      params: [task.gid, ['status']],
    });

    if (status.data.result.status == 'complete') {
      ElMessage({
        message: "下载完成",
        type: "success",
      });
    } else {
    }
  } catch (error) {
    console.error(error);
  }
};

请大家帮我看看这个代码怎么优化,因为我取任务状态使用了1秒的计时器。所以任务在下载完后,并不能百分百的获取任务完成得下载结果。怎么才能在下载完成后,第一时间返回下载结果给DownloadCP。

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 12月7日
    • 创建了问题 11月29日