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