config
This commit is contained in:
22
src/routes-simple/event.ts
Normal file
22
src/routes-simple/event.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { router, error, checkAuth, clients, getTaskId } from './router.ts';
|
||||
|
||||
router.get('/api/events', async (req, res) => {
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
Connection: 'keep-alive',
|
||||
});
|
||||
const tokenUser = await checkAuth(req, res);
|
||||
if (!tokenUser) return;
|
||||
const taskId = getTaskId(req);
|
||||
if (!taskId) {
|
||||
res.end(error('task-id is required'));
|
||||
return;
|
||||
}
|
||||
// 将客户端连接推送到 clients 数组
|
||||
clients.set(taskId, { client: res, tokenUser });
|
||||
// 移除客户端连接
|
||||
req.on('close', () => {
|
||||
clients.delete(taskId);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user