From 06542590e11561609f6c5d6c505231242b1aa110 Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Tue, 29 Sep 2020 18:44:32 +0200 Subject: [PATCH] Make viewers counter period configurable --- docs/ghostream.example.yml | 1 + main.go | 1 + web/static/js/viewersCounter.js | 9 +++++---- web/template/viewer.html | 5 +++++ web/web.go | 11 ++++++----- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/docs/ghostream.example.yml b/docs/ghostream.example.yml index 2290792..1df72ae 100644 --- a/docs/ghostream.example.yml +++ b/docs/ghostream.example.yml @@ -35,6 +35,7 @@ web: hostname: localhost favicon: https://www.crans.org/images/favicon.ico widgetURL: https://example.com/ + viewersCounterRefreshPeriod: 20000 # Configure WebRTC server webrtc: diff --git a/main.go b/main.go index 9236a9d..5f8f1d3 100644 --- a/main.go +++ b/main.go @@ -53,6 +53,7 @@ func loadConfiguration() { viper.SetDefault("Web.Name", "Ghostream") viper.SetDefault("Web.Hostname", "localhost") viper.SetDefault("Web.Favicon", "/favicon.ico") + viper.SetDefault("Web.ViewersCounterRefreshPeriod", 20000) viper.SetDefault("WebRTC.MinPortUDP", 10000) viper.SetDefault("WebRTC.MaxPortUDP", 10005) viper.SetDefault("WebRTC.STUNServers", []string{"stun:stun.l.google.com:19302"}) diff --git a/web/static/js/viewersCounter.js b/web/static/js/viewersCounter.js index a8311a3..98bbeaf 100644 --- a/web/static/js/viewersCounter.js +++ b/web/static/js/viewersCounter.js @@ -1,4 +1,5 @@ -function refreshViewersCounter() { +// Refresh viewer count by pulling metric from server +function refreshViewersCounter(period) { let xhr = new XMLHttpRequest() xhr.open("GET", "/_stats/", true) xhr.onload = function () { @@ -12,7 +13,7 @@ function refreshViewersCounter() { } xhr.send() - setTimeout(refreshViewersCounter, 20000) + setTimeout(() => { + refreshViewersCounter(period) + }, period) } - -refreshViewersCounter() diff --git a/web/template/viewer.html b/web/template/viewer.html index a18238d..dc3d938 100644 --- a/web/template/viewer.html +++ b/web/template/viewer.html @@ -50,5 +50,10 @@ {{end}} ] startPeerConnection() + + // Wait a bit before pulling viewers counter for the first time + setTimeout(() => { + refreshViewersCounter({{.Cfg.ViewersCounterRefreshPeriod}}) + }, 1000) {{end}} \ No newline at end of file diff --git a/web/web.go b/web/web.go index eca6e54..958660b 100644 --- a/web/web.go +++ b/web/web.go @@ -15,11 +15,12 @@ import ( // Options holds web package configuration type Options struct { - ListenAddress string - Name string - Hostname string - Favicon string - WidgetURL string + ListenAddress string + Name string + Hostname string + Favicon string + WidgetURL string + ViewersCounterRefreshPeriod int // Copied from WebRTC configuration STUNServers []string