File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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!!!!!
Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments