Update micUploadRoutes.js via upload script - 2026-09-21 01:11:04
This commit is contained in:
parent
e6e859ea46
commit
4e3b2131e0
|
|
@ -153,9 +153,22 @@ function registerMicUpload(app, server, ctx) {
|
||||||
});
|
});
|
||||||
|
|
||||||
// ---- 会话生命周期 ----
|
// ---- 会话生命周期 ----
|
||||||
|
// 改成单例:全局只有一个会话,固定id='default'。前端每次挂载都调这个接口,
|
||||||
|
// 如果浏览器已经在跑,直接复用(不重启、不影响登录态),只有没有会话时才
|
||||||
|
// 真正启动一个新的。这样避免"刷新页面=开一个新浏览器"导致旧进程占着
|
||||||
|
// profile目录的锁、登录态传不过去的问题——只要不主动点"关闭会话",
|
||||||
|
// 浏览器和登录态就一直在。
|
||||||
|
|
||||||
app.post('/api/mic-upload/sessions', requireAuth, async (req, res) => {
|
app.post('/api/mic-upload/sessions', requireAuth, async (req, res) => {
|
||||||
const id = crypto.randomBytes(8).toString('hex');
|
const id = 'default';
|
||||||
|
const existing = sessions.get(id);
|
||||||
|
if (existing && existing.session && !existing.session.closed) {
|
||||||
|
// 已经有一个活着的会话,直接复用——不new浏览器,登录态原样保留。
|
||||||
|
// token重新发一个(简单起见每次连接都发新的,旧token自然失效)。
|
||||||
|
existing.token = crypto.randomBytes(24).toString('hex');
|
||||||
|
return res.json({ sessionId: id, wsToken: existing.token, phase: existing.phase, reused: true });
|
||||||
|
}
|
||||||
|
|
||||||
const token = crypto.randomBytes(24).toString('hex');
|
const token = crypto.randomBytes(24).toString('hex');
|
||||||
const entry = { session: null, token, wsClients: new Set(), createdAt: Date.now(), phase: 'browsing' };
|
const entry = { session: null, token, wsClients: new Set(), createdAt: Date.now(), phase: 'browsing' };
|
||||||
sessions.set(id, entry);
|
sessions.set(id, entry);
|
||||||
|
|
@ -168,7 +181,7 @@ function registerMicUpload(app, server, ctx) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await session.start();
|
await session.start();
|
||||||
res.json({ sessionId: id, wsToken: token });
|
res.json({ sessionId: id, wsToken: token, phase: entry.phase, reused: false });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
sessions.delete(id);
|
sessions.delete(id);
|
||||||
res.status(500).json({ error: `浏览器启动失败: ${e.message}` });
|
res.status(500).json({ error: `浏览器启动失败: ${e.message}` });
|
||||||
|
|
@ -334,16 +347,13 @@ function registerMicUpload(app, server, ctx) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// 空会话超时清理(比如浏览器标签页被直接关掉,没走DELETE接口)
|
// 之前这里有个"30分钟无连接自动清理"的定时器,但既然目标是长期保持登录、
|
||||||
setInterval(() => {
|
// 单例复用,这个自动清理反而会在你没开着网页的时候把浏览器关掉、白白丢失
|
||||||
const now = Date.now();
|
// 登录态——所以去掉了,改成完全手动:只有点"关闭会话"才会真正关闭浏览器。
|
||||||
for (const [id, entry] of sessions.entries()) {
|
// 代价是如果你长期不用也不点关闭,这个无头Chromium进程会一直占着内存
|
||||||
if (entry.wsClients.size === 0 && now - entry.createdAt > 30 * 60 * 1000) {
|
// (几十MB量级,对单人使用的VPS影响不大);真要回收就SSH上去手动
|
||||||
entry.session.close();
|
// `systemctl restart customer-crm` 即可,重启后浏览器会重新launch,
|
||||||
sessions.delete(id);
|
// 登录态因为是持久化profile,还在磁盘上,大概率还是已登录状态。
|
||||||
}
|
|
||||||
}
|
|
||||||
}, 5 * 60 * 1000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { registerMicUpload };
|
module.exports = { registerMicUpload };
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue