Use new websocket proto for view counting
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
<script>
|
<script>
|
||||||
|
import { onMount } from "svelte";
|
||||||
import { formatDataVolume, formatThousands } from "../util/Formatting.svelte"
|
import { formatDataVolume, formatThousands } from "../util/Formatting.svelte"
|
||||||
|
|
||||||
export let file = {
|
export let file = {
|
||||||
@@ -9,6 +10,7 @@ export let file = {
|
|||||||
bandwidth_used: 0,
|
bandwidth_used: 0,
|
||||||
bandwidth_used_paid: 0,
|
bandwidth_used_paid: 0,
|
||||||
}
|
}
|
||||||
|
export let view_token = ""
|
||||||
|
|
||||||
let views = 0
|
let views = 0
|
||||||
let downloads = 0
|
let downloads = 0
|
||||||
@@ -30,18 +32,28 @@ let update_stats = async id => {
|
|||||||
}
|
}
|
||||||
size = file.size
|
size = file.size
|
||||||
|
|
||||||
// If the socket is already active we need to close it
|
send_watch_command()
|
||||||
if (socket !== null) {
|
}
|
||||||
// Disable the error handler so it doesn't start retrying the connection
|
|
||||||
socket.onerror = null
|
const send_watch_command = () => {
|
||||||
socket.close()
|
if (socket.readyState === WebSocket.OPEN) {
|
||||||
socket = null
|
socket.send(
|
||||||
|
JSON.stringify(
|
||||||
|
{cmd: "watch_file", a1: file.id, a2: view_token}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const init_socket = () => {
|
||||||
|
if (socket !== null || socket.readyState !== WebSocket.CLOSED) {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("opening socket for", id)
|
console.log("initializing socket")
|
||||||
socket = new WebSocket(
|
socket = new WebSocket(location.origin.replace(/^http/, 'ws') + "/api/file_stats")
|
||||||
location.origin.replace(/^http/, 'ws') + "/api/file/" + id + "/stats"
|
|
||||||
)
|
socket.onopen = () => send_watch_command()
|
||||||
socket.onmessage = msg => {
|
socket.onmessage = msg => {
|
||||||
let j = JSON.parse(msg.data)
|
let j = JSON.parse(msg.data)
|
||||||
console.debug("WS update", j)
|
console.debug("WS update", j)
|
||||||
@@ -63,14 +75,20 @@ let update_stats = async id => {
|
|||||||
socket = null
|
socket = null
|
||||||
error_msg = "failed to get stats, retrying..."
|
error_msg = "failed to get stats, retrying..."
|
||||||
|
|
||||||
window.setTimeout(() => {
|
window.setTimeout(init_socket, 2000)
|
||||||
if (socket === null) {
|
|
||||||
update_stats(file.id)
|
|
||||||
}
|
|
||||||
}, 3000)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
init_socket()
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (socket !== null) {
|
||||||
|
socket.close()
|
||||||
|
socket = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
|
@@ -207,13 +207,6 @@ const open_file_index = async index => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
apply_customizations(file)
|
apply_customizations(file)
|
||||||
|
|
||||||
// Register a file view
|
|
||||||
fetch(window.api_endpoint + "/file/" + file.id + "/view", {
|
|
||||||
method: "POST",
|
|
||||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
||||||
body: "token=" + view_token
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
const toggle_gallery = () => {
|
const toggle_gallery = () => {
|
||||||
if (view === "gallery") {
|
if (view === "gallery") {
|
||||||
@@ -425,7 +418,7 @@ const keyboard_event = evt => {
|
|||||||
<div class="file_preview_row">
|
<div class="file_preview_row">
|
||||||
<div class="toolbar" class:toolbar_visible>
|
<div class="toolbar" class:toolbar_visible>
|
||||||
{#if view === "file"}
|
{#if view === "file"}
|
||||||
<FileStats file={file}/>
|
<FileStats file={file} view_token={view_token}/>
|
||||||
{:else if view === "gallery"}
|
{:else if view === "gallery"}
|
||||||
<ListStats list={list}/>
|
<ListStats list={list}/>
|
||||||
{/if}
|
{/if}
|
||||||
|
Reference in New Issue
Block a user