@@ -281,23 +281,23 @@ function connectWebSocket() {
281281 console . log ( 'WebSocket URL:' , wsUrl ) ;
282282
283283 // Create WebSocket connection
284- var ws = new WebSocket ( wsUrl ) ;
284+ var newWs = new WebSocket ( wsUrl ) ;
285285
286286 // Connection opened
287- ws . onopen = function ( ) {
287+ newWs . onopen = function ( ) {
288288 console . log ( 'WebSocket connected successfully' ) ;
289289 wsReconnectAttempts = 0 ; // Reset reconnect attempts on successful connection
290290
291291 // Send a ping to verify connection
292292 try {
293- ws . send ( JSON . stringify ( { type : 'ping' } ) ) ;
293+ newWs . send ( JSON . stringify ( { type : 'ping' } ) ) ;
294294 } catch ( e ) {
295295 console . error ( 'Error sending ping:' , e ) ;
296296 }
297297 } ;
298298
299299 // Listen for messages
300- ws . onmessage = function ( event ) {
300+ newWs . onmessage = function ( event ) {
301301 try {
302302 if ( ! event ) {
303303 console . warn ( 'Received invalid WebSocket event' ) ;
@@ -318,12 +318,12 @@ function connectWebSocket() {
318318 } ;
319319
320320 // Handle errors
321- ws . onerror = function ( error ) {
321+ newWs . onerror = function ( error ) {
322322 console . error ( 'WebSocket error occurred:' , error ) ;
323323 } ;
324324
325325 // Connection closed
326- ws . onclose = function ( event ) {
326+ newWs . onclose = function ( event ) {
327327 console . log ( 'WebSocket disconnected, code:' , event . code , 'reason:' , event . reason || 'No reason provided' ) ;
328328
329329 // Try to reconnect with exponential backoff
@@ -333,10 +333,8 @@ function connectWebSocket() {
333333 console . log ( 'Attempting to reconnect in ' + delay + 'ms (attempt ' + wsReconnectAttempts + '/' + maxReconnectAttempts + ')' ) ;
334334
335335 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 ) {
340338 console . log ( 'New WebSocket connection established' ) ;
341339 }
342340 } , delay ) ;
@@ -345,7 +343,7 @@ function connectWebSocket() {
345343 }
346344 } ;
347345
348- return ws ;
346+ return newWs ;
349347 } catch ( error ) {
350348 console . error ( 'Failed to create WebSocket connection:' , error ) ;
351349 return null ;
@@ -454,4 +452,4 @@ setTimeout(function() {
454452 loadingIndicator . style . display = 'none' ;
455453 } , 5000 ) ;
456454 }
457- } , 20000 ) ;
455+ } , 20000 ) ;
0 commit comments