@@ -281,23 +281,23 @@ function connectWebSocket() {
281
281
console . log ( 'WebSocket URL:' , wsUrl ) ;
282
282
283
283
// Create WebSocket connection
284
- var ws = new WebSocket ( wsUrl ) ;
284
+ var newWs = new WebSocket ( wsUrl ) ;
285
285
286
286
// Connection opened
287
- ws . onopen = function ( ) {
287
+ newWs . onopen = function ( ) {
288
288
console . log ( 'WebSocket connected successfully' ) ;
289
289
wsReconnectAttempts = 0 ; // Reset reconnect attempts on successful connection
290
290
291
291
// Send a ping to verify connection
292
292
try {
293
- ws . send ( JSON . stringify ( { type : 'ping' } ) ) ;
293
+ newWs . send ( JSON . stringify ( { type : 'ping' } ) ) ;
294
294
} catch ( e ) {
295
295
console . error ( 'Error sending ping:' , e ) ;
296
296
}
297
297
} ;
298
298
299
299
// Listen for messages
300
- ws . onmessage = function ( event ) {
300
+ newWs . onmessage = function ( event ) {
301
301
try {
302
302
if ( ! event ) {
303
303
console . warn ( 'Received invalid WebSocket event' ) ;
@@ -318,12 +318,12 @@ function connectWebSocket() {
318
318
} ;
319
319
320
320
// Handle errors
321
- ws . onerror = function ( error ) {
321
+ newWs . onerror = function ( error ) {
322
322
console . error ( 'WebSocket error occurred:' , error ) ;
323
323
} ;
324
324
325
325
// Connection closed
326
- ws . onclose = function ( event ) {
326
+ newWs . onclose = function ( event ) {
327
327
console . log ( 'WebSocket disconnected, code:' , event . code , 'reason:' , event . reason || 'No reason provided' ) ;
328
328
329
329
// Try to reconnect with exponential backoff
@@ -333,10 +333,8 @@ function connectWebSocket() {
333
333
console . log ( 'Attempting to reconnect in ' + delay + 'ms (attempt ' + wsReconnectAttempts + '/' + maxReconnectAttempts + ')' ) ;
334
334
335
335
setTimeout ( function ( ) {
336
- var newWs = connectWebSocket ( ) ;
337
- if ( newWs ) {
338
- // We can't update the global ws variable from here
339
- // Instead, we'll rely on the polling fallback
336
+ ws = connectWebSocket ( ) ;
337
+ if ( ws ) {
340
338
console . log ( 'New WebSocket connection established' ) ;
341
339
}
342
340
} , delay ) ;
@@ -345,7 +343,7 @@ function connectWebSocket() {
345
343
}
346
344
} ;
347
345
348
- return ws ;
346
+ return newWs ;
349
347
} catch ( error ) {
350
348
console . error ( 'Failed to create WebSocket connection:' , error ) ;
351
349
return null ;
@@ -454,4 +452,4 @@ setTimeout(function() {
454
452
loadingIndicator . style . display = 'none' ;
455
453
} , 5000 ) ;
456
454
}
457
- } , 20000 ) ;
455
+ } , 20000 ) ;
0 commit comments