@@ -12,9 +12,23 @@ 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_FOUND , MATCH_CANCELLED , MATCH_CONFIRMED , MATCH_TIMEOUT , MATCH_REQUESTED } from './match.event' ;
15
+ import {
16
+ MATCH_FOUND ,
17
+ MATCH_CANCELLED ,
18
+ MATCH_CONFIRMED ,
19
+ MATCH_TIMEOUT ,
20
+ MATCH_REQUESTED ,
21
+ } from './match.event' ;
16
22
17
- @WebSocketGateway ( { namespace : '/match' } )
23
+ @WebSocketGateway ( {
24
+ namespace : '/match' ,
25
+ cors : {
26
+ origin : '*' ,
27
+ methods : [ 'GET' , 'POST' ] ,
28
+ allowedHeaders : [ 'Content-Type' , 'Accept' ] ,
29
+ credentials : true ,
30
+ } ,
31
+ } )
18
32
export class MatchGateway implements OnGatewayInit {
19
33
@WebSocketServer ( ) server : Server ;
20
34
private userSockets : Map < string , string > = new Map ( ) ;
@@ -28,7 +42,7 @@ export class MatchGateway implements OnGatewayInit {
28
42
// Subscribe to Redis Pub/Sub for match notifications
29
43
this . redisService . subscribeToMatchEvents ( ( matchedUsers ) => {
30
44
this . notifyUsersWithMatch ( matchedUsers ) ;
31
- } ) ;
45
+ } ) ;
32
46
33
47
this . redisService . subscribeToTimeoutEvents ( ( timedOutUsers ) => {
34
48
this . notifyUsersWithTimeout ( timedOutUsers ) ;
@@ -50,7 +64,11 @@ export class MatchGateway implements OnGatewayInit {
50
64
try {
51
65
firstValueFrom ( this . matchingClient . send ( 'match.request' , matchPayload ) )
52
66
. then ( ( ) => console . log ( `Match requested for user ${ payload . userId } ` ) )
53
- . catch ( ( error ) => console . error ( `Error requesting match for user ${ payload . userId } : ${ error . message } ` ) ) ;
67
+ . catch ( ( error ) =>
68
+ console . error (
69
+ `Error requesting match for user ${ payload . userId } : ${ error . message } ` ,
70
+ ) ,
71
+ ) ;
54
72
this . server . to ( client . id ) . emit ( MATCH_REQUESTED , {
55
73
message : `Match request sent to the matching service.` ,
56
74
} ) ;
@@ -61,10 +79,19 @@ export class MatchGateway implements OnGatewayInit {
61
79
}
62
80
63
81
@SubscribeMessage ( 'matchCancel' )
64
- async handleCancelMatch ( @ConnectedSocket ( ) client : Socket , @MessageBody ( ) payload : { userId : string } ) {
65
- firstValueFrom ( this . matchingClient . send ( 'match.cancel' , { userId : payload . userId } ) )
82
+ async handleCancelMatch (
83
+ @ConnectedSocket ( ) client : Socket ,
84
+ @MessageBody ( ) payload : { userId : string } ,
85
+ ) {
86
+ firstValueFrom (
87
+ this . matchingClient . send ( 'match.cancel' , { userId : payload . userId } ) ,
88
+ )
66
89
. then ( ( ) => console . log ( `Match canceled for user ${ payload . userId } ` ) )
67
- . catch ( ( error ) => console . error ( `Error canceling match for user ${ payload . userId } : ${ error . message } ` ) ) ;
90
+ . catch ( ( error ) =>
91
+ console . error (
92
+ `Error canceling match for user ${ payload . userId } : ${ error . message } ` ,
93
+ ) ,
94
+ ) ;
68
95
this . server . to ( client . id ) . emit ( MATCH_CANCELLED , {
69
96
message : `You have been cancelled from the match.` ,
70
97
} ) ;
@@ -88,7 +115,7 @@ export class MatchGateway implements OnGatewayInit {
88
115
}
89
116
90
117
notifyUsersWithTimeout ( timedOutUsers : string [ ] ) {
91
- timedOutUsers . forEach ( user => {
118
+ timedOutUsers . forEach ( ( user ) => {
92
119
const socketId = this . getUserSocketId ( user ) ;
93
120
if ( socketId ) {
94
121
this . server . to ( socketId ) . emit ( MATCH_TIMEOUT , {
@@ -117,7 +144,11 @@ export class MatchGateway implements OnGatewayInit {
117
144
// Remove user from Redis pool
118
145
firstValueFrom ( this . matchingClient . send ( 'match.cancel' , { userId } ) )
119
146
. then ( ( ) => console . log ( `Match canceled for user ${ userId } ` ) )
120
- . catch ( ( error ) => console . error ( `Error canceling match for user ${ userId } : ${ error . message } ` ) ) ;
147
+ . catch ( ( error ) =>
148
+ console . error (
149
+ `Error canceling match for user ${ userId } : ${ error . message } ` ,
150
+ ) ,
151
+ ) ;
121
152
console . log ( `User ${ userId } disconnected` ) ;
122
153
}
123
154
}
0 commit comments