@@ -109,7 +109,7 @@ io.on('connection', (client) => {
109
109
try {
110
110
let userList ,
111
111
hostID ,
112
- correctPassword = false ;
112
+ userMayEnter = false ;
113
113
114
114
if ( roomLists [ roomCode ] && method === 'CREATE' ) {
115
115
io . emit ( 'room is already taken' ) ;
@@ -119,6 +119,7 @@ io.on('connection', (client) => {
119
119
roomLists [ roomCode ] = { } ;
120
120
roomLists [ roomCode ] [ client . id ] = { userName, password : roomPassword } ;
121
121
122
+ userMayEnter = true ;
122
123
io . emit ( 'user created a new room' ) ;
123
124
}
124
125
@@ -132,7 +133,7 @@ io.on('connection', (client) => {
132
133
password : roomPassword
133
134
} ;
134
135
135
- correctPassword = true ;
136
+ userMayEnter = true ;
136
137
io . emit ( 'correct password' ) ;
137
138
} else {
138
139
io . emit ( 'wrong password' ) ;
@@ -142,32 +143,34 @@ io.on('connection', (client) => {
142
143
userList = Object . keys ( roomLists [ roomCode ] ) ;
143
144
hostID = userList [ 0 ] ;
144
145
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
+ }
171
174
}
172
175
} catch ( error ) {
173
176
console . log (
0 commit comments