Error handling, host argument support, cleanup

This commit is contained in:
Mikko Ahlroth 2020-04-04 12:14:31 +03:00
parent 430b416d30
commit 99a69380db
3 changed files with 26 additions and 5 deletions

View file

@ -1,2 +1,5 @@
/** How many seconds a page view is considered "current". */ /** How many seconds a page view is considered "current". */
export const CURRENT_THRESHOLD = 300; export const CURRENT_THRESHOLD = 300 as const;
/** Maximum reconnect delay in seconds. */
export const MAX_RECONNECT_DELAY = 30 as const;

View file

@ -1,4 +1,5 @@
import { connectListener } from "./archive-client.js"; import { connectListener } from "./archive-client.js";
import { MAX_RECONNECT_DELAY } from "./config.js";
import { CurrentSessions } from "./current-sessions.js"; import { CurrentSessions } from "./current-sessions.js";
import { LiveCounter } from "./live-counter.js"; import { LiveCounter } from "./live-counter.js";
import { IRemotePageView, parseView } from "./page-view.js"; import { IRemotePageView, parseView } from "./page-view.js";
@ -10,13 +11,20 @@ let currentyLiveCounter: LiveCounter;
let currentSessions: CurrentSessions; let currentSessions: CurrentSessions;
let topPaths: TopPaths; let topPaths: TopPaths;
let eventSource: EventSource; let eventSource: EventSource;
let host: string;
function openCallback(stream: EventSource, e: Event) { function openCallback(stream: EventSource, e: Event) {
console.log("Opened event stream."); console.log("Opened event stream.");
} }
function errorCallback(stream: EventSource, e: ErrorEvent) { function errorCallback(stream: EventSource, e: ErrorEvent) {
console.log(e); console.error(e);
stream.close();
console.log("Reconnecting...");
const delaySecs = Math.random() * MAX_RECONNECT_DELAY;
setTimeout(initConnection, delaySecs * 1000);
} }
function msgCallback(stream: EventSource, e: MessageEvent) { function msgCallback(stream: EventSource, e: MessageEvent) {
@ -31,13 +39,25 @@ function msgCallback(stream: EventSource, e: MessageEvent) {
topPaths.handleView(parsedData); topPaths.handleView(parsedData);
} }
function initConnection() {
eventSource = connectListener(host, openCallback, msgCallback, errorCallback);
}
function main() { function main() {
currentyLiveCounter = new LiveCounter(document.getElementById("currently-live-counter")!); currentyLiveCounter = new LiveCounter(document.getElementById("currently-live-counter")!);
worldMap = new WorldMap(document.getElementById("world-map")!); worldMap = new WorldMap(document.getElementById("world-map")!);
currentSessions = new CurrentSessions(document.getElementById("current-sessions-data")!); currentSessions = new CurrentSessions(document.getElementById("current-sessions-data")!);
topPaths = new TopPaths(document.getElementById("top-paths-data")!); topPaths = new TopPaths(document.getElementById("top-paths-data")!);
eventSource = connectListener("localhost:1971", openCallback, msgCallback, errorCallback); const hostParam = (new URL(window.location.href)).searchParams.get("host");
if (hostParam === null) {
alert("Missing host! Give host as ?host= query argument.");
throw new Error("Missing host.");
}
host = hostParam;
initConnection();
} }
if (document.readyState === "interactive") { if (document.readyState === "interactive") {

View file

@ -2,8 +2,6 @@ import { Component } from "./component.js";
import { CURRENT_THRESHOLD } from "./config.js"; import { CURRENT_THRESHOLD } from "./config.js";
import { IPageView } from "./page-view.js"; import { IPageView } from "./page-view.js";
const UPDATE_TIMER = 10;
/** /**
* LiveCounter shows the amount of users on the site in the last 5 minutes. * LiveCounter shows the amount of users on the site in the last 5 minutes.
*/ */