diff --git a/logic.js b/logic.js index 2d4e0ba..cb0abcc 100644 --- a/logic.js +++ b/logic.js @@ -94,7 +94,6 @@ var build_absolute_url = function build_absolute_url(req, url) { }; exports.options_eventsource = function options_eventsource(req, res) { - res.header('Connection', 'keep-alive'); var origin = req.header('Origin'); if (origin != null) { res.header('Access-Control-Allow-Origin', origin); @@ -102,17 +101,20 @@ exports.options_eventsource = function options_eventsource(req, res) { res.header('Access-Control-Allow-Headers', 'X-Requested-With'); res.header('Access-Control-Expose-Headers', 'Location'); } + res.header('Connection', 'keep-alive'); + res.header('Content-Length', '0'); res.sendStatus(204); }; exports.options_eventsource_entry = function options_eventsource_entry(req, res) { - res.header('Connection', 'keep-alive'); var origin = req.header('Origin'); if (origin != null) { res.header('Access-Control-Allow-Origin', origin); res.header('Access-Control-Allow-Methods', 'GET, DELETE'); res.header('Access-Control-Allow-Headers', 'X-Requested-With'); } + res.header('Connection', 'keep-alive'); + res.header('Content-Length', '0'); res.sendStatus(204); }; @@ -193,6 +195,8 @@ exports.eventsource = function eventsource(req, res) { res.header('Cache-Control', 'no-cache'); res.header('Connection', 'keep-alive'); + // Forbid Nginx buffering + res.header('X-Accel-Buffering', 'no'); req.socket.setTimeout(0); if (connection.response != null) { @@ -220,6 +224,8 @@ exports.eventsource = function eventsource(req, res) { url: build_absolute_url(req, '/eventsource/' + connection.id) }).toString('utf8') + '\n\n'); + // Force sending init event + res.flush(); res.on('close', connection.close_listener); }; @@ -253,12 +259,11 @@ exports.delete_eventsource = function delete_eventsource(req, res) { delete callbacks[callback_id]; } + res.header('Content-Length', '0'); res.sendStatus(204); }; exports.options_callbacks = function options_callbacks(req, res) { - res.header('Cache-Control', 'no-cache'); - res.header('Connection', 'keep-alive'); var origin = req.header('Origin'); if (origin != null) { res.header('Access-Control-Allow-Origin', origin); @@ -266,6 +271,9 @@ exports.options_callbacks = function options_callbacks(req, res) { res.header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With'); res.header('Access-Control-Expose-Headers', 'Location'); } + res.header('Cache-Control', 'no-cache'); + res.header('Connection', 'keep-alive'); + res.header('Content-Length', '0'); res.sendStatus(204); }; @@ -274,6 +282,7 @@ exports.create_callback = function create_callback(req, res) { res.header('Cache-Control', 'no-cache'); res.header('Connection', 'keep-alive'); + res.header('Content-Length', '0'); origin = req.header('Origin'); if (origin != null) { res.header('Access-Control-Allow-Origin', origin); @@ -341,24 +350,28 @@ exports.process_callback = function process_callback(req, res) { }).toString('utf8'); eventsource.write('event: notification\n'); eventsource.write('data: ' + data + '\n\n'); + // Send this event + eventsource.flush(); } else { console.log('Ignoring notification as the client is not connected'); } + res.header('Content-Length', '0'); res.sendStatus(204); callbacks[req.params.id].notification_counter++; }); }; exports.options_callback_entry = function options_callback_entry(req, res) { - res.header('Cache-Control', 'no-cache'); - res.header('Connection', 'keep-alive'); var origin = req.header('Origin'); if (origin != null) { res.header('Access-Control-Allow-Origin', origin); res.header('Access-Control-Allow-Methods', 'DELETE, OPTIONS, POST'); res.header('Access-Control-Allow-Headers', 'X-Requested-With'); } + res.header('Cache-Control', 'no-cache'); + res.header('Connection', 'keep-alive'); + res.header('Content-Length', '0'); res.sendStatus(204); }; @@ -377,5 +390,8 @@ exports.delete_callback = function delete_callback(req, res) { } removeCallback(req.params.id); + res.header('Cache-Control', 'no-cache'); + res.header('Connection', 'keep-alive'); + res.header('Content-Length', '0'); res.sendStatus(204); };