RLG-Web: make multiple watchers possible.
Split the TermSession class into the new TermSession, which handles the PTY, and client classes, which handle HTTP sessions. These are Player and Watcher. This allows multiple watchers per game, and other improvements.
This commit is contained in:
parent
41b3389ec0
commit
1ffb72866a
2 changed files with 302 additions and 224 deletions
83
rlgterm.js
83
rlgterm.js
|
|
@ -151,60 +151,61 @@ var msgQ = []; // Queue for out-of-order messages.
|
|||
* All non-special responseTexts should be handed directly to this function.
|
||||
*/
|
||||
function processMsg(msg) {
|
||||
var msgDict;
|
||||
var msgDicts;
|
||||
var havedata = null; // eventual return value
|
||||
try {
|
||||
msgDict = JSON.parse(msg);
|
||||
msgDicts = JSON.parse(msg);
|
||||
} catch (e) {
|
||||
if (e instanceof SyntaxError)
|
||||
return null;
|
||||
}
|
||||
if (!msgDict.t)
|
||||
return null;
|
||||
else if (msgDict.t == "E") {
|
||||
if (msgDict.c == 1 || msgDict.c == 6 || msgDict.c == 7) {
|
||||
gameover();
|
||||
if (msgDict.c == 1) {
|
||||
logout();
|
||||
if (msgDicts.length === 0)
|
||||
return false;
|
||||
for (var j = 0; j < msgDicts.length; j++) {
|
||||
if (!msgDicts[j].t)
|
||||
continue;
|
||||
else if (msgDicts[j].t == "E") {
|
||||
if (msgDicts[j].c == 1 || msgDicts[j].c == 6 || msgDicts[j].c == 7) {
|
||||
gameover();
|
||||
if (msgDicts[j].c == 1) {
|
||||
logout();
|
||||
}
|
||||
}
|
||||
debug(1, "Server error: " + msgDicts[j].s);
|
||||
}
|
||||
debug(1, "Server error: " + msgDict.s);
|
||||
}
|
||||
else if (msgDict.t == "n") {
|
||||
havedata = false;
|
||||
}
|
||||
// A data message
|
||||
else if (msgDict.t == "d"){
|
||||
if (msgDict.n === nrecv) {
|
||||
writeData(msgDict.d);
|
||||
nrecv++;
|
||||
/* Process anything in the queue that's now ready. */
|
||||
var next;
|
||||
while ((next = msgQ.shift()) !== undefined) {
|
||||
writeData(next.d);
|
||||
// A data message
|
||||
else if (msgDicts[j].t == "d") {
|
||||
if (msgDicts[j].n === nrecv) {
|
||||
writeData(msgDicts[j].d);
|
||||
nrecv++;
|
||||
/* Process anything in the queue that's now ready. */
|
||||
var next;
|
||||
while ((next = msgQ.shift()) !== undefined) {
|
||||
writeData(next.d);
|
||||
nrecv++;
|
||||
}
|
||||
}
|
||||
else if (msgDicts[j].n > nrecv) {
|
||||
/* The current message comes after one still missing. Queue this one
|
||||
* for later use. */
|
||||
debug(1, "Got packet " + msgDicts[j].n + ", expected " + nrecv);
|
||||
msgQ[msgDicts[j].n - nrecv - 1] = msgDicts[j];
|
||||
}
|
||||
else {
|
||||
/* This message's number was encountered previously. */
|
||||
debug(1, "Discarding packet " + msgDicts[j].n + ", expected " + nrecv);
|
||||
}
|
||||
havedata = true;
|
||||
}
|
||||
else if (msgDict.n > nrecv) {
|
||||
/* The current message comes after one still missing. Queue this one
|
||||
* for later use. */
|
||||
debug(1, "Got packet " + msgDict.n + ", expected " + nrecv);
|
||||
msgQ[msgDict.n - nrecv - 1] = msgDict;
|
||||
else if (msgDicts[j].t == "T") {
|
||||
setTitle(msgDicts[j].d);
|
||||
}
|
||||
else if (msgDicts[j].t == "q") {
|
||||
gameover();
|
||||
}
|
||||
else {
|
||||
/* This message's number was encountered previously. */
|
||||
debug(1, "Discarding packet " + msgDict.n + ", expected " + nrecv);
|
||||
debug(1, "Unrecognized server message " + msg);
|
||||
}
|
||||
havedata = true;
|
||||
}
|
||||
else if (msgDict.t == "T") {
|
||||
setTitle(msgDict.d);
|
||||
}
|
||||
else if (msgDict.t == "q") {
|
||||
gameover();
|
||||
}
|
||||
else {
|
||||
debug(1, "Unrecognized server message " + msg);
|
||||
}
|
||||
return havedata;
|
||||
}
|
||||
|
|
@ -488,7 +489,7 @@ function startgame(game) {
|
|||
if (req.readyState != 4 || req.status != 200)
|
||||
return;
|
||||
var reply = JSON.parse(req.responseText);
|
||||
if (reply.t == 'l') {
|
||||
if (reply.t == 's') {
|
||||
/* Success */
|
||||
termemu.sessid = reply.id;
|
||||
termemu.resize(reply.h, reply.w);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue