ghostream/web/static/js/modules/viewerCounter.js

30 lines
720 B
JavaScript
Raw Normal View History

2020-10-20 19:29:41 +00:00
/**
* ViewerCounter show the number of active viewers
*/
export class ViewerCounter {
/**
* @param {HTMLElement} element
* @param {String} streamName
*/
constructor(element, streamName) {
this.element = element;
this.url = "/_stats/" + streamName;
}
/**
* Regulary update counter
*
* @param {Number} updatePeriod
*/
regularUpdate(updatePeriod) {
2020-10-21 20:38:36 +00:00
setInterval(() => this.refreshViewersCounter(), updatePeriod);
2020-10-20 19:29:41 +00:00
}
2020-10-21 20:38:36 +00:00
refreshViewersCounter() {
2020-10-20 19:29:41 +00:00
fetch(this.url)
.then(response => response.json())
.then((data) => this.element.innerText = data.ConnectedViewers)
.catch(console.log);
}
}