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

31 lines
807 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;
2020-11-09 16:57:55 +00:00
this.uid = Math.floor(1e19 * Math.random()).toString(16);
2020-10-20 19:29:41 +00:00
}
/**
* 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-11-09 16:57:55 +00:00
fetch(this.url + "?uid=" + this.uid)
2020-10-20 19:29:41 +00:00
.then(response => response.json())
.then((data) => this.element.innerText = data.ConnectedViewers)
.catch(console.log);
}
}