Skip to content

Commit cbae5a3

Browse files
committed
check to enter
1 parent 77485f1 commit cbae5a3

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

server/server.ts

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ io.on('connection', (client) => {
109109
try {
110110
let userList,
111111
hostID,
112-
correctPassword = false;
112+
userMayEnter = false;
113113

114114
if (roomLists[roomCode] && method === 'CREATE') {
115115
io.emit('room is already taken');
@@ -119,6 +119,7 @@ io.on('connection', (client) => {
119119
roomLists[roomCode] = {};
120120
roomLists[roomCode][client.id] = { userName, password: roomPassword };
121121

122+
userMayEnter = true;
122123
io.emit('user created a new room');
123124
}
124125

@@ -132,7 +133,7 @@ io.on('connection', (client) => {
132133
password: roomPassword
133134
};
134135

135-
correctPassword = true;
136+
userMayEnter = true;
136137
io.emit('correct password');
137138
} else {
138139
io.emit('wrong password');
@@ -142,32 +143,34 @@ io.on('connection', (client) => {
142143
userList = Object.keys(roomLists[roomCode]);
143144
hostID = userList[0];
144145

145-
const newClientID = userList[userList.length - 1];
146-
//server ask host for the current state
147-
const hostState = await io //once the request is sent back save to host state
148-
.timeout(5000)
149-
.to(hostID) // sends only to host
150-
.emitWithAck('requesting state from host'); //sending request
151-
152-
//share host's state with the latest user
153-
const newClientResponse = await io
154-
.timeout(5000)
155-
.to(newClientID) // sends only to new client
156-
.emitWithAck('server emitting state from host', hostState[0]); //Once the server got host state, sending state to the new client
157-
158-
if (newClientResponse[0].status === 'confirmed') {
159-
client.join(roomCode); //client joining a room
160-
io.to(roomCode).emit(
161-
'updateUserList',
162-
{
163-
userList: Object.values(roomLists[roomCode])
164-
} // send updated userList to all users in room
165-
);
166-
io.to(roomCode).emit('new chat message', {
167-
userName,
168-
message: `${userName} joined chat room`,
169-
type: 'activity'
170-
});
146+
if (userMayEnter === true) {
147+
const newClientID = userList[userList.length - 1];
148+
//server ask host for the current state
149+
const hostState = await io //once the request is sent back save to host state
150+
.timeout(5000)
151+
.to(hostID) // sends only to host
152+
.emitWithAck('requesting state from host'); //sending request
153+
154+
//share host's state with the latest user
155+
const newClientResponse = await io
156+
.timeout(5000)
157+
.to(newClientID) // sends only to new client
158+
.emitWithAck('server emitting state from host', hostState[0]); //Once the server got host state, sending state to the new client
159+
160+
if (newClientResponse[0].status === 'confirmed') {
161+
client.join(roomCode); //client joining a room
162+
io.to(roomCode).emit(
163+
'updateUserList',
164+
{
165+
userList: Object.values(roomLists[roomCode])
166+
} // send updated userList to all users in room
167+
);
168+
io.to(roomCode).emit('new chat message', {
169+
userName,
170+
message: `${userName} joined chat room`,
171+
type: 'activity'
172+
});
173+
}
171174
}
172175
} catch (error) {
173176
console.log(

0 commit comments

Comments
 (0)