Bladeren bron

Add co_connect coroutine for auto-reconnect

- co_connect(): attempts connection if ws.connected=false
- Exponential backoff: 30s → 60s → 120s → 240s → max 5min
- Resets on successful reconnect
- Runs alongside dhcp/wan/commands coroutines
- Better log messages
Luis Rosales 2 maanden geleden
bovenliggende
commit
568a4b8fcc
1 gewijzigde bestanden met toevoegingen van 30 en 2 verwijderingen
  1. 30 2
      package/src/client2server-unified.lua

+ 30 - 2
package/src/client2server-unified.lua

@@ -374,6 +374,33 @@ local function co_commands()
     end
 end
 
+-- COROUTINE: Auto-reconnect (keeps connection alive)
+local function co_connect()
+    local retry_delay = 30  -- Start at 30s
+    local max_delay = 300    -- Max 5 minutes
+    
+    while true do
+        if not ws.connected then
+            log_info("Connecting to " .. cfg.server_url .. "...")
+            
+            local sock = ws.connect(cfg.server_url)
+            
+            if sock then
+                ws.connected = true
+                retry_delay = 30  -- Reset on success
+                log_info("Connected!")
+                buffer.flush(function(d) return ws.send(d) end)
+            else
+                log_err("Connection failed, retry in " .. retry_delay .. "s")
+                coroutine.yield(retry_delay)
+                retry_delay = math.min(retry_delay * 2, max_delay)
+            end
+        else
+            coroutine.yield(30)  -- Still connected, check every 30s
+        end
+    end
+end
+
 -- ============================================================================
 -- MAIN
 -- ============================================================================
@@ -382,7 +409,8 @@ local function scheduler()
     local cos = {
         co_dhcp = coroutine.create(co_dhcp),
         co_wan = coroutine.create(co_wan),
-        co_commands = coroutine.create(co_commands),  -- 🔥 Commands run concurrently!
+        co_commands = coroutine.create(co_commands),
+        co_connect = coroutine.create(co_connect),  -- Auto-reconnect!
     }
     
     while true do
@@ -415,7 +443,7 @@ local function main()
     if pf then pf:write(tostring(os.getpid())); pf:close() end
     
     -- Connect
-    log_info("Connecting...")
+    log_info("Initial connection...")
     local sock = ws.connect(cfg.server_url)
     
     if sock then