Skip to content

Commit

Permalink
Fix join game race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerkrewson committed Jul 28, 2024
1 parent ca124e7 commit e08f6aa
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pages/[gameCode].js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,24 @@ const Game = ({ loading }) => {
status: "loading",
});
const [isRocketcrab, setIsRocketcrab] = useState(false);
const [isConnected, setIsConnected] = useState(false);

useEffect(() => {
const cl = socket.on("connect", () => setIsConnected(true));
const dl = socket.on("disconnect", () => setIsConnected(false));

return () => {
socket.off("connect", cl);
socket.off("disconnect", dl);
};
}, []);

useEffect(() => {
if (!isConnected) {
socket.connect();
return;
}

const { previousGameCode, previousName } = parseCookies();

if (previousGameCode === gameCode && previousName) {
Expand Down Expand Up @@ -51,7 +67,7 @@ const Game = ({ loading }) => {
socket.close();
setGameState({ status: "loading" });
};
}, []);
}, [isConnected]);

const onNameEntry = (name) => {
socket.emit("name", name);
Expand Down

0 comments on commit e08f6aa

Please sign in to comment.