|
@@ -19,14 +19,22 @@
|
|
|
-- Try to load luv (libuv bindings for async operations)
|
|
-- Try to load luv (libuv bindings for async operations)
|
|
|
local luv_ok, luv = pcall(require, "luv")
|
|
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 ws_client = nil
|
|
|
local has_websocket, websocket = pcall(require, "websocket")
|
|
local has_websocket, websocket = pcall(require, "websocket")
|
|
|
|
|
|
|
|
|
|
+
|
|
|
if has_websocket then
|
|
if has_websocket then
|
|
|
ws_client = websocket.client.sync()
|
|
ws_client = websocket.client.sync()
|
|
|
end
|
|
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
|
|
-- CONFIG
|
|
|
-- ============================================================================
|
|
-- ============================================================================
|
|
@@ -591,14 +599,34 @@ local function main()
|
|
|
-- Idle work - check WebSocket periodically
|
|
-- Idle work - check WebSocket periodically
|
|
|
end)
|
|
end)
|
|
|
|
|
|
|
|
- -- WebSocket connection - try to connect and stay connected
|
|
|
|
|
|
|
+ -- WebSocket/HTTP connection - try to connect and stay connected
|
|
|
local function ws_loop()
|
|
local function ws_loop()
|
|
|
log("info", "Connecting to server...")
|
|
log("info", "Connecting to server...")
|
|
|
|
|
|
|
|
|
|
+ -- Try WebSocket first
|
|
|
if ws_client then
|
|
if ws_client then
|
|
|
sock = ws.connect(cfg.url)
|
|
sock = ws.connect(cfg.url)
|
|
|
end
|
|
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
|
|
if sock then
|
|
|
log("info", "Connected!")
|
|
log("info", "Connected!")
|
|
|
retries = 0
|
|
retries = 0
|