@@ -63,10 +63,16 @@ export class MatchGateway implements OnGatewayInit {
63
63
! payload . selectedTopic ||
64
64
! payload . selectedDifficulty
65
65
) {
66
- client . emit (
67
- MATCH_ERROR ,
68
- 'Invalid match request. Please check your payload and try again.' ,
69
- ) ;
66
+ client . emit ( EXCEPTION , 'Invalid match request payload.' ) ;
67
+ return ;
68
+ }
69
+
70
+ // Retrieve the userId associated with the current socket
71
+ const storedUserId = [ ...this . userSockets . entries ( ) ] . find (
72
+ ( [ , socketId ] ) => socketId === client . id ,
73
+ ) ?. [ 0 ] ;
74
+ if ( ! storedUserId || storedUserId !== payload . userId ) {
75
+ client . emit ( EXCEPTION , 'UserId does not match the current socket.' ) ;
70
76
return ;
71
77
}
72
78
@@ -80,10 +86,10 @@ export class MatchGateway implements OnGatewayInit {
80
86
message : result . message ,
81
87
} ) ;
82
88
} else {
83
- client . emit ( MATCH_ERROR , result . message ) ;
89
+ client . emit ( EXCEPTION , result . message ) ;
84
90
}
85
91
} catch ( error ) {
86
- client . emit ( MATCH_ERROR , `Error requesting match: ${ error . message } ` ) ;
92
+ client . emit ( EXCEPTION , `Error requesting match: ${ error . message } ` ) ;
87
93
return ;
88
94
}
89
95
}
@@ -95,24 +101,29 @@ export class MatchGateway implements OnGatewayInit {
95
101
) {
96
102
if ( ! payload . userId ) {
97
103
client . emit (
98
- MATCH_ERROR ,
104
+ EXCEPTION ,
99
105
'Invalid userId. Please check your payload and try again.' ,
100
106
) ;
101
107
return ;
102
108
}
103
109
104
- firstValueFrom (
105
- this . matchingClient . send ( 'match-cancel' , { userId : payload . userId } ) ,
106
- )
107
- . then ( ( ) => console . log ( `Match canceled for user ${ payload . userId } ` ) )
108
- . catch ( ( error ) =>
109
- console . error (
110
- `Error canceling match for user ${ payload . userId } : ${ error . message } ` ,
111
- ) ,
110
+ try {
111
+ const result = await firstValueFrom (
112
+ this . matchingClient . send ( 'match-cancel' , { userId : payload . userId } ) ,
112
113
) ;
113
- this . server . to ( client . id ) . emit ( MATCH_CANCELLED , {
114
- message : `You have been cancelled from the match.` ,
115
- } ) ;
114
+
115
+ if ( result . success ) {
116
+ this . server . to ( client . id ) . emit ( MATCH_CANCELLED , {
117
+ message : result . message ,
118
+ } ) ;
119
+ } else {
120
+ client . emit ( EXCEPTION , result . message ) ;
121
+ }
122
+ } catch ( error ) {
123
+ console . log ( error ) ;
124
+ client . emit ( EXCEPTION , `Error canceling match: ${ error . message } ` ) ;
125
+ return ;
126
+ }
116
127
}
117
128
118
129
// Notify both matched users via WebSocket
@@ -148,21 +159,17 @@ export class MatchGateway implements OnGatewayInit {
148
159
const id = client . handshake . query . userId as string ;
149
160
150
161
if ( ! id ) {
151
- client . emit (
152
- EXCEPTION ,
153
- 'Error connecting to /match socket: No userId provided.' ,
154
- ) ;
155
- client . disconnect ( ) ;
162
+ this . emitExceptionAndDisconnect ( client , 'Invalid userId.' ) ;
156
163
return ;
157
164
}
158
165
159
166
try {
160
167
// Check if user is already connected
161
168
const existingSocketId = this . userSockets . get ( id ) ;
162
169
if ( existingSocketId ) {
163
- client . emit (
164
- EXCEPTION ,
165
- 'Error connecting to /match socket: User already connected.' ,
170
+ this . emitExceptionAndDisconnect (
171
+ client ,
172
+ `User ${ id } is already connected with socket ID ${ existingSocketId } ` ,
166
173
) ;
167
174
return ;
168
175
}
@@ -173,11 +180,7 @@ export class MatchGateway implements OnGatewayInit {
173
180
) ;
174
181
175
182
if ( ! existingUser ) {
176
- client . emit (
177
- EXCEPTION ,
178
- 'Error connecting to /match socket: Invalid userId.' ,
179
- ) ;
180
- client . disconnect ( ) ;
183
+ this . emitExceptionAndDisconnect ( client , `User ${ id } not found.` ) ;
181
184
return ;
182
185
}
183
186
@@ -186,10 +189,7 @@ export class MatchGateway implements OnGatewayInit {
186
189
console . log ( `User ${ id } connected with socket ID ${ client . id } ` ) ;
187
190
}
188
191
} catch ( error ) {
189
- client . emit (
190
- EXCEPTION ,
191
- `Error connecting to /match socket: ${ error . message } ` ,
192
- ) ;
192
+ this . emitExceptionAndDisconnect ( client , error . message ) ;
193
193
return ;
194
194
}
195
195
}
@@ -199,21 +199,44 @@ export class MatchGateway implements OnGatewayInit {
199
199
( [ , socketId ] ) => socketId === client . id ,
200
200
) ?. [ 0 ] ;
201
201
202
- if ( userId ) {
203
- this . userSockets . delete ( userId ) ;
202
+ if ( ! userId ) {
203
+ this . emitExceptionAndDisconnect (
204
+ client ,
205
+ 'User not found in userSockets at disconnect.' ,
206
+ ) ;
207
+ return ;
208
+ }
209
+
210
+ try {
204
211
// Remove user from Redis pool
205
- firstValueFrom ( this . matchingClient . send ( 'match-cancel' , { userId } ) )
206
- . then ( ( ) => console . log ( `Match canceled for user ${ userId } ` ) )
207
- . catch ( ( error ) =>
208
- console . error (
209
- `Error canceling match for user ${ userId } : ${ error . message } ` ,
210
- ) ,
212
+ const result = await firstValueFrom (
213
+ this . matchingClient . send ( 'match-cancel' , { userId } ) ,
214
+ ) ;
215
+
216
+ if ( result . success ) {
217
+ console . log ( `Match canceled successfully for user ${ userId } ` ) ;
218
+ } else {
219
+ console . warn (
220
+ `Match cancellation failed for user ${ userId } : ${ result . message } ` ,
211
221
) ;
212
- console . log ( `User ${ userId } disconnected` ) ;
222
+ }
223
+
224
+ this . userSockets . delete ( userId ) ;
225
+ console . log ( `User ${ userId } disconnected and removed from userSockets.` ) ;
226
+ } catch ( error ) {
227
+ client . emit (
228
+ EXCEPTION ,
229
+ `Error canceling match for user ${ userId } : ${ error . message } ` ,
230
+ ) ;
213
231
}
214
232
}
215
233
216
- getUserSocketId ( userId : string ) : string | undefined {
234
+ private getUserSocketId ( userId : string ) : string | undefined {
217
235
return this . userSockets . get ( userId ) ;
218
236
}
237
+
238
+ private emitExceptionAndDisconnect ( client : Socket , message : string ) {
239
+ client . emit ( EXCEPTION , `Error connecting to /match socket: ${ message } ` ) ;
240
+ client . disconnect ( ) ;
241
+ }
219
242
}
0 commit comments