fix: code update
This commit is contained in:
@@ -55,19 +55,42 @@ wss.on('connection', (ws) => {
|
||||
ws.send(JSON.stringify({ type: 'situation', data: getSituation(), stats: getStats() }))
|
||||
})
|
||||
|
||||
function broadcastSituation() {
|
||||
// 仅用 situation.updated_at + situation_update 条数做“版本”,避免无变更时重复查库和推送
|
||||
function getBroadcastVersion() {
|
||||
try {
|
||||
const meta = db.prepare('SELECT updated_at FROM situation WHERE id = 1').get()
|
||||
const row = db.prepare('SELECT COUNT(*) as c FROM situation_update').get()
|
||||
return `${meta?.updated_at || ''}_${row?.c ?? 0}`
|
||||
} catch (_) {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
let lastBroadcastVersion = null
|
||||
|
||||
function broadcastSituation(force = false) {
|
||||
if (!force && wss.clients.size === 0) return
|
||||
const version = getBroadcastVersion()
|
||||
if (!force && version === lastBroadcastVersion) return
|
||||
try {
|
||||
const data = JSON.stringify({ type: 'situation', data: getSituation(), stats: getStats() })
|
||||
wss.clients.forEach((c) => {
|
||||
if (c.readyState === 1) c.send(data)
|
||||
})
|
||||
lastBroadcastVersion = version
|
||||
} catch (_) {}
|
||||
}
|
||||
app.set('broadcastSituation', broadcastSituation)
|
||||
|
||||
app.set('broadcastSituation', () => broadcastSituation(true))
|
||||
|
||||
if (typeof routes.setBroadcastSituation === 'function') {
|
||||
routes.setBroadcastSituation(broadcastSituation)
|
||||
routes.setBroadcastSituation(() => broadcastSituation(true))
|
||||
}
|
||||
|
||||
const BROADCAST_INTERVAL_MS = Math.max(0, parseInt(process.env.BROADCAST_INTERVAL_MS, 10) || 30000)
|
||||
if (BROADCAST_INTERVAL_MS > 0) {
|
||||
setInterval(() => broadcastSituation(false), BROADCAST_INTERVAL_MS)
|
||||
}
|
||||
setInterval(broadcastSituation, 3000)
|
||||
|
||||
// 供爬虫调用:先从磁盘重载 DB(纳入爬虫写入),再更新 updated_at 并立即广播
|
||||
function notifyCrawlerUpdate() {
|
||||
|
||||
Reference in New Issue
Block a user