Skip to content

Commit

Permalink
chore: debug broadcastMessage on prod
Browse files Browse the repository at this point in the history
  • Loading branch information
ynwd committed Dec 4, 2024
1 parent e7069b7 commit d7028b9
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions modules/socket/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ export default function socketModule(s: Fastro) {
data: Data,
) {
const connected = ctx.stores.get("connected");
console.log("connected", connected);
console.log("joinRoom-connected", connected);
console.log("joinRoom-data", data);
if (data.user) {
connected?.set(data.user, { data, socket });
return true;
}

return false;
}

const injectData = async (ctx: Context, data: Data) => {
Expand Down Expand Up @@ -89,18 +93,21 @@ export default function socketModule(s: Fastro) {

socket.onmessage = async (event) => {
const data: Data = JSON.parse(event.data);
await joinRoom(ctx, socket, data);
if (data.type === "ping") {
const res = await joinRoom(ctx, socket, data);
console.log("socket.onmessage-res", res);
if (res && data.type === "ping") {
return await broadcastConnection(ctx, data);
}
if (data.type === "message" && data.message?.msg !== "") {
if (res && data.type === "message" && data.message?.msg !== "") {
broadcastMessage(
ctx,
data.room,
JSON.stringify(data.message),
);
return await injectData(ctx, data);
}

console.log("socket.onmessage-fail to connect", data);
};
socket.onclose = async () => {
const c = ctx.stores.get("connected");
Expand Down

0 comments on commit d7028b9

Please sign in to comment.