Skip to content

Commit 5a77bab

Browse files
committed
Fix socket not closing on timeout
1 parent 40d7c95 commit 5a77bab

File tree

2 files changed

+78
-64
lines changed

2 files changed

+78
-64
lines changed

frontend/src/api/user.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ export const login = async (email: string, password: string) => {
7474

7575
switch (response.status) {
7676
case 200:
77-
return handleSuccessfulLogin(data.data);
77+
handleSuccessfulLogin(data.data);
78+
break;
7879
case 401:
7980
toast.fire({
8081
icon: "error",

frontend/src/app/(auth)/match/page.tsx

Lines changed: 76 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const SOCKET_URL =
7474

7575
const CURRENT_USER = getBaseUserData().username; // Username is unique
7676

77-
const TIMEOUT_TIMER = 500000; // in seconds
77+
const TIMEOUT_TIMER = 3; // in seconds
7878

7979
const FindPeer = () => {
8080
const stompClientRef = useRef<StompClient | null>(null);
@@ -91,6 +91,17 @@ const FindPeer = () => {
9191
};
9292

9393
const makeSocketConnection = (): Promise<void> => {
94+
const timeout = setTimeout(() => {
95+
const client = stompClientRef.current;
96+
client?.deactivate();
97+
Swal.fire({
98+
title: "Timeout",
99+
text: "We could not find a match for you, try a new set of filters? :(",
100+
icon: "error",
101+
showCloseButton: true,
102+
});
103+
}, TIMEOUT_TIMER * 1000);
104+
94105
return new Promise((resolve, reject) => {
95106
const socket = new SockJS(SOCKET_URL);
96107
const client = new StompClient({
@@ -104,19 +115,72 @@ const FindPeer = () => {
104115
setIsConnected(true);
105116

106117
client.subscribe("/user/queue/matches", (message) => {
107-
console.log("Received message: ", message.body);
108-
setSocketMessages((prevMessages) => [
109-
...prevMessages,
110-
message.body,
111-
]);
118+
try {
119+
console.log("Received message: ", message.body);
120+
setSocketMessages((prevMessages) => [
121+
...prevMessages,
122+
message.body,
123+
]);
124+
const response: FindMatchSocketMessageResponse = JSON.parse(
125+
message.body
126+
);
127+
const matchedUserEmail = response.matchedUserEmail;
128+
closeLoadingSpinner();
129+
clearTimeout(timeout);
130+
Swal.fire(
131+
"Match Found!",
132+
`We found a match for you! You have been matched with ${matchedUserEmail}.`,
133+
"success"
134+
);
135+
client.deactivate();
136+
} catch (error) {
137+
console.error(
138+
"Error subscribing to /user/queue/matches: ",
139+
error
140+
);
141+
closeLoadingSpinner();
142+
clearTimeout(timeout);
143+
144+
Swal.fire(
145+
"Error",
146+
"An error occurred while trying to find a match for you. Please try again later.",
147+
"error"
148+
);
149+
client.deactivate();
150+
}
112151
});
113152

114153
client.subscribe("/user/queue/requestRejection", (message) => {
115-
console.log("Received message: ", message.body);
116-
setSocketMessages((prevMessages) => [
117-
...prevMessages,
118-
message.body,
119-
]);
154+
try {
155+
console.log("Received message: ", message.body);
156+
setSocketMessages((prevMessages) => [
157+
...prevMessages,
158+
message.body,
159+
]);
160+
const response: string = message.body;
161+
closeLoadingSpinner();
162+
clearTimeout(timeout);
163+
Swal.fire(
164+
"A new Match Request cannot be sent!",
165+
`${response}`,
166+
"error"
167+
);
168+
client.deactivate();
169+
} catch (error) {
170+
console.error(
171+
"Error subscribing to /user/queue/requestRejection: ",
172+
error
173+
);
174+
closeLoadingSpinner();
175+
clearTimeout(timeout);
176+
177+
Swal.fire(
178+
"Error",
179+
"An error occurred while trying to find a match for you. Please try again later.",
180+
"error"
181+
);
182+
client.deactivate();
183+
}
120184
});
121185

122186
stompClientRef.current = client;
@@ -131,7 +195,7 @@ const FindPeer = () => {
131195
reject(new Error(error.headers.message));
132196
},
133197
});
134-
198+
stompClientRef.current = client;
135199
client.activate();
136200
});
137201
};
@@ -168,57 +232,6 @@ const FindPeer = () => {
168232
console.log("Match request sent: ", CURRENT_USER);
169233

170234
showLoadingSpinner(cancelSocketConnection);
171-
172-
const timeout = setTimeout(() => {
173-
stompClientRef.current?.deactivate();
174-
Swal.update({
175-
title: "Timeout",
176-
text: "We could not find a match for you. Perhaps try a new set of filters? :(",
177-
icon: "error",
178-
showCloseButton: true,
179-
});
180-
}, TIMEOUT_TIMER * 1000);
181-
182-
try {
183-
client.subscribe("/user/queue/requestRejection", (message) => {
184-
const response: string = message.body;
185-
console.log("Received message: ", response);
186-
closeLoadingSpinner();
187-
clearTimeout(timeout);
188-
Swal.fire(
189-
"A new Match Request cannot be sent!",
190-
`${response}`,
191-
"error"
192-
);
193-
client.deactivate();
194-
});
195-
196-
client.subscribe("/user/queue/matches", (message) => {
197-
const response: FindMatchSocketMessageResponse = JSON.parse(
198-
message.body
199-
);
200-
console.log("Received message: ", response);
201-
const matchedUserEmail = response.matchedUserEmail;
202-
closeLoadingSpinner();
203-
clearTimeout(timeout);
204-
Swal.fire(
205-
"Match Found!",
206-
`We found a match for you! You have been matched with ${matchedUserEmail}.`,
207-
"success"
208-
);
209-
client.deactivate();
210-
});
211-
} catch (error) {
212-
console.error("Error subscribing to /user/queue/matches: ", error);
213-
closeLoadingSpinner();
214-
clearTimeout(timeout);
215-
216-
Swal.fire(
217-
"Error",
218-
"An error occurred while trying to find a match for you. Please try again later.",
219-
"error"
220-
);
221-
}
222235
};
223236
const token = getToken();
224237

0 commit comments

Comments
 (0)