Skip to content

Commit

Permalink
fix: emit on reset
Browse files Browse the repository at this point in the history
  • Loading branch information
lewxdev committed Aug 21, 2024
1 parent 664b34e commit 9854159
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
7 changes: 2 additions & 5 deletions app/components/socket-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ export function SocketProvider({ children }: React.PropsWithChildren) {
}
});

socket.on("sessionAlive", (sessionId) => {
socket.on("session", (sessionId) => {
socket.auth = { sessionId };
localStorage.setItem("sessionId", sessionId);
setSessionState("alive");
});

socket.on("sessionDead", () => {
setSessionState("dead");
});
socket.on("sessionState", setSessionState);
}, []);

return (
Expand Down
6 changes: 4 additions & 2 deletions app/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ async function main() {

io.on("connection", async (socket) => {
await redis.setSession(socket.data.sessionId, "alive");
socket.emit("sessionAlive", socket.data.sessionId);
socket.emit("session", socket.data.sessionId);
socket.emit("sessionState", "alive");

clientsCount++;
io.emit("clientsCount", clientsCount);
Expand All @@ -44,10 +45,11 @@ async function main() {
socket.on("expose", async (index) => {
if (field.exposeCell(index) === "dead") {
await redis.setSession(socket.data.sessionId, "dead");
socket.emit("sessionDead");
socket.emit("sessionState", "dead");
} else if (field.isComplete) {
field = await Field.create(field.size + 10);
await redis.resetSessions();
io.emit("sessionState", "alive");
}
io.emit("exposedPercent", field.exposedPercent);
io.emit("update", field.plots);
Expand Down
4 changes: 2 additions & 2 deletions app/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export type ServerToClientEvents = {
update(state: Field["plots"]): void;
clientsCount(count: number): void;
exposedPercent(count: number): void;
sessionAlive(sessionId: string): void;
sessionDead(): void;
session(sessionId: string): void;
sessionState(state: SessionState): void;
};

type InterServerEvents = {};
Expand Down

0 comments on commit 9854159

Please sign in to comment.