Skip to content

Commit df21ede

Browse files
authored
Merge pull request #12 from oslabs-beta/nam
Fix User List bug, fully functional
2 parents 1f3d37b + 0fcd25a commit df21ede

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

app/src/components/left/RoomsContainer.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const RoomsContainer = () => {
5555
socket.emit('join-room', roomCode); // Join the room when connected
5656
console.log(`Your Nickname Is: ${userName}`);
5757
//passing current client nickname to server
58-
socket.emit('userJoined', userName, roomCode);
58+
socket.emit('joining', userName, roomCode);
5959
//listening to back end for updating user list
6060
socket.on('updateUserList', (newUserList) => {
6161
dispatch(setUserList(Object.values(newUserList)));
@@ -114,7 +114,6 @@ const RoomsContainer = () => {
114114

115115
function leaveRoom() {
116116
if (socket) {
117-
socket.emit('updateUserDisconnect', roomCode);
118117
socket.disconnect();
119118
} //disconnecting socket functionality
120119
dispatch(setRoomCode(''));
@@ -125,13 +124,9 @@ const RoomsContainer = () => {
125124

126125
//checking if both text field have any input (not including spaces)
127126
function checkInputField(...inputs: any) {
128-
let userName: String = inputs[0].trim();
129-
let roomCode: String = inputs[1].trim();
130-
if (userName.length !== 0 && roomCode.length !== 0) {
131-
return false;
132-
} else {
133-
return true;
134-
}
127+
let userName: string = inputs[0].trim();
128+
let roomCode: string = inputs[1].trim();
129+
return userName.length === 0 || roomCode.length === 0;
135130
}
136131

137132
return (

server/server.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ const io = new Server(httpServer, {
9494
origin: ['http://localhost:5656', 'http://localhost:8080', API_BASE_URL]
9595
}
9696
});
97-
//creating map for user list
98-
const userList = {};
97+
98+
const roomLists = {}; //key: roomCode, value: Obj{ socketid: username }
9999
io.on('connection', (client) => {
100100
client.on('custom-event', (redux_store, room) => {
101101
if (room) {
@@ -112,34 +112,33 @@ io.on('connection', (client) => {
112112
client.join(roomCode);
113113
});
114114

115-
client.on('userJoined', (userName, roomCode) => {
116-
//working
117-
userList[client.id] = userName;
118-
io.in(roomCode).emit('updateUserList', userList); //send the message to all clients in room
119-
console.log('User list when user Joined', userList);
120-
});
121-
122-
client.on('updateUserDisconnect', (roomCode) => { //leave room function
123-
delete userList[client.id]; //remove the user from the obj
124-
console.log('User list after User Left', userList);
125-
io.in(roomCode).emit('updateUserList', userList); //send the message to all client but the sender.
115+
//when user Joined a room
116+
client.on('joining', (userName, roomCode) => {
117+
if (!roomLists[roomCode]) roomLists[roomCode] = {}; //if no room exist, create new room in server
118+
roomLists[roomCode][client.id] = userName; // add user into the room with id: userName
119+
io.in(roomCode).emit('updateUserList', roomLists[roomCode]); //send the message to all clients in room
120+
console.log('full room lists', roomLists);
121+
console.log(
122+
`User list of room ${roomCode}: `,
123+
roomLists[roomCode]
124+
);
126125
});
127126

128-
client.on('disconnect', () => { //connection drop function
129-
// const userName = userList[client.id];
130-
delete userList[client.id]; //remove the user from the obj
131-
console.log('User list after User Left', userList);
132-
io.emit('updateUserList', userList); //send the message to all client but the sender.
127+
client.on('disconnecting', () => {
128+
// the client.rooms Set contains at least the socket ID
129+
const roomCode = Array.from(client.rooms)[1]; //grabbing current room client was in when disconnecting
130+
delete roomLists[roomCode][client.id];
131+
//if room empty, delete room from room list
132+
if (!Object.keys(roomLists[roomCode]).length) {
133+
delete roomLists[roomCode];
134+
} else {
135+
//else emit updated user list
136+
console.log('User list after User Left', roomLists[roomCode]);
137+
io.to(roomCode).emit('updateUserList', roomLists[roomCode]);
138+
}
133139
});
134140
});
135141

136-
// app.get('/', userController.getUsername, (req, res) =>{
137-
// const username = req.body.username
138-
// console.log('username: ', username)
139-
// usersInRoom.push({username: username})
140-
// console.log(usersInRoom)
141-
// return res.status(200).json({username: username})
142-
// });
143142
/*
144143
GraphQl Router
145144
*/

0 commit comments

Comments
 (0)