@@ -12,7 +12,7 @@ import { Inject } from '@nestjs/common';
12
12
import { firstValueFrom } from 'rxjs' ;
13
13
import { RedisService } from './redis.service' ;
14
14
import { MatchRequestDto } from './dto' ;
15
- import { MATCH_SUCCESS , MATCH_TIMEOUT } from './match.event' ;
15
+ import { MATCH_FOUND , MATCH_CANCELLED , MATCH_CONFIRMED , MATCH_TIMEOUT , MATCH_REQUESTED } from './match.event' ;
16
16
17
17
@WebSocketGateway ( { namespace : '/match' } )
18
18
export class MatchGateway implements OnGatewayInit {
@@ -48,27 +48,39 @@ export class MatchGateway implements OnGatewayInit {
48
48
49
49
// Send the match request to the microservice
50
50
try {
51
- await firstValueFrom ( this . matchingClient . send ( 'match.request' , matchPayload ) ) ;
51
+ firstValueFrom ( this . matchingClient . send ( 'match.request' , matchPayload ) )
52
+ . then ( ( ) => console . log ( `Match requested for user ${ payload . userId } ` ) )
53
+ . catch ( ( error ) => console . error ( `Error requesting match for user ${ payload . userId } : ${ error . message } ` ) ) ;
54
+ this . server . to ( client . id ) . emit ( MATCH_REQUESTED , {
55
+ message : `Match request sent to the matching service.` ,
56
+ } ) ;
52
57
} catch ( error ) {
53
58
client . emit ( 'matchError' , `Error requesting match: ${ error . message } ` ) ;
54
59
return ;
55
60
}
56
61
}
57
62
63
+ @SubscribeMessage ( 'matchCancel' )
64
+ async handleCancelMatch ( @ConnectedSocket ( ) client : Socket , @MessageBody ( ) payload : { userId : string } ) {
65
+ firstValueFrom ( this . matchingClient . send ( 'match.cancel' , { userId : payload . userId } ) )
66
+ . then ( ( ) => console . log ( `Match canceled for user ${ payload . userId } ` ) )
67
+ . catch ( ( error ) => console . error ( `Error canceling match for user ${ payload . userId } : ${ error . message } ` ) ) ;
68
+ this . server . to ( client . id ) . emit ( MATCH_CANCELLED , {
69
+ message : `You have been cancelled from the match.` ,
70
+ } ) ;
71
+ }
72
+
58
73
// Notify both matched users via WebSocket
59
74
notifyUsersWithMatch ( matchedUsers : string [ ] ) {
60
75
const [ user1 , user2 ] = matchedUsers ;
61
76
const user1SocketId = this . getUserSocketId ( user1 ) ;
62
77
const user2SocketId = this . getUserSocketId ( user2 ) ;
63
- if ( user1SocketId ) {
64
- this . server . to ( user1SocketId ) . emit ( MATCH_SUCCESS , {
78
+ if ( user1SocketId && user2SocketId ) {
79
+ this . server . to ( user1SocketId ) . emit ( MATCH_FOUND , {
65
80
message : `You have been matched with user ${ user2 } ` ,
66
81
matchedUserId : user2 ,
67
82
} ) ;
68
- }
69
-
70
- if ( user2SocketId ) {
71
- this . server . to ( user2SocketId ) . emit ( MATCH_SUCCESS , {
83
+ this . server . to ( user2SocketId ) . emit ( MATCH_FOUND , {
72
84
message : `You have been matched with user ${ user1 } ` ,
73
85
matchedUserId : user1 ,
74
86
} ) ;
@@ -95,13 +107,17 @@ export class MatchGateway implements OnGatewayInit {
95
107
}
96
108
}
97
109
98
- handleDisconnect ( @ConnectedSocket ( ) client : Socket ) {
110
+ async handleDisconnect ( @ConnectedSocket ( ) client : Socket ) {
99
111
const userId = [ ...this . userSockets . entries ( ) ] . find (
100
112
( [ , socketId ] ) => socketId === client . id ,
101
113
) ?. [ 0 ] ;
102
114
103
115
if ( userId ) {
104
116
this . userSockets . delete ( userId ) ;
117
+ // Remove user from Redis pool
118
+ firstValueFrom ( this . matchingClient . send ( 'match.cancel' , { userId } ) )
119
+ . then ( ( ) => console . log ( `Match canceled for user ${ userId } ` ) )
120
+ . catch ( ( error ) => console . error ( `Error canceling match for user ${ userId } : ${ error . message } ` ) ) ;
105
121
console . log ( `User ${ userId } disconnected` ) ;
106
122
}
107
123
}
0 commit comments