Skip to content

Commit 91d49cb

Browse files
committed
Enable cors for websocket
1 parent 717f5e0 commit 91d49cb

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

backend/gateway-service/src/modules/match/match.controller.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,23 @@ import { Inject } from '@nestjs/common';
1212
import { firstValueFrom } from 'rxjs';
1313
import { RedisService } from './redis.service';
1414
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';
1622

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+
})
1832
export class MatchGateway implements OnGatewayInit {
1933
@WebSocketServer() server: Server;
2034
private userSockets: Map<string, string> = new Map();
@@ -28,7 +42,7 @@ export class MatchGateway implements OnGatewayInit {
2842
// Subscribe to Redis Pub/Sub for match notifications
2943
this.redisService.subscribeToMatchEvents((matchedUsers) => {
3044
this.notifyUsersWithMatch(matchedUsers);
31-
});
45+
});
3246

3347
this.redisService.subscribeToTimeoutEvents((timedOutUsers) => {
3448
this.notifyUsersWithTimeout(timedOutUsers);
@@ -50,7 +64,11 @@ export class MatchGateway implements OnGatewayInit {
5064
try {
5165
firstValueFrom(this.matchingClient.send('match.request', matchPayload))
5266
.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+
);
5472
this.server.to(client.id).emit(MATCH_REQUESTED, {
5573
message: `Match request sent to the matching service.`,
5674
});
@@ -61,10 +79,19 @@ export class MatchGateway implements OnGatewayInit {
6179
}
6280

6381
@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+
)
6689
.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+
);
6895
this.server.to(client.id).emit(MATCH_CANCELLED, {
6996
message: `You have been cancelled from the match.`,
7097
});
@@ -88,7 +115,7 @@ export class MatchGateway implements OnGatewayInit {
88115
}
89116

90117
notifyUsersWithTimeout(timedOutUsers: string[]) {
91-
timedOutUsers.forEach(user => {
118+
timedOutUsers.forEach((user) => {
92119
const socketId = this.getUserSocketId(user);
93120
if (socketId) {
94121
this.server.to(socketId).emit(MATCH_TIMEOUT, {
@@ -117,7 +144,11 @@ export class MatchGateway implements OnGatewayInit {
117144
// Remove user from Redis pool
118145
firstValueFrom(this.matchingClient.send('match.cancel', { userId }))
119146
.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+
);
121152
console.log(`User ${userId} disconnected`);
122153
}
123154
}

0 commit comments

Comments
 (0)