@@ -94,8 +94,8 @@ const io = new Server(httpServer, {
94
94
origin : [ 'http://localhost:5656' , 'http://localhost:8080' , API_BASE_URL ]
95
95
}
96
96
} ) ;
97
- //creating map for user list
98
- const userList = { } ;
97
+
98
+ const roomLists = { } ; //key: roomCode, value: Obj{ socketid: username }
99
99
io . on ( 'connection' , ( client ) => {
100
100
client . on ( 'custom-event' , ( redux_store , room ) => {
101
101
if ( room ) {
@@ -112,34 +112,33 @@ io.on('connection', (client) => {
112
112
client . join ( roomCode ) ;
113
113
} ) ;
114
114
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
+ ) ;
126
125
} ) ;
127
126
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
+ }
133
139
} ) ;
134
140
} ) ;
135
141
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
- // });
143
142
/*
144
143
GraphQl Router
145
144
*/
0 commit comments