Mercurial > hg > rlgwebd
diff rlgwebd.js @ 103:f30495f7ede8
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.
author | John "Elwin" Edwards <elwin@sdf.org> |
---|---|
date | Fri, 13 Jul 2012 09:17:05 -0700 |
parents | f34a286c51bd |
children | 7d444ba4739e |
line wrap: on
line diff
--- 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" });