|
@@ -374,6 +374,33 @@ local function co_commands()
|
|
|
end
|
|
end
|
|
|
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
|
|
-- MAIN
|
|
|
-- ============================================================================
|
|
-- ============================================================================
|
|
@@ -382,7 +409,8 @@ local function scheduler()
|
|
|
local cos = {
|
|
local cos = {
|
|
|
co_dhcp = coroutine.create(co_dhcp),
|
|
co_dhcp = coroutine.create(co_dhcp),
|
|
|
co_wan = coroutine.create(co_wan),
|
|
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
|
|
while true do
|
|
@@ -415,7 +443,7 @@ local function main()
|
|
|
if pf then pf:write(tostring(os.getpid())); pf:close() end
|
|
if pf then pf:write(tostring(os.getpid())); pf:close() end
|
|
|
|
|
|
|
|
-- Connect
|
|
-- Connect
|
|
|
- log_info("Connecting...")
|
|
|
|
|
|
|
+ log_info("Initial connection...")
|
|
|
local sock = ws.connect(cfg.server_url)
|
|
local sock = ws.connect(cfg.server_url)
|
|
|
|
|
|
|
|
if sock then
|
|
if sock then
|