Skip to content

Commit 0de947e

Browse files
committed
wait for server to emit state from host b4 joining
1 parent d46b21f commit 0de947e

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

app/src/components/left/RoomsContainer.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const RoomsContainer = () => {
5757
// console.log('userList[0]-------', userList[0]);
5858
// }, [userList]);
5959

60-
function initSocketConnection(roomCode) {
60+
function initSocketConnection(roomCode: string) {
6161
if (socket) socket.disconnect(); //edge case check if socket connection existed
6262

6363
socket = io(API_BASE_URL, {
@@ -69,6 +69,11 @@ const RoomsContainer = () => {
6969
console.log(`Your Nickname Is: ${userName}`);
7070
//passing current client nickname and room code to server
7171
socket.emit('joining', userName, roomCode);
72+
socket.on('back emitting state from host', (state) => {
73+
store.dispatch(allCooperativeState(state.appState));
74+
store.dispatch(codePreviewCooperative(state.codePreviewCooperative));
75+
store.dispatch(cooperativeStyle(state.styleSlice));
76+
});
7277
});
7378

7479
//listening to back end for updating user list

server/server.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,24 @@ io.on('connection', (client) => {
105105
});
106106

107107
//when user Joined a room
108-
client.on('joining', (userName, roomCode) => {
109-
client.join(roomCode); //client joining a room
108+
client.on('joining', (userName: string, roomCode: string) => {
110109
//if room exists, get state from host
111110
if (roomLists[roomCode]) {
112111
let hostID = Object.keys(roomLists[roomCode])[0];
113112
console.log('back requesting state from host');
114113
console.log('host username:', roomLists[roomCode][hostID]);
115114
io.to(roomLists[roomCode][hostID]).emit('requesting state from host');
116115
client.on('state from host', (state) => {
117-
io.to(client.id).emit('new state from back', state);
116+
io.to(client.id).emit('back emitting state from host', state);
118117
});
119118
//if no room exist, create new room in server
120119
} else if (!roomLists[roomCode]) {
121120
roomLists[roomCode] = {};
122121
}
123122
roomLists[roomCode][client.id] = userName; // add user into the room with id: userName
123+
124+
client.join(roomCode); //client joining a room
125+
124126
console.log('back emitting new user list');
125127
io.to(roomCode).emit('updateUserList', roomLists[roomCode]); //send the message to all clients in room
126128

0 commit comments

Comments
 (0)