Skip to content

Commit d5c9f91

Browse files
committed
Add connected event
1 parent 0ab91ae commit d5c9f91

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
MATCH_ERROR,
2424
EXCEPTION,
2525
MATCH_DECLINED,
26+
CONNECTED,
2627
} from './match.event';
2728
import {
2829
ACCEPT_MATCH,
@@ -68,16 +69,13 @@ export class MatchGateway implements OnGatewayInit {
6869
@ConnectedSocket() client: Socket,
6970
@MessageBody() payload: MatchRequestDto,
7071
) {
71-
if (
72-
!payload.userId ||
73-
!payload.selectedTopic ||
74-
!payload.selectedDifficulty
75-
) {
72+
const { userId, selectedTopic, selectedDifficulty } = payload;
73+
if (!userId || !selectedTopic || !selectedDifficulty) {
7674
client.emit(MATCH_ERROR, 'Invalid match request payload.');
7775
return;
7876
}
7977

80-
if (!this.validateUserId(client, payload.userId)) {
78+
if (!this.validateUserId(client, userId)) {
8179
return;
8280
}
8381

@@ -104,18 +102,19 @@ export class MatchGateway implements OnGatewayInit {
104102
@ConnectedSocket() client: Socket,
105103
@MessageBody() payload: { userId: string },
106104
) {
107-
if (!payload.userId) {
105+
const { userId } = payload;
106+
if (!userId) {
108107
client.emit(MATCH_ERROR, 'Invalid userId in payload.');
109108
return;
110109
}
111110

112-
if (!this.validateUserId(client, payload.userId)) {
111+
if (!this.validateUserId(client, userId)) {
113112
return;
114113
}
115114

116115
try {
117116
const result = await firstValueFrom(
118-
this.matchingClient.send('match-cancel', { userId: payload.userId }),
117+
this.matchingClient.send('match-cancel', { userId: userId }),
119118
);
120119

121120
if (result.success) {
@@ -280,38 +279,40 @@ export class MatchGateway implements OnGatewayInit {
280279
}
281280

282281
async handleConnection(@ConnectedSocket() client: Socket) {
283-
const id = client.handshake.query.userId as string;
282+
const userId = client.handshake.query.userId as string;
284283

285-
if (!id) {
284+
if (!userId) {
286285
this.emitExceptionAndDisconnect(client, 'Invalid userId.');
287286
return;
288287
}
289288

290289
try {
291290
// 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) {
294293
this.emitExceptionAndDisconnect(
295294
client,
296-
`User ${id} is already connected with socket ID ${existingSocketId}`,
295+
`User ${userId} is already connected with socket ID ${existingSocketId}`,
297296
);
298297
return;
299298
}
300299

301300
// Check if valid user exists in database
302301
const existingUser = await firstValueFrom(
303-
this.userClient.send({ cmd: 'get-user-by-id' }, id),
302+
this.userClient.send({ cmd: 'get-user-by-id' }, userId),
304303
);
305304

306305
if (!existingUser) {
307-
this.emitExceptionAndDisconnect(client, `User ${id} not found.`);
306+
this.emitExceptionAndDisconnect(client, `User ${userId} not found.`);
308307
return;
309308
}
310309

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}`);
315316
} catch (error) {
316317
this.emitExceptionAndDisconnect(client, error.message);
317318
return;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export const CONNECTED = 'connected';
12
export const MATCH_FOUND = 'matchFound';
23
export const MATCH_REQUESTED = 'matchRequested';
34
export const MATCH_CANCELLED = 'matchCancelled';

0 commit comments

Comments
 (0)