@@ -87,7 +87,6 @@ app.use('/auth', authRoutes);
8787import { createServer } from 'http' ;
8888import { Server } from 'socket.io' ;
8989
90-
9190const httpServer = createServer ( app ) ;
9291const io = new Server ( httpServer , {
9392 transports : [ 'websocket' ] ,
@@ -96,34 +95,35 @@ const io = new Server(httpServer, {
9695 }
9796} ) ;
9897//creating map for user list
99- const userList = new Map ( ) ;
98+ const userList = { } ;
10099io . on ( 'connection' , ( socket ) => {
101-
102100 console . log ( 'Socket ID: -----' , socket . id ) ;
103101 socket . on ( 'custom-event' , ( redux_store , room ) => {
104102 if ( room ) {
105103 //sending to sender client, only if they are in room
104+ console . log ( 'emiting to room receive message event to front end' ) ;
106105 socket . to ( room ) . emit ( 'receive message' , redux_store ) ;
107106 } else {
108107 //send to all connected clients except the one that sent the message
108+ console . log ( 'emiting broadcast receive message event to front end' ) ;
109109 socket . broadcast . emit ( 'receive message' , redux_store ) ;
110110 }
111111 } ) ;
112112 socket . on ( 'room-code' , ( roomCode ) => {
113113 //working
114114 socket . join ( roomCode ) ;
115115 } ) ;
116- socket . on ( 'user ' , ( userName ) => {
116+ socket . on ( 'userJoined ' , ( userName ) => {
117117 //working
118- userList . set ( socket . id , userName ) ;
119- io . emit ( 'updateUserList' , userList ) ;
118+ userList [ socket . id ] = userName ;
119+ io . emit ( 'updateUserList' , userList ) ; //work on this
120120 } ) ;
121121 socket . on ( 'disconnect' , ( ) => {
122- const userName = userList . get ( socket . id ) ;
122+ const userName = userList [ socket . id ] ;
123123 console . log ( 'User list before remove user' , userList ) ;
124- userList . delete ( socket . id ) ; //remove the user from the obj
124+ delete userList [ socket . id ] ; //remove the user from the obj
125125 console . log ( 'User list after remove user' , userList ) ;
126- io . emit ( 'disconnected ' , userName ) ;
126+ io . emit ( 'updateUserList ' , userList ) ; //check this
127127 } ) ;
128128} ) ;
129129
0 commit comments