|
|
@@ -14,6 +14,7 @@ local cfg = {
|
|
|
enabled = true,
|
|
|
check_interval = 5,
|
|
|
log_level = "info",
|
|
|
+ debug = false,
|
|
|
buffer_file = "/tmp/event_buffer",
|
|
|
max_buffer = 100,
|
|
|
pid_file = "/var/run/client2server.pid",
|
|
|
@@ -46,6 +47,7 @@ pcall(function()
|
|
|
cfg.enabled = uci:get("client2server", "general", "enabled") or cfg.enabled
|
|
|
cfg.check_interval = tonumber(uci:get("client2server", "general", "check_interval")) or cfg.check_interval
|
|
|
cfg.log_level = uci:get("client2server", "general", "log_level") or cfg.log_level
|
|
|
+ cfg.debug = uci:get("client2server", "general", "debug") == "1"
|
|
|
cfg.buffer_file = uci:get("client2server", "general", "buffer_file") or cfg.buffer_file
|
|
|
cfg.max_buffer = tonumber(uci:get("client2server", "general", "max_buffer")) or cfg.max_buffer
|
|
|
-- Server
|
|
|
@@ -93,6 +95,23 @@ end
|
|
|
|
|
|
local function log_info(msg) log("info", msg) end
|
|
|
local function log_err(msg) log("err", msg) end
|
|
|
+local function log_debug(msg)
|
|
|
+ if cfg.debug then log("debug", msg) end
|
|
|
+end
|
|
|
+local function log_send(event_type, data)
|
|
|
+ if cfg.debug then
|
|
|
+ local s = data:gsub("\"", "\\"")
|
|
|
+ if #s > 200 then s = s:sub(1, 200) .. "..." end
|
|
|
+ log("debug", "SEND " .. event_type .. ": " .. s)
|
|
|
+ end
|
|
|
+end
|
|
|
+local function log_recv(data)
|
|
|
+ if cfg.debug then
|
|
|
+ local s = data:gsub("\"", "\\"")
|
|
|
+ if #s > 200 then s = s:sub(1, 200) .. "..." end
|
|
|
+ log("debug", "RECV: " .. s)
|
|
|
+ end
|
|
|
+end
|
|
|
|
|
|
-- ============================================================================
|
|
|
-- JSON
|
|
|
@@ -282,6 +301,10 @@ function ws.connect(url)
|
|
|
end
|
|
|
|
|
|
function ws.send(data)
|
|
|
+ if cfg.debug then
|
|
|
+ local s = #data > 150 and data:sub(1,150).."..." or data
|
|
|
+ log("debug", "SEND: " .. s)
|
|
|
+ end
|
|
|
if not ws.connected then buffer.add(data); return false end
|
|
|
|
|
|
-- WebSocket frame: FIN(1) + opcode(1) = 0x81 (text)
|
|
|
@@ -300,6 +323,7 @@ function ws.recv()
|
|
|
if not ws.connected then return nil end
|
|
|
|
|
|
ws.sock:settimeout(0.5)
|
|
|
+ if cfg.debug then log("debug", "Waiting for data...") end
|
|
|
local data, err = ws.sock:receive("*l")
|
|
|
ws.sock:settimeout(10)
|
|
|
|
|
|
@@ -313,6 +337,11 @@ function ws.recv()
|
|
|
data = data:sub(2)
|
|
|
end
|
|
|
|
|
|
+ if cfg.debug and data then
|
|
|
+ local s = #data > 150 and data:sub(1,150).."..." or data
|
|
|
+ log("debug", "RECV: " .. s)
|
|
|
+ end
|
|
|
+
|
|
|
return data
|
|
|
end
|
|
|
|