comparison rlgwebd.js @ 163:0f6da35b27a0

RLGWebD: overhaul the list of current games. The /status WebSocket now only sends a complete list when opened. At 40-second intervals, it sends a list of games that have been updated in the last minute. The client now uses this to keep its own list.
author John "Elwin" Edwards
date Sun, 04 Jan 2015 16:55:57 -0500
parents 5a7e7ec136c8
children 3a97e4ee50f0
comparison
equal deleted inserted replaced
162:5a7e7ec136c8 163:0f6da35b27a0
696 function getStatus(callback) { 696 function getStatus(callback) {
697 var now = new Date(); 697 var now = new Date();
698 var statusinfo = {"s": allowlogin, "g": []}; 698 var statusinfo = {"s": allowlogin, "g": []};
699 for (var tag in sessions) { 699 for (var tag in sessions) {
700 var gamedesc = {}; 700 var gamedesc = {};
701 gamedesc["tag"] = tag;
702 gamedesc["p"] = sessions[tag].pname; 701 gamedesc["p"] = sessions[tag].pname;
703 gamedesc["g"] = sessions[tag].game.uname; 702 gamedesc["g"] = sessions[tag].game.uname;
704 gamedesc["i"] = now - sessions[tag].lasttime; 703 gamedesc["i"] = now - sessions[tag].lasttime;
705 statusinfo["g"].push(gamedesc); 704 statusinfo["g"].push(gamedesc);
706 } 705 }
707 statusinfo["dgl"] = []; 706 statusinfo["dgl"] = [];
708 for (var tag in dglgames) { 707 for (var tag in dglgames) {
709 statusinfo["dgl"].push(tag); 708 var dglinfo = {};
709 var slash = tag.search('/');
710 dglinfo["g"] = tag.slice(0, slash);
711 dglinfo["p"] = tag.slice(slash + 1);
712 dglinfo["i"] = -1;
713 statusinfo["dgl"].push(dglinfo);
710 } 714 }
711 callback(statusinfo); 715 callback(statusinfo);
712 } 716 }
713 717
714 function statusmsg(req, res) { 718 function statusmsg(req, res) {
971 } 975 }
972 else 976 else
973 wsRequest.reject(404, "No such resource."); 977 wsRequest.reject(404, "No such resource.");
974 } 978 }
975 979
976 /* TODO use a list instead */ 980 /* Only games with low idle time are included. Use getStatus() for the
981 * complete list. */
977 function pushStatus() { 982 function pushStatus() {
978 getStatus(function(info) { 983 var now = new Date();
979 info["t"] = "t"; 984 var statusinfo = {"t": "p", "s": allowlogin, "g": [], "dgl": []};
980 gamemux.emit('list', info); 985 for (var tag in sessions) {
981 }); 986 var delta = now - sessions[tag].lasttime;
987 if (delta < 60000) {
988 var gamedesc = {};
989 gamedesc["p"] = sessions[tag].pname;
990 gamedesc["g"] = sessions[tag].game.uname;
991 gamedesc["i"] = delta;
992 statusinfo["g"].push(gamedesc);
993 }
994 }
995 for (var tag in dglgames) {
996 var dglinfo = {};
997 var slash = tag.search('/');
998 dglinfo["g"] = tag.slice(0, slash);
999 dglinfo["p"] = tag.slice(slash + 1);
1000 dglinfo["i"] = -1;
1001 statusinfo["dgl"].push(dglinfo);
1002 }
1003 gamemux.emit('list', statusinfo);
982 } 1004 }
983 1005
984 function shutdown () { 1006 function shutdown () {
985 httpServer.close(); 1007 httpServer.close();
986 httpServer.removeAllListeners('request'); 1008 httpServer.removeAllListeners('request');
1065 tslog('rlgwebd running on port %d', httpPort); 1087 tslog('rlgwebd running on port %d', httpPort);
1066 wsServer = new WebSocketServer({"httpServer": httpServer}); 1088 wsServer = new WebSocketServer({"httpServer": httpServer});
1067 wsServer.on("request", wsHandler); 1089 wsServer.on("request", wsHandler);
1068 tslog('WebSockets are online'); 1090 tslog('WebSockets are online');
1069 progressWatcher = startProgressWatcher(); 1091 progressWatcher = startProgressWatcher();
1070 setInterval(pushStatus, 4000); 1092 setInterval(pushStatus, 40000);
1071 }); 1093 });
1072 1094