Skip to content

Commit

Permalink
Fix name already taken on refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerkrewson committed Jul 28, 2024
1 parent 7b9692b commit 109f37a
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pages/[gameCode].js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ const Game = ({ loading }) => {
const [isRocketcrab, setIsRocketcrab] = useState(false);
const [isConnected, setIsConnected] = useState(socket.connected);

const cleanup = () => {
socket.close();
setGameState({ status: "loading" });
};

useEffect(() => {
const cl = socket.on("connect", () => setIsConnected(true));
window.addEventListener("beforeunload", cleanup);

return () => {
socket.off("connect", cl);
window.removeEventListener("beforeunload", cleanup);
};
}, []);

Expand Down Expand Up @@ -73,11 +80,6 @@ const Game = ({ loading }) => {
setIsRocketcrab(true);
onNameEntry(name);
}

return function cleanup() {
socket.close();
setGameState({ status: "loading" });
};
}, [isConnected]);

const onNameEntry = (name) => {
Expand Down

0 comments on commit 109f37a

Please sign in to comment.