clients.lua

changeset 15
67858d731518
parent 14
1d28dfcd9c94
child 17
dd2a570367a9
equal deleted inserted replaced
14:1d28dfcd9c94 15:67858d731518
80 active_clients[conn] = os.time(); 80 active_clients[conn] = os.time();
81 client_by_cookie[cookie] = conn; 81 client_by_cookie[cookie] = conn;
82 82
83 update_have_clients(); 83 update_have_clients();
84 84
85 events.fire_event("new-client", { conn = conn, cookie = cookie });
86
85 if last_chunk then 87 if last_chunk then
86 conn:write(last_chunk); 88 conn:write(last_chunk);
87 end 89 end
88 90
89 conn:setlistener(listener); 91 conn:setlistener(listener);
119 active = active + 1; 121 active = active + 1;
120 end 122 end
121 end 123 end
122 mark_active(event.request); 124 mark_active(event.request);
123 return tostring(total); 125 return tostring(total);
126 end
127
128 function handle_watcher_info(event)
129 local watchers = {};
130 for client in pairs(clients) do
131 local bytes_rx, bytes_tx, age_sec = client:socket():getstats();
132 table.insert(watchers, json.encode{
133 location = client.location or "(Unknown location)";
134 active = not not active_clients[client];
135 age = math.floor(age_sec);
136 sent = math.floor(bytes_tx/1024);
137 });
138 end
139 mark_active(event.request);
140 return "[\n "..table.concat(watchers, ",\n ").."\n]";
124 end 141 end
125 142
126 -- Called when a HTTP stream client closes 143 -- Called when a HTTP stream client closes
127 local function client_closed(request) 144 local function client_closed(request)
128 local stream = request._watching; 145 local stream = request._watching;
181 http_server.add_host("localhost"); 198 http_server.add_host("localhost");
182 http_server.set_default_host("localhost"); 199 http_server.set_default_host("localhost");
183 http_server.add_handler("GET localhost/*", handle_request); 200 http_server.add_handler("GET localhost/*", handle_request);
184 http_server.add_handler("GET localhost/active", handle_active); 201 http_server.add_handler("GET localhost/active", handle_active);
185 http_server.add_handler("GET localhost/watchers", handle_watchers); 202 http_server.add_handler("GET localhost/watchers", handle_watchers);
203 http_server.add_handler("GET localhost/watcher_info", handle_watcher_info);
186 http_server.add_handler("GET localhost/notifications", handle_notifications); 204 http_server.add_handler("GET localhost/notifications", handle_notifications);
187 http_server.add_handler("GET localhost/push/*", handle_push); 205 http_server.add_handler("GET localhost/push/*", handle_push);
188 http_server.listen_on(8006); 206 http_server.listen_on(8006);

mercurial