refactor: 移除WebSocket,改用fetch一次性请求

This commit is contained in:
yinzhou.ma
2026-07-17 09:36:50 +08:00
parent b567e2466b
commit ee8fb21f1c
24 changed files with 22 additions and 5720 deletions
+16 -33
View File
@@ -5,7 +5,6 @@ const multer = require('multer');
const initSqlJs = require('sql.js');
const { v4: uuidv4 } = require('uuid');
const fs = require('fs');
const WebSocket = require('ws');
const ADMIN_PORT = 16888;
const FRONT_PORT = 80;
@@ -78,14 +77,6 @@ function getEnabledGroups() {
}
// ========== 轮播逻辑 ==========
let wsClients = new Set();
function broadcast(data) {
const msg = JSON.stringify(data);
wsClients.forEach(ws => {
if (ws.readyState === WebSocket.OPEN) ws.send(msg);
});
}
function getCurrentState() {
const featureEnabled = getConfig('feature_enabled') === 'true';
@@ -121,9 +112,7 @@ function getCurrentState() {
};
}
function broadcastState() {
broadcast(getCurrentState());
}
// broadcastState 已移除,前端通过 /api/state 主动拉取
function startCarouselTimer() {
setInterval(() => {
@@ -142,7 +131,7 @@ function startCarouselTimer() {
// 首次启动,记录开始时间并广播
if (startTime === 0) {
setConfig('current_group_start_time', Math.floor(Date.now() / 1000));
broadcastState();
return;
}
@@ -154,7 +143,7 @@ function startCarouselTimer() {
const nextIdx = findNextGroupIndex(groups, idx);
setConfig('current_group_index', nextIdx);
setConfig('current_group_start_time', Math.floor(Date.now() / 1000));
broadcastState();
}
}, 1000);
}
@@ -170,7 +159,7 @@ function findNextGroupIndex(groups, currentIdx) {
function resetCarousel() {
setConfig('current_group_index', 0);
setConfig('current_group_start_time', Math.floor(Date.now() / 1000));
broadcastState();
}
// ========== Multer ==========
@@ -185,22 +174,16 @@ const frontApp = express();
frontApp.use(express.static(path.join(__dirname, 'public')));
frontApp.use('/uploads', express.static(path.join(__dirname, 'uploads')));
frontApp.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
// 首页API,返回当前状态
frontApp.get('/api/state', (req, res) => {
res.json(getCurrentState());
});
const frontServer = frontApp.listen(FRONT_PORT, '0.0.0.0', () => {
console.log(`前台运行在 http://0.0.0.0:${FRONT_PORT}`);
});
const wss = new WebSocket.Server({ server: frontServer });
wss.on('connection', (ws) => {
wsClients.add(ws);
ws.send(JSON.stringify(getCurrentState()));
ws.on('close', () => wsClients.delete(ws));
});
// ========== 后台服务 (端口 18888) ==========
// ========== 后台服务 (端口 16888) ==========
const adminApp = express();
adminApp.use(express.json());
adminApp.use(express.urlencoded({ extended: true }));
@@ -255,7 +238,7 @@ adminApp.post('/api/groups', authMiddleware, (req, res) => {
runExec('INSERT INTO groups (name, display_seconds) VALUES (?, ?)', [name || '新分组', display_seconds || 30]);
saveDB();
const id = db.exec("SELECT last_insert_rowid()")[0].values[0][0];
broadcastState();
res.json({ id });
});
@@ -264,7 +247,7 @@ adminApp.put('/api/groups/:id', authMiddleware, (req, res) => {
runExec('UPDATE groups SET name=?, display_seconds=?, enabled=? WHERE id=?',
[name, display_seconds, enabled, parseInt(req.params.id)]);
saveDB();
broadcastState();
res.json({ success: true });
});
@@ -272,7 +255,7 @@ adminApp.put('/api/groups/:id/settime', authMiddleware, (req, res) => {
const { display_seconds } = req.body;
runExec('UPDATE groups SET display_seconds=? WHERE id=?', [display_seconds, parseInt(req.params.id)]);
saveDB();
broadcastState();
res.json({ success: true });
});
@@ -308,18 +291,18 @@ adminApp.post('/api/upload', authMiddleware, upload.single('image'), (req, res)
[group_id, req.file.filename, req.file.originalname, req.file.size || 0]);
saveDB();
const id = db.exec("SELECT last_insert_rowid()")[0].values[0][0];
broadcastState();
res.json({ id, filename: req.file.filename });
});
adminApp.put('/api/images/:id/enable', authMiddleware, (req, res) => {
runExec('UPDATE images SET enabled=1 WHERE id=?', [parseInt(req.params.id)]);
saveDB(); broadcastState(); res.json({ success: true });
saveDB(); res.json({ success: true });
});
adminApp.put('/api/images/:id/disable', authMiddleware, (req, res) => {
runExec('UPDATE images SET enabled=0 WHERE id=?', [parseInt(req.params.id)]);
saveDB(); broadcastState(); res.json({ success: true });
saveDB(); res.json({ success: true });
});
adminApp.delete('/api/images/:id', authMiddleware, (req, res) => {
@@ -330,7 +313,7 @@ adminApp.delete('/api/images/:id', authMiddleware, (req, res) => {
if (fs.existsSync(fp)) fs.unlinkSync(fp);
}
runExec('DELETE FROM images WHERE id=?', [id]);
saveDB(); broadcastState(); res.json({ success: true });
saveDB(); res.json({ success: true });
});
// 配置
@@ -343,7 +326,7 @@ adminApp.get('/api/config', (req, res) => {
adminApp.put('/api/config', authMiddleware, (req, res) => {
setConfig(req.body.key, req.body.value);
broadcastState();
res.json({ success: true });
});