Skip to content

Commit d3a33c1

Browse files
committed
added webgamedb json file
1 parent 7d5a764 commit d3a33c1

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

functions/api/ccu.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export async function onRequest(context) {
2+
const { request, env } = context;
3+
4+
const headers = {
5+
"Content-Type": "application/json",
6+
"Access-Control-Allow-Origin": "*",
7+
};
8+
9+
if (request.method === "POST") {
10+
const url = new URL(request.url);
11+
const sid = url.searchParams.get("sid");
12+
if (!sid) return new Response("missing sid", { status: 400 });
13+
14+
// store session with 3 minute ttl (auto-expires if heartbeat stops)
15+
await env.CCU_KV.put(`session:${sid}`, "1", { expirationTtl: 180 });
16+
return new Response(JSON.stringify({ ok: true }), { headers });
17+
}
18+
19+
// Poll: GET /api/ccu
20+
if (request.method === "GET") {
21+
const list = await env.CCU_KV.list({ prefix: "session:" });
22+
return new Response(JSON.stringify({ ccu: list.keys.length }), { headers });
23+
}
24+
25+
return new Response("Method not allowed", { status: 405 });
26+
} // for da webgamedb!!!!!

index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@
156156
};
157157
</script>
158158

159+
<script>
160+
161+
// ccu heartbeat
162+
const _sid = Math.random().toString(36).slice(2);
163+
const _hb = () => fetch("/api/ccu?sid=" + _sid, { method: "POST" });
164+
_hb(); // ping immediately on load
165+
setInterval(_hb, 60_000); // then every 60 sec and stuff
166+
167+
</script>
159168
<img src="https://librecounter.org/counter.svg" referrerPolicy="unsafe-url" width="0" />
160169

161170
<script>

0 commit comments

Comments
 (0)