ptSend/router/index.js

64 lines
1.5 KiB
JavaScript

const Router = require("koa-router")
const { getQqListData } = require("../util/getList/qq")
const KoaSSEStream = require('koa-sse-stream');
const { dowload } = require("../util/download");
const router = new Router()
const dowloadlist = []
router.get("/getQqList",async (ctx)=>{
let url = ctx.query.url
let list = await getQqListData(url)
ctx.body = list
})
router.get("/startDown",async (ctx)=>{
let title = ctx.query.title
let url = ctx.query.url
let save = ctx.query.save
dowloadlist.push({
title,url,save
})
// startDown()
ctx.body={
code:0,
msg:"添加成功"
}
})
// 连接池
const clientList = [];
// koa-sse-stream 配置
const SSE_CONF = {
maxClients: 5, // 最大连接数
pingInterval: 40000 // 重连时间
}
router.get('/msg', KoaSSEStream(SSE_CONF), ctx => {
// 每次连接会进行一个 push
clientList.push(ctx.sse);
if(clientList.length > 5){
// 删除
clientList.shift()
}
})
let start = false
function startDown(){
start = true
dowload(dowloadlist[0],(data,isok)=>{
let name = dowloadlist[0].title
clientList.forEach((i)=>{
i.send(isok ? `${name}下载完成` : data )
})
if(isok){
dowloadlist.shift()
start = false
}
})
}
setInterval(()=>{
if(dowloadlist.length != 0 && !start){
startDown()
}else if(dowloadlist.length == 0){
start = false
}
},1000)
module.exports = router