@@ -63,12 +63,41 @@ wss.on("connection", function connection(ws, req) {
6363 }
6464
6565 const userId = req . user . id ;
66- const user : User = {
66+ let user : User = {
6767 userId,
6868 userName : userId ,
6969 ws,
7070 rooms : [ ] ,
7171 } ;
72+
73+ const existingUserIndex = users . findIndex ( ( u ) => u . userId === userId ) ;
74+ if ( existingUserIndex !== - 1 ) {
75+ console . log ( `User ${ userId } reconnecting - cleaning up old connection` ) ;
76+ const existingUser = users [ existingUserIndex ] ;
77+ if ( ! existingUser ) {
78+ console . error ( "Error: Existing User not found." ) ;
79+ return ;
80+ }
81+ // Keep the rooms they were in
82+ const existingRooms = [ ...existingUser . rooms ] ;
83+ // Remove old connection
84+ users . splice ( existingUserIndex , 1 ) ;
85+ // Add back with new connection but keep rooms
86+ user = {
87+ userId,
88+ userName : existingUser . userName || userId ,
89+ ws,
90+ rooms : existingRooms ,
91+ } ;
92+ } else {
93+ user = {
94+ userId,
95+ userName : userId ,
96+ ws,
97+ rooms : [ ] ,
98+ } ;
99+ }
100+
72101 users . push ( user ) ;
73102
74103 console . log ( `User ${ userId } connected` ) ;
@@ -97,8 +126,40 @@ wss.on("connection", function connection(ws, req) {
97126 console . error ( "No roomId provided for JOIN message" ) ;
98127 return ;
99128 }
129+
130+ if ( ! user . rooms . includes ( parsedData . roomId ) ) {
131+ user . rooms . push ( parsedData . roomId ) ;
132+ }
133+
134+ const uniqueParticipantsMap = new Map ( ) ;
135+ users
136+ . filter (
137+ ( u ) => u . rooms . includes ( parsedData . roomId ! )
138+ )
139+ . forEach ( ( u ) =>
140+ uniqueParticipantsMap . set ( u . userId , {
141+ userId : u . userId ,
142+ userName : u . userName ,
143+ } )
144+ ) ;
145+
146+ const currentParticipants = Array . from (
147+ uniqueParticipantsMap . values ( )
148+ ) ;
149+
150+ ws . send (
151+ JSON . stringify ( {
152+ type : WS_DATA_TYPE . USER_JOINED ,
153+ userId : user . userId ,
154+ roomId : parsedData . roomId ,
155+ userName : parsedData . userName ,
156+ timestamp : new Date ( ) . toISOString ( ) ,
157+ participants : currentParticipants ,
158+ } )
159+ ) ;
160+
100161 console . log ( `User ${ userId } joining room ${ parsedData . roomId } ` ) ;
101- user . rooms . push ( parsedData . roomId ) ;
162+
102163 broadcastToRoom (
103164 parsedData . roomId ,
104165 {
@@ -107,8 +168,10 @@ wss.on("connection", function connection(ws, req) {
107168 roomId : parsedData . roomId ,
108169 userName : parsedData . userName ,
109170 timestamp : new Date ( ) . toISOString ( ) ,
171+ participants : currentParticipants ,
110172 } ,
111- [ ]
173+ [ user . userId ] ,
174+ false
112175 ) ;
113176 break ;
114177
@@ -124,7 +187,8 @@ wss.on("connection", function connection(ws, req) {
124187 userName : user . userName ,
125188 roomId : parsedData . roomId ,
126189 } ,
127- [ user . userId ]
190+ [ user . userId ] ,
191+ true
128192 ) ;
129193 break ;
130194
@@ -266,8 +330,23 @@ wss.on("connection", function connection(ws, req) {
266330function broadcastToRoom (
267331 roomId : string ,
268332 message : WebSocketMessage ,
269- excludeUsers : string [ ] = [ ]
333+ excludeUsers : string [ ] = [ ] ,
334+ includeParticipants : boolean = false
270335) {
336+ if ( includeParticipants && ! message . participants ) {
337+ const uniqueParticipantsMap = new Map ( ) ;
338+ users
339+ . filter ( ( u ) => u . rooms . includes ( roomId ) )
340+ . forEach ( ( u ) =>
341+ uniqueParticipantsMap . set ( u . userId , {
342+ userId : u . userId ,
343+ userName : u . userName ,
344+ } )
345+ ) ;
346+
347+ const currentParticipants = Array . from ( uniqueParticipantsMap . values ( ) ) ;
348+ message . participants = currentParticipants ;
349+ }
271350 users . forEach ( ( u ) => {
272351 if ( u . rooms . includes ( roomId ) && ! excludeUsers . includes ( u . userId ) ) {
273352 u . ws . send ( JSON . stringify ( message ) ) ;
0 commit comments