@@ -74,7 +74,7 @@ const SOCKET_URL =
74
74
75
75
const CURRENT_USER = getBaseUserData ( ) . username ; // Username is unique
76
76
77
- const TIMEOUT_TIMER = 500000 ; // in seconds
77
+ const TIMEOUT_TIMER = 3 ; // in seconds
78
78
79
79
const FindPeer = ( ) => {
80
80
const stompClientRef = useRef < StompClient | null > ( null ) ;
@@ -91,6 +91,17 @@ const FindPeer = () => {
91
91
} ;
92
92
93
93
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
+
94
105
return new Promise ( ( resolve , reject ) => {
95
106
const socket = new SockJS ( SOCKET_URL ) ;
96
107
const client = new StompClient ( {
@@ -104,19 +115,72 @@ const FindPeer = () => {
104
115
setIsConnected ( true ) ;
105
116
106
117
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
+ }
112
151
} ) ;
113
152
114
153
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
+ }
120
184
} ) ;
121
185
122
186
stompClientRef . current = client ;
@@ -131,7 +195,7 @@ const FindPeer = () => {
131
195
reject ( new Error ( error . headers . message ) ) ;
132
196
} ,
133
197
} ) ;
134
-
198
+ stompClientRef . current = client ;
135
199
client . activate ( ) ;
136
200
} ) ;
137
201
} ;
@@ -168,57 +232,6 @@ const FindPeer = () => {
168
232
console . log ( "Match request sent: " , CURRENT_USER ) ;
169
233
170
234
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
- }
222
235
} ;
223
236
const token = getToken ( ) ;
224
237
0 commit comments