# HG changeset patch # User John "Elwin" Edwards # Date 1342196225 25200 # Node ID f30495f7ede8cff2174f76e5dd21f42b1156cfa0 # Parent f34a286c51bd847a1f89d41e2437144bc4a34a81 RLG-Web server: refactor statusmsg() to work with WebSockets. Separate the game-listing and http-responding components of statusmsg() so the listing function can be reused for WebSockets. diff -r f34a286c51bd -r f30495f7ede8 rlgwebd.js --- a/rlgwebd.js Fri Jul 13 08:52:17 2012 -0700 +++ b/rlgwebd.js Fri Jul 13 09:17:05 2012 -0700 @@ -872,34 +872,28 @@ res.end(); } -function statusmsg(req, res) { +function getStatus(callback) { var now = new Date(); - var reply = {"s": allowlogin, "g": []}; - function respond() { - res.writeHead(200, { "Content-Type": "application/json" }); - if (req.method != 'HEAD') - res.write(JSON.stringify(reply)); - res.end(); - } + var statusinfo = {"s": allowlogin, "g": []}; function idleset(i, idletime) { - if (i >= 0 && i < reply.g.length) { - reply.g[i].i = idletime; + if (i >= 0 && i < statusinfo.g.length) { + statusinfo.g[i].i = idletime; } - for (var j = 0; j < reply.g.length; j++) { - if (!("i" in reply.g[j])) + for (var j = 0; j < statusinfo.g.length; j++) { + if (!("i" in statusinfo.g[j])) return; } - respond(); + callback(statusinfo); } for (var sessid in sessions) { var gamedesc = {}; gamedesc["n"] = sessid; gamedesc["p"] = sessions[sessid].pname; gamedesc["g"] = sessions[sessid].game.uname; - reply["g"].push(gamedesc); + statusinfo["g"].push(gamedesc); } - if (reply.g.length == 0) { - respond(); + if (statusinfo.g.length == 0) { + callback(statusinfo); return; } function makecallback(i) { @@ -910,9 +904,9 @@ idleset(i, now - stats.mtime); } } - for (var i = 0; i < reply.g.length; i++) { - if (reply.g[i].n in sessions) { - fs.fstat(sessions[reply.g[i].n].record.fd, makecallback(i)); + for (var i = 0; i < statusinfo.g.length; i++) { + if (statusinfo.g[i].n in sessions) { + fs.fstat(sessions[statusinfo.g[i].n].record.fd, makecallback(i)); } else { idleset(i, null); @@ -920,6 +914,16 @@ } } +function statusmsg(req, res) { + function respond(info) { + res.writeHead(200, { "Content-Type": "application/json" }); + if (req.method != 'HEAD') + res.write(JSON.stringify(info)); + res.end(); + } + getStatus(respond); +} + function pstatusmsg(req, res) { if (req.method == 'HEAD') { res.writeHead(200, { "Content-Type": "application/json" });