Skip to content

Commit

Permalink
chore: update socket and add cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
ynwd committed Nov 17, 2024
1 parent 0765f82 commit 01dfdd5
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 36 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
18 changes: 14 additions & 4 deletions modules/hook/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 = () => {
Expand All @@ -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) => {
Expand All @@ -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]);
Expand Down
3 changes: 1 addition & 2 deletions modules/socket/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
60 changes: 60 additions & 0 deletions utils/cmd.ts
Original file line number Diff line number Diff line change
@@ -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);
}
29 changes: 0 additions & 29 deletions utils/kv.ts

This file was deleted.

0 comments on commit 01dfdd5

Please sign in to comment.