@@ -23,6 +23,7 @@ import {
23
23
MATCH_ERROR ,
24
24
EXCEPTION ,
25
25
MATCH_DECLINED ,
26
+ CONNECTED ,
26
27
} from './match.event' ;
27
28
import {
28
29
ACCEPT_MATCH ,
@@ -68,16 +69,13 @@ export class MatchGateway implements OnGatewayInit {
68
69
@ConnectedSocket ( ) client : Socket ,
69
70
@MessageBody ( ) payload : MatchRequestDto ,
70
71
) {
71
- if (
72
- ! payload . userId ||
73
- ! payload . selectedTopic ||
74
- ! payload . selectedDifficulty
75
- ) {
72
+ const { userId, selectedTopic, selectedDifficulty } = payload ;
73
+ if ( ! userId || ! selectedTopic || ! selectedDifficulty ) {
76
74
client . emit ( MATCH_ERROR , 'Invalid match request payload.' ) ;
77
75
return ;
78
76
}
79
77
80
- if ( ! this . validateUserId ( client , payload . userId ) ) {
78
+ if ( ! this . validateUserId ( client , userId ) ) {
81
79
return ;
82
80
}
83
81
@@ -104,18 +102,19 @@ export class MatchGateway implements OnGatewayInit {
104
102
@ConnectedSocket ( ) client : Socket ,
105
103
@MessageBody ( ) payload : { userId : string } ,
106
104
) {
107
- if ( ! payload . userId ) {
105
+ const { userId } = payload ;
106
+ if ( ! userId ) {
108
107
client . emit ( MATCH_ERROR , 'Invalid userId in payload.' ) ;
109
108
return ;
110
109
}
111
110
112
- if ( ! this . validateUserId ( client , payload . userId ) ) {
111
+ if ( ! this . validateUserId ( client , userId ) ) {
113
112
return ;
114
113
}
115
114
116
115
try {
117
116
const result = await firstValueFrom (
118
- this . matchingClient . send ( 'match-cancel' , { userId : payload . userId } ) ,
117
+ this . matchingClient . send ( 'match-cancel' , { userId : userId } ) ,
119
118
) ;
120
119
121
120
if ( result . success ) {
@@ -280,38 +279,40 @@ export class MatchGateway implements OnGatewayInit {
280
279
}
281
280
282
281
async handleConnection ( @ConnectedSocket ( ) client : Socket ) {
283
- const id = client . handshake . query . userId as string ;
282
+ const userId = client . handshake . query . userId as string ;
284
283
285
- if ( ! id ) {
284
+ if ( ! userId ) {
286
285
this . emitExceptionAndDisconnect ( client , 'Invalid userId.' ) ;
287
286
return ;
288
287
}
289
288
290
289
try {
291
290
// Check if user is already connected
292
- const existingSocketId = this . userSockets . get ( id ) ;
293
- if ( existingSocketId ) {
291
+ const existingSocketId = this . userSockets . get ( userId ) ;
292
+ if ( existingSocketId && existingSocketId !== client . id ) {
294
293
this . emitExceptionAndDisconnect (
295
294
client ,
296
- `User ${ id } is already connected with socket ID ${ existingSocketId } ` ,
295
+ `User ${ userId } is already connected with socket ID ${ existingSocketId } ` ,
297
296
) ;
298
297
return ;
299
298
}
300
299
301
300
// Check if valid user exists in database
302
301
const existingUser = await firstValueFrom (
303
- this . userClient . send ( { cmd : 'get-user-by-id' } , id ) ,
302
+ this . userClient . send ( { cmd : 'get-user-by-id' } , userId ) ,
304
303
) ;
305
304
306
305
if ( ! existingUser ) {
307
- this . emitExceptionAndDisconnect ( client , `User ${ id } not found.` ) ;
306
+ this . emitExceptionAndDisconnect ( client , `User ${ userId } not found.` ) ;
308
307
return ;
309
308
}
310
309
311
- if ( id ) {
312
- this . userSockets . set ( id as string , client . id ) ;
313
- console . log ( `User ${ id } connected with socket ID ${ client . id } ` ) ;
314
- }
310
+ this . userSockets . set ( userId , client . id ) ;
311
+
312
+ client . emit ( CONNECTED , {
313
+ message : `User ${ userId } connected with socket ID ${ client . id } ` ,
314
+ } ) ;
315
+ console . log ( `User ${ userId } connected with socket ID ${ client . id } ` ) ;
315
316
} catch ( error ) {
316
317
this . emitExceptionAndDisconnect ( client , error . message ) ;
317
318
return ;
0 commit comments