clients.lua: Add /watcher_info endpoint to get a more detailed list of streams

Mon, 04 Jan 2016 16:23:57 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 04 Jan 2016 16:23:57 +0000
changeset 15
67858d731518
parent 14
1d28dfcd9c94
child 16
d35376a53644

clients.lua: Add /watcher_info endpoint to get a more detailed list of streams

clients.lua file | annotate | diff | comparison | revisions
--- a/clients.lua	Mon Jan 04 14:52:06 2016 +0000
+++ b/clients.lua	Mon Jan 04 16:23:57 2016 +0000
@@ -82,6 +82,8 @@
 
 	update_have_clients();
 
+	events.fire_event("new-client", { conn = conn, cookie = cookie });
+
 	if last_chunk then
 		conn:write(last_chunk);
 	end
@@ -123,6 +125,21 @@
 	return tostring(total);
 end
 
+function handle_watcher_info(event)
+	local watchers = {};
+	for client in pairs(clients) do
+		local bytes_rx, bytes_tx, age_sec = client:socket():getstats();
+		table.insert(watchers, json.encode{
+			location = client.location or "(Unknown location)";
+			active = not not active_clients[client];
+			age = math.floor(age_sec);
+			sent = math.floor(bytes_tx/1024);
+		});
+	end
+	mark_active(event.request);
+	return "[\n  "..table.concat(watchers, ",\n  ").."\n]";
+end
+
 -- Called when a HTTP stream client closes
 local function client_closed(request)
 	local stream = request._watching;
@@ -183,6 +200,7 @@
 http_server.add_handler("GET localhost/*", handle_request);
 http_server.add_handler("GET localhost/active", handle_active);
 http_server.add_handler("GET localhost/watchers", handle_watchers);
+http_server.add_handler("GET localhost/watcher_info", handle_watcher_info);
 http_server.add_handler("GET localhost/notifications", handle_notifications);
 http_server.add_handler("GET localhost/push/*", handle_push);
 http_server.listen_on(8006);

mercurial