X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=www%2Fserve-analysis.js;h=4f0d2f010376df9748333a58a985c0fbbf1bcd21;hb=0d83fa679dc2bb17d40e34779c60abbd511275ef;hp=5778dc921c99ced8bb8c82a64250f9e4d513ca5e;hpb=405e4d1cabe8e21b8bc6291f4258da35d51f5958;p=remoteglot diff --git a/www/serve-analysis.js b/www/serve-analysis.js index 5778dc9..4f0d2f0 100644 --- a/www/serve-analysis.js +++ b/www/serve-analysis.js @@ -54,10 +54,13 @@ var reread_file = function(event, filename) { }); } var possibly_wakeup_clients = function() { + var num_viewers = count_viewers(); for (var i in sleeping_clients) { clearTimeout(sleeping_clients[i].timer); - mark_recently_seen(sleeping_clients[request_id].unique); - send_json(sleeping_clients[i].response, sleeping_clients[i].accept_gzip); + mark_recently_seen(sleeping_clients[i].unique); + send_json(sleeping_clients[i].response, + sleeping_clients[i].accept_gzip, + num_viewers); } sleeping_clients = {}; } @@ -68,11 +71,11 @@ var send_404 = function(response) { response.write('Something went wrong. Sorry.'); response.end(); } -var send_json = function(response, accept_gzip) { +var send_json = function(response, accept_gzip, num_viewers) { var headers = { 'Content-Type': 'text/json', 'X-Remoteglot-Last-Modified': json_last_modified, - 'X-Remoteglot-Num-Viewers': count_viewers(), + 'X-Remoteglot-Num-Viewers': num_viewers, 'Access-Control-Allow-Origin': 'http://analysis.sesse.net', 'Access-Control-Expose-Headers': 'X-Remoteglot-Last-Modified, X-Remoteglot-Num-Viewers', 'Expires': 'Mon, 01 Jan 1970 00:00:00 UTC', @@ -90,7 +93,7 @@ var send_json = function(response, accept_gzip) { } var timeout_client = function(client) { mark_recently_seen(client.unique); - send_json(client.response, client.accept_gzip); + send_json(client.response, client.accept_gzip, count_viewers()); delete sleeping_clients[client.request_id]; } var mark_recently_seen = function(unique) { @@ -128,7 +131,8 @@ var count_viewers = function() { fs.watch(path.dirname(json_filename), reread_file); reread_file(null, path.basename(json_filename)); -http.createServer(function(request, response) { +var server = http.createServer(); +server.on('request', function(request, response) { var u = url.parse(request.url, true); var ims = (u.query)['ims']; var unique = (u.query)['unique']; @@ -154,7 +158,7 @@ http.createServer(function(request, response) { // If we already have something newer than what the user has, // just send it out and be done with it. if (json_last_modified !== undefined && (!ims || json_last_modified > ims)) { - send_json(response, accept_gzip); + send_json(response, accept_gzip, count_viewers()); return; } @@ -168,4 +172,16 @@ http.createServer(function(request, response) { client.accept_gzip = accept_gzip; client.unique = unique; sleeping_clients[request_id++] = client; -}).listen(5000); + + request.socket.client = client; +}); +server.on('connection', function(socket) { + socket.on('close', function() { + var client = socket.client; + if (client) { + mark_recently_seen(client.unique); + delete sleeping_clients[client.request_id]; + } + }); +}); +server.listen(5000);