Explorar o código

Add HTTP fallback when WebSocket not available

Try WebSocket first, fall back to HTTP POST if not available.
Gogs hai 2 meses
pai
achega
720e771ffd
Modificáronse 1 ficheiros con 30 adicións e 2 borrados
  1. 30 2
      usr/sbin/client2server-luv.lua

+ 30 - 2
usr/sbin/client2server-luv.lua

@@ -19,14 +19,22 @@
 -- Try to load luv (libuv bindings for async operations)
 local luv_ok, luv = pcall(require, "luv")
 
--- Try to load websocket library, fallback to simple HTTP
+-- Try to load websocket library
 local ws_client = nil
 local has_websocket, websocket = pcall(require, "websocket")
 
+
 if has_websocket then
     ws_client = websocket.client.sync()
 end
 
+-- Try to load HTTP library as fallback
+local http_client = nil
+local has_http, http = pcall(require, "ssl/https") or pcall(require, "http")
+if has_http then
+    http_client = http
+end
+
 -- ============================================================================
 -- CONFIG
 -- ============================================================================
@@ -591,14 +599,34 @@ local function main()
                 -- Idle work - check WebSocket periodically
             end)
             
-            -- WebSocket connection - try to connect and stay connected
+            -- WebSocket/HTTP connection - try to connect and stay connected
             local function ws_loop()
                 log("info", "Connecting to server...")
                 
+                -- Try WebSocket first
                 if ws_client then
                     sock = ws.connect(cfg.url)
                 end
                 
+                -- Fallback to HTTP POST if no WebSocket
+                if not sock and http_client then
+                    -- Send events via HTTP POST
+                    local url = cfg.url:gsub("wss", "https"):gsub("ws", "http")
+                    if url == cfg.url then url = cfg.url .. "/events" end
+                    
+                    -- POST buffered events
+                    for _, event in ipairs(buffer.events) do
+                        local ok = http_client.request(url, "POST", event, {
+                            ["Content-Type"] = "application/json"
+                        })
+                        if ok then
+                            log("info", "Event sent: " .. event)
+                        end
+                    end
+                    buffer.clear()
+                    sock = "http"  -- Mark as connected
+                end
+                
                 if sock then
                     log("info", "Connected!")
                     retries = 0