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.
This commit is contained in:
parent
031bc239f8
commit
4174840c3e
1 changed files with 23 additions and 19 deletions
42
rlgwebd.js
42
rlgwebd.js
|
|
@ -872,34 +872,28 @@ function readFeed(client, res) {
|
|||
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 @@ function statusmsg(req, res) {
|
|||
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 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" });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue