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