webdojo: attempt reconnecting only after success

Instead of always endlessly trying even if the endpoint was never
acknowledged.
This commit is contained in:
Fang 2020-10-06 18:28:50 +02:00
parent b12f89228b
commit f9e6c9f913
No known key found for this signature in database
GPG Key ID: EB035760C1BBA972

View File

@ -19,9 +19,11 @@ export default class Subscription {
setupSlog() { setupSlog() {
const slog = new EventSource('/~/slog', { withCredentials: true }); const slog = new EventSource('/~/slog', { withCredentials: true });
let available = false;
slog.onopen = e => { slog.onopen = e => {
console.log('slog: opened stream'); console.log('slog: opened stream');
available = true;
} }
slog.onmessage = e => { slog.onmessage = e => {
@ -30,6 +32,7 @@ export default class Subscription {
slog.onerror = e => { slog.onerror = e => {
console.error('slog: eventsource error:', e); console.error('slog: eventsource error:', e);
if (available) {
window.setTimeout(() => { window.setTimeout(() => {
if (slog.readyState !== EventSource.CLOSED) return; if (slog.readyState !== EventSource.CLOSED) return;
console.log('slog: reconnecting...'); console.log('slog: reconnecting...');
@ -37,6 +40,7 @@ export default class Subscription {
}, 10000); }, 10000);
} }
} }
}
delete() { delete() {
this.channel.delete(); this.channel.delete();