diff --git a/web/handler.go b/web/handler.go index 8a30ffb..b9c5727 100644 --- a/web/handler.go +++ b/web/handler.go @@ -4,22 +4,24 @@ package web import ( "bytes" "encoding/json" + "github.com/markbates/pkger" + "gitlab.crans.org/nounous/ghostream/internal/monitoring" + "gitlab.crans.org/nounous/ghostream/stream/ovenmediaengine" + "gitlab.crans.org/nounous/ghostream/stream/webrtc" "html/template" "log" "net" "net/http" "regexp" "strings" - - "github.com/markbates/pkger" - "gitlab.crans.org/nounous/ghostream/internal/monitoring" - "gitlab.crans.org/nounous/ghostream/stream/ovenmediaengine" - "gitlab.crans.org/nounous/ghostream/stream/webrtc" + "time" ) var ( // Precompile regex validPath = regexp.MustCompile("^/[a-z0-9@_-]*$") + + connectedClients = make(map[string]map[string]int64) ) // Handle site index and viewer pages @@ -90,14 +92,33 @@ func staticHandler() http.Handler { } func statisticsHandler(w http.ResponseWriter, r *http.Request) { + // Retrieve stream name from URL name := strings.SplitN(strings.Replace(r.URL.Path[7:], "/", "", -1), "@", 2)[0] userCount := 0 + // Clients have a unique generated identifier per session, that expires in 40 seconds. + // Each time the client connects to this page, the identifier is renewed. + // Yeah, that's not a good way to have stats, but it works... + if connectedClients[name] == nil { + connectedClients[name] = make(map[string]int64) + } + currentTime := time.Now().Unix() + if _, ok := r.URL.Query()["uid"]; ok { + uid := r.URL.Query()["uid"][0] + connectedClients[name][uid] = currentTime + } + for uid, oldTime := range connectedClients[name] { + if currentTime-oldTime > 40 { + delete(connectedClients, uid) + } + } + // Get requested stream stream, err := streams.Get(name) if err == nil { userCount = stream.ClientCount() userCount += webrtc.GetNumberConnectedSessions(name) + userCount += len(connectedClients[name]) } // Display connected users statistics diff --git a/web/static/js/modules/viewerCounter.js b/web/static/js/modules/viewerCounter.js index b95bf38..7a27b23 100644 --- a/web/static/js/modules/viewerCounter.js +++ b/web/static/js/modules/viewerCounter.js @@ -9,6 +9,7 @@ export class ViewerCounter { constructor(element, streamName) { this.element = element; this.url = "/_stats/" + streamName; + this.uid = Math.floor(1e19 * Math.random()).toString(16); } /** @@ -21,7 +22,7 @@ export class ViewerCounter { } refreshViewersCounter() { - fetch(this.url) + fetch(this.url + "?uid=" + this.uid) .then(response => response.json()) .then((data) => this.element.innerText = data.ConnectedViewers) .catch(console.log);