diff --git a/deno.json b/deno.json index 2cca2e2d..200ac3fd 100644 --- a/deno.json +++ b/deno.json @@ -52,6 +52,6 @@ "route_middleware": "deno run -A examples/route_middleware.ts", "server_rendering": "deno run -A examples/server_rendering.tsx", "store": "deno run -A --env examples/store.ts", - "kv": "deno run --env -A --unstable-kv utils/kv.ts" + "cmd": "deno run --env -A --unstable-kv utils/cmd.ts" } } diff --git a/modules/hook/socket.ts b/modules/hook/socket.ts index eebe7a99..d34d9bd1 100644 --- a/modules/hook/socket.ts +++ b/modules/hook/socket.ts @@ -12,7 +12,7 @@ const useWebSocket = (url: string, room: string, user: string) => { function ping(data: any) { const i = setInterval(() => { if ( - countRef.current > 3 + countRef.current > 0 ) { setIsConnected(true); return clearInterval(i); @@ -26,10 +26,11 @@ const useWebSocket = (url: string, room: string, user: string) => { }), ); countRef.current++; + return console.log(`WebSocket connection established: ${room}`); } - }, 100); - console.log(`WebSocket connection established: ${room}`); + console.log(`WebSocket connection closed: ${room}`); + }, 100); } const connectWebSocket = () => { @@ -56,7 +57,13 @@ const useWebSocket = (url: string, room: string, user: string) => { console.log( "WebSocket connection closed. Attempting to reconnect...", ); - reconnect(); + const i = setInterval(() => { + if (isConnected) { + return clearInterval(i); + } + countRef.current++; + reconnect(countRef.current); + }, 1000); }; socketRef.current.onerror = (error) => { @@ -81,6 +88,9 @@ const useWebSocket = (url: string, room: string, user: string) => { socketRef.current?.close(); if (reconnectTimeoutRef.current) { clearTimeout(reconnectTimeoutRef.current); + setIsConnected(false); + countRef.current = 0; + setMessage(""); } }; }, [url]); diff --git a/modules/socket/mod.ts b/modules/socket/mod.ts index 1ae39b0f..aabbb279 100644 --- a/modules/socket/mod.ts +++ b/modules/socket/mod.ts @@ -56,10 +56,9 @@ export default function socketModule(s: Fastro) { socket: WebSocket, data: Data, ) { - // console.log("joinRoom:", JSON.stringify(data)); const connected = ctx.stores.get("connected"); if (data.user) { - connected?.set(data.user, { data, socket }); + connected?.set(data.user, { data, socket }, DAY); } } diff --git a/utils/cmd.ts b/utils/cmd.ts new file mode 100644 index 00000000..51a94ac2 --- /dev/null +++ b/utils/cmd.ts @@ -0,0 +1,60 @@ +// deno-lint-ignore-file no-explicit-any +import { kv, reset } from "@app/utils/db.ts"; +import { parseArgs } from "jsr:@std/cli/parse-args"; + +const flags = parseArgs(Deno.args, { + boolean: ["deploy"], +}); + +let k: Deno.Kv; +if (flags.deploy) { + k = await Deno.openKv( + `https://api.deno.com/databases/${Deno.env.get("DATABASE_ID")}/connect`, + ); +} else { + k = kv; +} + +const cmd = Deno.args[0]; +if (cmd === "reset") { + await reset(); +} + +if (cmd === "list") { + if (flags.deploy) { + const args = Deno.args[2]; + const key = args ? [args] : []; + getAll(k, key); + } else { + const args = Deno.args[1]; + const key = args ? [args] : []; + getAll(k, key); + } +} + +if (cmd === "delete") { + if (flags.deploy) { + const args = Deno.args[2]; + const key = args ? [args] : []; + del(k, key); + } else { + const args = Deno.args[1]; + const key = args ? [args] : []; + del(k, key); + } +} + +async function getAll(kv: Deno.Kv, key: any) { + console.log("key", key); + const entries = kv.list({ prefix: key }); + for await (const entry of entries) { + console.log(`[${entry.key}]:`, entry.value); + } +} + +async function del(k: Deno.Kv, key: any) { + const iter = k.list({ prefix: key }); + const promises = []; + for await (const res of iter) promises.push(kv.delete(res.key)); + await Promise.all(promises); +} diff --git a/utils/kv.ts b/utils/kv.ts deleted file mode 100644 index e419e9e4..00000000 --- a/utils/kv.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { kv, reset } from "@app/utils/db.ts"; - -const args = Deno.args[0]; -if (args === "reset") { - await reset(); -} - -if (args === "deploy") { - const kd = await Deno.openKv( - `https://api.deno.com/databases/${Deno.env.get("DATABASE_ID")}/connect`, - ); - - const iter = kd.list({ prefix: [] }); - await Array.fromAsync(iter, ({ key, value }) => { - console.log(`[${key}]:`, value); - return value; - }); -} else { - getAll(kv); -} - -async function getAll(kv: Deno.Kv) { - const key = args ? [args] : []; - const entries = kv.list({ prefix: key }); - - for await (const entry of entries) { - console.log(`[${entry.key}]:`, entry.value); - } -}