From a95fdbbf102863d9f31d101ed6f904cafee53179 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Wed, 25 Nov 2020 23:34:09 +0000 Subject: [PATCH] Real heartbeat timeouts server -> client --- main.go | 3 +-- static/remote.js | 10 ++++++++++ static/remote.ts | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index a3cb1eb..bfd3e0e 100644 --- a/main.go +++ b/main.go @@ -623,8 +623,7 @@ func writeInitial(client *client, w http.ResponseWriter, flusher http.Flusher) { } func writeHeartbeat(w http.ResponseWriter, flusher http.Flusher) { - fmt.Fprintf(w, ":\n\n") - flusher.Flush() + writeEvent(&event{}, w, flusher) } func writeEvent(e *event, w http.ResponseWriter, flusher http.Flusher) { diff --git a/static/remote.js b/static/remote.js index 64a47c5..7ef87da 100644 --- a/static/remote.js +++ b/static/remote.js @@ -74,7 +74,15 @@ function watch(roomId, clientId, adminSecret, prnt) { } function createEventSource(url) { const es = new EventSource(url.toString()); + let lastMessage = performance.now(); + const intId = setInterval(() => { + if (performance.now() - lastMessage > 10000) { + console.warn("timeout"); + es.dispatchEvent(new Event("error")); + } + }, 1000); es.addEventListener("open", () => { + console.info("connected"); messageBus.dispatchEvent(new Event("open")); }); es.addEventListener("message", (e) => { @@ -82,10 +90,12 @@ function createEventSource(url) { data: e.data, lastEventId: e.lastEventId, })); + lastMessage = performance.now(); }); es.addEventListener("error", () => { console.warn("disconnected"); es.close(); + clearInterval(intId); setTimeout(() => createEventSource(url), 3000); messageBus.dispatchEvent(new Event("error")); }); diff --git a/static/remote.ts b/static/remote.ts index 9a70763..2800ec8 100644 --- a/static/remote.ts +++ b/static/remote.ts @@ -159,7 +159,17 @@ function watch(roomId: string, clientId: string, adminSecret: string | null, prn function createEventSource(url: URL) { const es = new EventSource(url.toString()); + let lastMessage = performance.now(); + + const intId = setInterval(() => { + if (performance.now() - lastMessage > 10000) { + console.warn("timeout"); + es.dispatchEvent(new Event("error")); + } + }, 1000); + es.addEventListener("open", () => { + console.info("connected"); messageBus.dispatchEvent(new Event("open")); }); @@ -168,11 +178,16 @@ function createEventSource(url: URL) { data: e.data, lastEventId: e.lastEventId, })); + + lastMessage = performance.now(); }); es.addEventListener("error", () => { console.warn("disconnected"); + es.close(); + clearInterval(intId); + setTimeout(() => createEventSource(url), 3000); messageBus.dispatchEvent(new Event("error"));