update view counter with websockets
This commit is contained in:
@@ -7,8 +7,7 @@ function Toolbar(viewer) {
|
|||||||
|
|
||||||
this.views = 0
|
this.views = 0
|
||||||
this.downloads = 0
|
this.downloads = 0
|
||||||
this.statsInterval = 2000
|
this.statsWebsocket = null
|
||||||
this.statsTimeout = null
|
|
||||||
|
|
||||||
this.divToolbar = document.getElementById("toolbar")
|
this.divToolbar = document.getElementById("toolbar")
|
||||||
this.divFilePreview = document.getElementById("filepreview")
|
this.divFilePreview = document.getElementById("filepreview")
|
||||||
@@ -37,41 +36,27 @@ Toolbar.prototype.setFile = function(file) {
|
|||||||
this.setStats()
|
this.setStats()
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function periodically updates the stats using an exponential backoff
|
|
||||||
// timer. It starts with one query per second, and it increases by one second
|
|
||||||
// every run. When the stats change the timeout decreases by one second
|
|
||||||
Toolbar.prototype.setStats = function() {
|
Toolbar.prototype.setStats = function() {
|
||||||
clearTimeout(this.statsTimeout)
|
|
||||||
|
|
||||||
let size = this.currentFile.size
|
let size = this.currentFile.size
|
||||||
|
|
||||||
fetch(this.currentFile.stats_href).then(resp => {
|
this.spanViews.innerText = "loading..."
|
||||||
return resp.json()
|
this.spanDownloads.innerText = "loading..."
|
||||||
}).then(resp => {
|
|
||||||
let downloads = Math.round(resp.bandwidth/size)
|
|
||||||
|
|
||||||
// If the new values are different we decrease the timeout
|
if (this.statsWebsocket !== null) {
|
||||||
if (resp.views != this.views || downloads != this.downloads) {
|
this.statsWebsocket.close()
|
||||||
this.statsInterval = this.statsInterval - 2000
|
}
|
||||||
if (this.statsInterval < 2000) { this.statsInterval = 2000 }
|
|
||||||
} else {
|
|
||||||
this.statsInterval = this.statsInterval + 2000
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save the new values
|
|
||||||
this.views = resp.views
|
|
||||||
this.downloads = downloads
|
|
||||||
|
|
||||||
|
this.statsWebsocket = new WebSocket(
|
||||||
|
location.origin.replace(/^http/, 'ws')+"/api/file/"+this.currentFile.id+"/stats"
|
||||||
|
)
|
||||||
|
this.statsWebsocket.onmessage = (msg) => {
|
||||||
|
let j = JSON.parse(msg.data)
|
||||||
|
this.views = j.views
|
||||||
|
this.downloads = Math.round(j.bandwidth/size)
|
||||||
this.spanViews.innerText = formatThousands(this.views)
|
this.spanViews.innerText = formatThousands(this.views)
|
||||||
this.spanDownloads.innerText = formatThousands(downloads)
|
this.spanDownloads.innerText = formatThousands(this.downloads)
|
||||||
|
console.log("WS update", j)
|
||||||
console.debug("updating stats in ", this.statsInterval)
|
}
|
||||||
|
|
||||||
this.statsTimeout = setTimeout(() => { this.setStats() }, this.statsInterval)
|
|
||||||
}).catch(err => {
|
|
||||||
console.error("Failed to update stats:", err)
|
|
||||||
this.statsTimeout = setTimeout(() => { this.setStats() }, this.statsInterval)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Toolbar.prototype.toggle = function() {
|
Toolbar.prototype.toggle = function() {
|
||||||
|
@@ -139,11 +139,6 @@ Viewer.prototype.setFile = function(file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.viewerScript.render(this.divFilepreview)
|
this.viewerScript.render(this.divFilepreview)
|
||||||
|
|
||||||
// let ws = new WebSocket("ws://127.0.0.1:8080/api/file/"+file.id+"/stats")
|
|
||||||
// ws.onmessage = (msg) => {
|
|
||||||
// console.log(msg)
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Viewer.prototype.renderSponsors = function() {
|
Viewer.prototype.renderSponsors = function() {
|
||||||
@@ -248,9 +243,6 @@ function fileFromAPIResp(resp) {
|
|||||||
resp.description = ""
|
resp.description = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
console.debug("New file:")
|
|
||||||
console.debug(resp)
|
|
||||||
|
|
||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
function fileFromSkyNet(resp) {
|
function fileFromSkyNet(resp) {
|
||||||
|
Reference in New Issue
Block a user