Skip to content

Commit d5df294

Browse files
authored
Merge pull request #29 from kevinsqi/upgrade-socket-io
Upgrade socket.io + implement game timers
2 parents 51a6233 + 034986f commit d5df294

File tree

6 files changed

+210
-411
lines changed

6 files changed

+210
-411
lines changed

client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"react-bootstrap": "^1.0.1",
1111
"react-dom": "^16.13.1",
1212
"react-scripts": "3.4.1",
13-
"socket.io-client": "^2.3.0"
13+
"socket.io-client": "^4.5.1"
1414
},
1515
"scripts": {
1616
"start": "react-scripts start",

client/src/App.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ function App() {
77
const [socket, setSocket] = React.useState(null);
88
const [lobby, setLobby] = React.useState(null);
99
const [playerId, setPlayerId] = React.useState(null);
10+
const [
11+
gameTimeRemainingSeconds,
12+
setGameTimeRemainingSeconds,
13+
] = React.useState(null);
1014

1115
React.useEffect(() => {
1216
const newSocket = io.connect("http://localhost:3001");
@@ -17,6 +21,12 @@ function App() {
1721
setLobby(result);
1822
});
1923

24+
newSocket.on("game:update_timer", (result) => {
25+
console.log("game:update_timer", result);
26+
27+
setGameTimeRemainingSeconds(result.timeRemainingSeconds);
28+
});
29+
2030
// TODO: close socket on unmount
2131
}, []);
2232

@@ -25,7 +35,14 @@ function App() {
2535
}
2636

2737
if (lobby) {
28-
return <Lobby socket={socket} lobby={lobby} playerId={playerId} />;
38+
return (
39+
<Lobby
40+
socket={socket}
41+
lobby={lobby}
42+
playerId={playerId}
43+
timeRemainingSeconds={gameTimeRemainingSeconds}
44+
/>
45+
);
2946
}
3047

3148
return (
@@ -58,7 +75,7 @@ function App() {
5875
);
5976
}
6077

61-
function Lobby({ lobby, playerId, socket }) {
78+
function Lobby({ lobby, playerId, socket, timeRemainingSeconds }) {
6279
if (!lobby.game) {
6380
return (
6481
<div>
@@ -116,6 +133,9 @@ function Lobby({ lobby, playerId, socket }) {
116133
);
117134
})}
118135
</ul>
136+
{timeRemainingSeconds != null && (
137+
<div>Time left: {timeRemainingSeconds}</div>
138+
)}
119139
<div>
120140
{isActivePlayer ? (
121141
`Describe word: ${currentRound.word}`

0 commit comments

Comments
 (0)