Skip to content

Commit b6542c2

Browse files
committed
Add check if user exists in socket
1 parent 459479f commit b6542c2

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export class MatchGateway implements OnGatewayInit {
145145
}
146146

147147
async handleConnection(@ConnectedSocket() client: Socket) {
148-
const id = client.handshake.query.userId;
148+
const id = client.handshake.query.userId as string;
149149

150150
if (!id) {
151151
client.emit(
@@ -157,6 +157,17 @@ export class MatchGateway implements OnGatewayInit {
157157
}
158158

159159
try {
160+
// Check if user is already connected
161+
const existingSocketId = this.userSockets.get(id);
162+
if (existingSocketId) {
163+
client.emit(
164+
EXCEPTION,
165+
'Error connecting to /match socket: User already connected.',
166+
);
167+
return;
168+
}
169+
170+
// Check if valid user exists in database
160171
const existingUser = await firstValueFrom(
161172
this.userClient.send({ cmd: 'get-user-by-id' }, id),
162173
);

0 commit comments

Comments
 (0)