Comparison

mod_http_stats_stream/mod_http_stats_stream.lua @ 3635:fd054689a64c

mod_http_stats_stream: Use existing header preparation This allows the CORS support in mod_http to work.
author Kim Alvefur <zash@zash.se>
date Wed, 31 Jul 2019 18:55:06 +0200
parent 2432:47a6f01231b2
child 3643:740870196b97
comparison
equal deleted inserted replaced
3634:915e32d5a147 3635:fd054689a64c
1 local statsman = require "core.statsmanager"; 1 local statsman = require "core.statsmanager";
2 local http = require "net.http.server";
2 local json = require "util.json"; 3 local json = require "util.json";
3 4
4 local sessions = {}; 5 local sessions = {};
5 6
6 local function updates_client_closed(response) 7 local function updates_client_closed(response)
11 local function get_updates(event) 12 local function get_updates(event)
12 local request, response = event.request, event.response; 13 local request, response = event.request, event.response;
13 14
14 response.on_destroy = updates_client_closed; 15 response.on_destroy = updates_client_closed;
15 16
16 response.conn:write(table.concat({ 17 response.headers.content_type = "text/event-stream";
17 "HTTP/1.1 200 OK"; 18 response.headers.x_accel_buffering = "no"; -- for nginx maybe?
18 "Content-Type: text/event-stream"; 19 local resp = http.prepare_header(response);
19 "X-Accel-Buffering: no"; -- For nginx maybe? 20 table.insert(resp, "event: stats-full\r\n");
20 ""; 21 table.insert(resp, "data: ");
21 "event: stats-full"; 22 table.insert(resp, json.encode(statsman.get_stats()));
22 "data: "..json.encode(statsman.get_stats()); 23 table.insert(resp, "\r\n\r\n");
23 ""; 24 response.conn:write(table.concat(resp));
24 "";
25 }, "\r\n"));
26 25
27 sessions[response] = request; 26 sessions[response] = request;
28 return true; 27 return true;
29 end 28 end
30 29