Mercurial > hg > rlgwebd
changeset 159:a613380ffdc2
RLGWebD: excise polling.
WebSockets are supported nearly everywhere now.
Listing current games and watching them are still broken.
| author | John "Elwin" Edwards | 
|---|---|
| date | Sat, 03 Jan 2015 15:23:04 -0500 | 
| parents | 9961a538c00e | 
| children | ed837da65e5f | 
| files | rlgterm.js rlgwebd.js | 
| diffstat | 2 files changed, 36 insertions(+), 666 deletions(-) [+] | 
line wrap: on
 line diff
--- a/rlgterm.js Thu Jan 01 15:56:22 2015 -0500 +++ b/rlgterm.js Sat Jan 03 15:23:04 2015 -0500 @@ -1,60 +1,5 @@ /* rlgterm.js: Roguelike Gallery's driver for termemu.js */ -// A state machine that keeps track of polling the server. -var ajaxstate = { - state: 0, - timerID: null, - clear: function () { - if (this.timerID != null) { - window.clearTimeout(this.timerID); - this.timerID = null; - } - }, - set: function (ms) { - this.clear(); - this.timerID = window.setTimeout(getData, ms); - }, - gotdata: function () { - this.set(1000); - this.state = 1; - }, - gotnothing: function () { - if (this.state == 0) { - this.set(1000); - this.state = 1; - } - else if (this.state < 4) { - this.set(4000); - this.state++; - } - else if (session.playing) { - if (this.state < 8) { - this.set(15000); - this.state++; - } - else { - /* It's been over a minute. Stop polling. */ - this.clear(); - } - } - else { - /* If watching, it can't stop polling entirely, because there - * are no POST events to start it up again. */ - this.set(10000); - } - }, - posted: function (wasdata) { - if (wasdata) { - this.set(1000); - this.state = 1; - } - else { - this.set(200); - this.state = 0; - } - } -}; - /* Data on the available games. */ var games = { "rogue3": { @@ -81,7 +26,7 @@ var session = { /* The session id assigned by the server. */ - id: null, + connect: false, /* Login name and key are now in sessionStorage. */ /* Whether the game is being played or just watched. */ playing: false, @@ -165,119 +110,15 @@ return; } -/* State for sending and receiving messages. */ -var nsend = 0; // The number of the next packet to send. -var nrecv = 0; // The next packet expected. -var msgQ = []; // Queue for out-of-order messages. - -/* Processes a message from the server, returning true or false if it was a - * data message with or without data, null if not data. - * All non-special responseTexts should be handed directly to this function. - */ -function processMsg(msg) { - var msgDicts; - var havedata = null; // eventual return value - try { - msgDicts = JSON.parse(msg); - } catch (e) { - if (e instanceof SyntaxError) - return null; - } - 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); - } - // 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. */
