Mercurial > hg > rlgwebd
comparison rlgwebd.js @ 176:bf518a00190b
Add client-side support for watching dgamelaunch games.
author | John "Elwin" Edwards |
---|---|
date | Wed, 14 Jan 2015 07:44:22 -0500 |
parents | 4dd87508fc96 |
children | 85fde763c7ed |
comparison
equal
deleted
inserted
replaced
175:4dd87508fc96 | 176:bf518a00190b |
---|---|
107 "name": "xterm-256color"}; | 107 "name": "xterm-256color"}; |
108 this.term = pty.spawn(this.game.path, args, spawnopts); | 108 this.term = pty.spawn(this.game.path, args, spawnopts); |
109 tslog("%s playing %s (pid %d)", this.pname, this.game.uname, this.term.pid); | 109 tslog("%s playing %s (pid %d)", this.pname, this.game.uname, this.term.pid); |
110 this.failed = false; | 110 this.failed = false; |
111 sessions[this.game.uname + "/" + this.pname] = this; | 111 sessions[this.game.uname + "/" + this.pname] = this; |
112 gamemux.emit('begin', this.game.uname, this.pname); | 112 gamemux.emit('begin', this.game.uname, this.pname, 'rlg'); |
113 /* Set up the lockfile and ttyrec */ | 113 /* Set up the lockfile and ttyrec */ |
114 this.lasttime = new Date(); | 114 this.lasttime = new Date(); |
115 var ts = timestamp(this.lasttime); | 115 var ts = timestamp(this.lasttime); |
116 var progressdir = path.join("/dgldir/inprogress", this.game.uname); | 116 var progressdir = path.join("/dgldir/inprogress", this.game.uname); |
117 this.lock = path.join(progressdir, this.pname + ":node:" + ts + ".ttyrec"); | 117 this.lock = path.join(progressdir, this.pname + ":node:" + ts + ".ttyrec"); |
343 } | 343 } |
344 else { | 344 else { |
345 ss.fd = fd; | 345 ss.fd = fd; |
346 ss.emit('open', true); | 346 ss.emit('open', true); |
347 tslog("DGL %s: open", ss.tag()); | 347 tslog("DGL %s: open", ss.tag()); |
348 gamemux.emit('begin', ss.gname, ss.pname, 'dgl'); | |
348 ss.readchunk(); | 349 ss.readchunk(); |
349 ss.recwatcher = fs.watch(ss.ttyrec, function (ev, finame) { | 350 ss.recwatcher = fs.watch(ss.ttyrec, function (ev, finame) { |
350 if (ev == "change") | 351 if (ev == "change") |
351 ss.readchunk(); | 352 ss.readchunk(); |
352 }); | 353 }); |
366 if (connlist[i].connected) | 367 if (connlist[i].connected) |
367 connlist[i].close(); | 368 connlist[i].close(); |
368 } | 369 } |
369 fs.close(this.fd); | 370 fs.close(this.fd); |
370 this.emit("close"); | 371 this.emit("close"); |
372 gamemux.emit('end', this.gname, this.pname); | |
371 tslog("DGL %s: closed", ss.tag()); | 373 tslog("DGL %s: closed", ss.tag()); |
372 }; | 374 }; |
373 this.watchers = []; | 375 this.watchers = []; |
374 /* Attach a watcher. */ | 376 /* Attach a watcher. */ |
375 this.attach = function (wsReq) { | 377 this.attach = function (wsReq) { |
818 /* Currently, this doesn't do anything blocking, but keep the callback */ | 820 /* Currently, this doesn't do anything blocking, but keep the callback */ |
819 function getStatus(callback) { | 821 function getStatus(callback) { |
820 var now = new Date(); | 822 var now = new Date(); |
821 var statusinfo = {"s": allowlogin, "g": []}; | 823 var statusinfo = {"s": allowlogin, "g": []}; |
822 for (var tag in sessions) { | 824 for (var tag in sessions) { |
823 var gamedesc = {}; | 825 var gamedesc = {"c": "rlg"}; |
824 gamedesc["p"] = sessions[tag].pname; | 826 gamedesc["p"] = sessions[tag].pname; |
825 gamedesc["g"] = sessions[tag].game.uname; | 827 gamedesc["g"] = sessions[tag].game.uname; |
826 gamedesc["i"] = now - sessions[tag].lasttime; | 828 gamedesc["i"] = now - sessions[tag].lasttime; |
827 gamedesc["w"] = sessions[tag].watchers.length; | 829 gamedesc["w"] = sessions[tag].watchers.length; |
828 statusinfo["g"].push(gamedesc); | 830 statusinfo["g"].push(gamedesc); |
829 } | 831 } |
830 statusinfo["dgl"] = []; | |
831 for (var tag in dglgames) { | 832 for (var tag in dglgames) { |
832 var dglinfo = {}; | 833 var dglinfo = {"c": "dgl"}; |
833 var slash = tag.search('/'); | 834 var slash = tag.search('/'); |
834 dglinfo["g"] = tag.slice(0, slash); | 835 dglinfo["g"] = tag.slice(0, slash); |
835 dglinfo["p"] = tag.slice(slash + 1); | 836 dglinfo["p"] = tag.slice(slash + 1); |
836 dglinfo["i"] = now - dglgames[tag].lasttime; | 837 dglinfo["i"] = now - dglgames[tag].lasttime; |
837 dglinfo["w"] = dglgames[tag].watchers.length; | 838 dglinfo["w"] = dglgames[tag].watchers.length; |
838 statusinfo["dgl"].push(dglinfo); | 839 statusinfo["g"].push(dglinfo); |
839 } | 840 } |
840 callback(statusinfo); | 841 callback(statusinfo); |
841 } | 842 } |
842 | 843 |
843 function statusmsg(req, res) { | 844 function statusmsg(req, res) { |
1082 getStatus(function (info) { | 1083 getStatus(function (info) { |
1083 info["t"] = "t"; | 1084 info["t"] = "t"; |
1084 conn.sendUTF(JSON.stringify(info)); | 1085 conn.sendUTF(JSON.stringify(info)); |
1085 }); | 1086 }); |
1086 } | 1087 } |
1087 var beginH = function (gname, pname) { | 1088 var beginH = function (gname, pname, client) { |
1088 conn.sendUTF(JSON.stringify({"t": "b", "p": pname, "g": gname})); | 1089 conn.sendUTF(JSON.stringify({"t": "b", "c": client, "p": pname, |
1090 "g": gname})); | |
1089 }; | 1091 }; |
1090 var listH = function (list) { | 1092 var listH = function (list) { |
1091 conn.sendUTF(JSON.stringify(list)); | 1093 conn.sendUTF(JSON.stringify(list)); |
1092 }; | 1094 }; |
1093 var endH = function (gname, pname) { | 1095 var endH = function (gname, pname) { |
1110 | 1112 |
1111 /* Only games with low idle time are included. Use getStatus() for the | 1113 /* Only games with low idle time are included. Use getStatus() for the |
1112 * complete list. */ | 1114 * complete list. */ |
1113 function pushStatus() { | 1115 function pushStatus() { |
1114 var now = new Date(); | 1116 var now = new Date(); |
1115 var statusinfo = {"t": "p", "s": allowlogin, "g": [], "dgl": []}; | 1117 var statusinfo = {"t": "p", "s": allowlogin, "g": []}; |
1116 for (var tag in sessions) { | 1118 for (var tag in sessions) { |
1117 var delta = now - sessions[tag].lasttime; | 1119 var delta = now - sessions[tag].lasttime; |
1118 if (delta < 60000) { | 1120 if (delta < 60000) { |
1119 var gamedesc = {}; | 1121 var gamedesc = {"c": "rlg"}; |
1120 gamedesc["p"] = sessions[tag].pname; | 1122 gamedesc["p"] = sessions[tag].pname; |
1121 gamedesc["g"] = sessions[tag].game.uname; | 1123 gamedesc["g"] = sessions[tag].game.uname; |
1122 gamedesc["i"] = delta; | 1124 gamedesc["i"] = delta; |
1123 gamedesc["w"] = sessions[tag].watchers.length; | 1125 gamedesc["w"] = sessions[tag].watchers.length; |
1124 statusinfo["g"].push(gamedesc); | 1126 statusinfo["g"].push(gamedesc); |
1125 } | 1127 } |
1126 } | 1128 } |
1127 for (var tag in dglgames) { | 1129 for (var tag in dglgames) { |
1128 var delta = now - dglgames[tag].lasttime; | 1130 var delta = now - dglgames[tag].lasttime; |
1129 if (delta < 60000) { | 1131 if (delta < 60000) { |
1130 var dglinfo = {}; | 1132 var dglinfo = {"c": "dgl"}; |
1131 var slash = tag.search('/'); | 1133 var slash = tag.search('/'); |
1132 dglinfo["g"] = tag.slice(0, slash); | 1134 dglinfo["g"] = tag.slice(0, slash); |
1133 dglinfo["p"] = tag.slice(slash + 1); | 1135 dglinfo["p"] = tag.slice(slash + 1); |
1134 dglinfo["i"] = delta; | 1136 dglinfo["i"] = delta; |
1135 dglinfo["w"] = dglgames[tag].watchers.length; | 1137 dglinfo["w"] = dglgames[tag].watchers.length; |
1136 statusinfo["dgl"].push(dglinfo); | 1138 statusinfo["g"].push(dglinfo); |
1137 } | 1139 } |
1138 } | 1140 } |
1139 gamemux.emit('list', statusinfo); | 1141 gamemux.emit('list', statusinfo); |
1140 } | 1142 } |
1141 | 1143 |