util.adminstream: Expose session events (specifically "disconnected")

Currently there is no way to get this info and perform cleanup tasks.
This commit is contained in:
Matthew Wild 2025-01-07 18:07:45 +00:00
parent d58c6ae7ca
commit 7a281ab905

View file

@ -185,9 +185,12 @@ local function new_connection(socket_path, listeners)
end end
local function new_server(sessions, stanza_handler) local function new_server(sessions, stanza_handler)
local listeners = {}; local s = {
events = events.new();
listeners = {};
};
function listeners.onconnect(conn) function s.listeners.onconnect(conn)
log("debug", "New connection"); log("debug", "New connection");
local session = sessionlib.new("admin"); local session = sessionlib.new("admin");
sessionlib.set_id(session); sessionlib.set_id(session);
@ -241,29 +244,28 @@ local function new_server(sessions, stanza_handler)
sessions[conn] = session; sessions[conn] = session;
end end
function listeners.onincoming(conn, data) function s.listeners.onincoming(conn, data)
local session = sessions[conn]; local session = sessions[conn];
if session then if session then
session.data(data); session.data(data);
end end
end end
function listeners.ondisconnect(conn, err) function s.listeners.ondisconnect(conn, err)
local session = sessions[conn]; local session = sessions[conn];
if session then if session then
session.log("info", "Admin client disconnected: %s", err or "connection closed"); session.log("info", "Admin client disconnected: %s", err or "connection closed");
session.conn = nil; session.conn = nil;
sessions[conn] = nil; sessions[conn] = nil;
s.events.fire_event("disconnected", { session = session });
end end
end end
function listeners.onreadtimeout(conn) function s.listeners.onreadtimeout(conn)
return conn:send(" "); return conn:send(" ");
end end
return { return s;
listeners = listeners;
};
end end
local function new_client() local function new_client()