@@ -30,8 +30,8 @@ const spotifySkippedSubscribers = new Set();
3030let wsReconnectAttempts = 0 ;
3131let wsReconnectTimer = null ;
3232let wsReconnectInProgress = false ;
33- const WS_MAX_RECONNECT_ATTEMPTS = 30 ;
34- const WS_RECONNECT_INTERVAL = 2000 ; // 2 seconds
33+ const WS_RECONNECT_BASE_INTERVAL = 1000 ;
34+ const WS_RECONNECT_MAX_INTERVAL = 30000 ;
3535
3636let isInitializingDiscovery = false ;
3737let isStoppingDiscovery = false ;
@@ -321,24 +321,26 @@ const processConnectQueue = async () => {
321321} ;
322322
323323const attemptWsReconnection = ( ) => {
324- if (
325- wsReconnectInProgress ||
326- wsReconnectAttempts >= WS_MAX_RECONNECT_ATTEMPTS
327- ) {
324+ if ( wsReconnectInProgress ) {
328325 return ;
329326 }
330327
331328 wsReconnectInProgress = true ;
332329 wsReconnectAttempts ++ ;
333330
331+ const delay = Math . min (
332+ WS_RECONNECT_BASE_INTERVAL * Math . pow ( 2 , wsReconnectAttempts - 1 ) ,
333+ WS_RECONNECT_MAX_INTERVAL ,
334+ ) ;
335+
334336 console . log (
335- `WebSocket reconnection attempt ${ wsReconnectAttempts } / ${ WS_MAX_RECONNECT_ATTEMPTS } ` ,
337+ `WebSocket reconnection attempt ${ wsReconnectAttempts } (next in ${ delay } ms) ` ,
336338 ) ;
337339
338340 wsReconnectTimer = setTimeout ( ( ) => {
339341 wsReconnectInProgress = false ;
340342 setupGlobalWebSocket ( ) ;
341- } , WS_RECONNECT_INTERVAL ) ;
343+ } , delay ) ;
342344} ;
343345
344346const setupGlobalWebSocket = async ( ) => {
@@ -1243,8 +1245,8 @@ export const useBluetooth = () => {
12431245 const retryDeviceAddressRef = useRef ( null ) ;
12441246 const reconnectInitTriggeredRef = useRef ( false ) ;
12451247
1246- const MAX_RECONNECT_ATTEMPTS = 15 ;
1247- const RECONNECT_INTERVAL = 5000 ;
1248+ const RECONNECT_BASE_INTERVAL = 2000 ;
1249+ const RECONNECT_MAX_INTERVAL = 60000 ;
12481250 const INITIAL_RECONNECT_DELAY = 1000 ;
12491251
12501252 useEffect ( ( ) => {
@@ -1284,20 +1286,6 @@ export const useBluetooth = () => {
12841286 return ;
12851287 }
12861288
1287- if (
1288- ! continuous &&
1289- reconnectAttemptsRef . current >= MAX_RECONNECT_ATTEMPTS
1290- ) {
1291- cleanupReconnectTimer ( ) ;
1292- reconnectAttemptsRef . current = 0 ;
1293- setReconnectAttempt ( 0 ) ;
1294- isReconnecting . current = false ;
1295- reconnectionExhausted = true ;
1296- window . dispatchEvent ( new Event ( "networkBannerHide" ) ) ;
1297- window . dispatchEvent ( new Event ( "networkScreenShow" ) ) ;
1298- return ;
1299- }
1300-
13011289 try {
13021290 isReconnecting . current = true ;
13031291 window . dispatchEvent ( new Event ( "networkBannerShow" ) ) ;
@@ -1318,6 +1306,7 @@ export const useBluetooth = () => {
13181306 isReconnecting . current = false ;
13191307 reconnectionExhausted = false ;
13201308 window . dispatchEvent ( new Event ( "networkBannerHide" ) ) ;
1309+ window . dispatchEvent ( new Event ( "networkScreenHide" ) ) ;
13211310 retryIsCancelled = true ;
13221311 return ;
13231312 }
@@ -1326,6 +1315,11 @@ export const useBluetooth = () => {
13261315 reconnectAttemptsRef . current ++ ;
13271316 setReconnectAttempt ( reconnectAttemptsRef . current ) ;
13281317
1318+ if ( reconnectAttemptsRef . current >= 10 ) {
1319+ reconnectionExhausted = true ;
1320+ window . dispatchEvent ( new Event ( "networkScreenShow" ) ) ;
1321+ }
1322+
13291323 const response = await queueConnectRequest ( lastDeviceAddress ) ;
13301324
13311325 if ( response . ok ) {
@@ -1338,61 +1332,39 @@ export const useBluetooth = () => {
13381332 reconnectionExhausted = false ;
13391333 retryIsCancelled = true ;
13401334 window . dispatchEvent ( new Event ( "networkBannerHide" ) ) ;
1335+ window . dispatchEvent ( new Event ( "networkScreenHide" ) ) ;
13411336 return ;
13421337 }
13431338 }
13441339
1345- if (
1346- continuous ||
1347- reconnectAttemptsRef . current < MAX_RECONNECT_ATTEMPTS
1348- ) {
1349- const delayTime = continuous
1350- ? Math . max ( RECONNECT_INTERVAL , 5000 )
1351- : RECONNECT_INTERVAL ;
1352- reconnectTimeoutRef . current = setTimeout ( ( ) => {
1353- reconnectTimeoutRef . current = null ;
1354- isReconnecting . current = false ;
1355- if ( continuous ) {
1356- attemptReconnect ( true ) ;
1357- } else {
1358- attemptReconnect ( ) ;
1359- }
1360- } , delayTime ) ;
1361- } else {
1362- cleanupReconnectTimer ( ) ;
1340+ const delayTime = Math . min (
1341+ RECONNECT_BASE_INTERVAL * Math . pow ( 2 , reconnectAttemptsRef . current - 1 ) ,
1342+ RECONNECT_MAX_INTERVAL ,
1343+ ) ;
1344+ reconnectTimeoutRef . current = setTimeout ( ( ) => {
1345+ reconnectTimeoutRef . current = null ;
13631346 isReconnecting . current = false ;
1364- reconnectionExhausted = true ;
1365- window . dispatchEvent ( new Event ( "networkBannerHide" ) ) ;
1366- window . dispatchEvent ( new Event ( "networkScreenShow" ) ) ;
1367- }
1347+ attemptReconnect ( continuous ) ;
1348+ } , delayTime ) ;
13681349 } catch ( error ) {
13691350 console . error ( "Reconnect attempt failed:" , error ) ;
13701351 reconnectAttemptsRef . current ++ ;
13711352 setReconnectAttempt ( reconnectAttemptsRef . current ) ;
13721353
1373- if (
1374- continuous ||
1375- reconnectAttemptsRef . current < MAX_RECONNECT_ATTEMPTS
1376- ) {
1377- const delayTime = continuous
1378- ? Math . max ( RECONNECT_INTERVAL , 5000 )
1379- : RECONNECT_INTERVAL ;
1380- reconnectTimeoutRef . current = setTimeout ( ( ) => {
1381- reconnectTimeoutRef . current = null ;
1382- isReconnecting . current = false ;
1383- if ( continuous ) {
1384- attemptReconnect ( true ) ;
1385- } else {
1386- attemptReconnect ( ) ;
1387- }
1388- } , delayTime ) ;
1389- } else {
1390- cleanupReconnectTimer ( ) ;
1391- isReconnecting . current = false ;
1354+ if ( reconnectAttemptsRef . current >= 10 ) {
13921355 reconnectionExhausted = true ;
1393- window . dispatchEvent ( new Event ( "networkBannerHide" ) ) ;
13941356 window . dispatchEvent ( new Event ( "networkScreenShow" ) ) ;
13951357 }
1358+
1359+ const delayTime = Math . min (
1360+ RECONNECT_BASE_INTERVAL * Math . pow ( 2 , reconnectAttemptsRef . current - 1 ) ,
1361+ RECONNECT_MAX_INTERVAL ,
1362+ ) ;
1363+ reconnectTimeoutRef . current = setTimeout ( ( ) => {
1364+ reconnectTimeoutRef . current = null ;
1365+ isReconnecting . current = false ;
1366+ attemptReconnect ( continuous ) ;
1367+ } , delayTime ) ;
13961368 }
13971369 } ,
13981370 [ cleanupReconnectTimer ] ,
0 commit comments