heartbeat
This commit is contained in:
parent
cfba207077
commit
55316027c0
|
|
@ -8,7 +8,11 @@ const router = Hype.router()
|
|||
// individual events so apps can react to specific lifecycle changes.
|
||||
router.sse('/stream', (send) => {
|
||||
const unsub = onEvent(event => send(event))
|
||||
return unsub
|
||||
const heartbeat = setInterval(() => send('', 'ping'), 60_000)
|
||||
return () => {
|
||||
clearInterval(heartbeat)
|
||||
unsub()
|
||||
}
|
||||
})
|
||||
|
||||
export default router
|
||||
|
|
|
|||
|
|
@ -35,8 +35,10 @@ function ensureConnection() {
|
|||
buf = lines.pop()!
|
||||
for (const line of lines) {
|
||||
if (!line.startsWith('data: ')) continue
|
||||
const payload = line.slice(6)
|
||||
if (!payload) continue
|
||||
try {
|
||||
const event: ToesEvent = JSON.parse(line.slice(6))
|
||||
const event: ToesEvent = JSON.parse(payload)
|
||||
_listeners.forEach(l => {
|
||||
if (l.types.includes(event.type)) l.callback(event)
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user