comparison rlgterm.js @ 55:96815eae4ebe

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.
author John "Elwin" Edwards <elwin@sdf.org>
date Mon, 18 Jun 2012 13:43:51 -0700
parents 2eda3909f6a3
children 7f3ca16409fe
comparison
equal deleted inserted replaced
54:de01aafd4dd6 55:96815eae4ebe
149 /* Processes a message from the server, returning true or false if it was a 149 /* Processes a message from the server, returning true or false if it was a
150 * data message with or without data, null if not data. 150 * data message with or without data, null if not data.
151 * All non-special responseTexts should be handed directly to this function. 151 * All non-special responseTexts should be handed directly to this function.
152 */ 152 */
153 function processMsg(msg) { 153 function processMsg(msg) {
154 var msgDict; 154 var msgDicts;
155 var havedata = null; // eventual return value 155 var havedata = null; // eventual return value
156 try { 156 try {
157 msgDict = JSON.parse(msg); 157 msgDicts = JSON.parse(msg);
158 } catch (e) { 158 } catch (e) {
159 if (e instanceof SyntaxError) 159 if (e instanceof SyntaxError)
160 return null; 160 return null;
161 } 161 }
162 if (!msgDict.t) 162 if (msgDicts.length === 0)
163 return null; 163 return false;
164 else if (msgDict.t == "E") { 164 for (var j = 0; j < msgDicts.length; j++) {
165 if (msgDict.c == 1 || msgDict.c == 6 || msgDict.c == 7) { 165 if (!msgDicts[j].t)
166 continue;
167 else if (msgDicts[j].t == "E") {
168 if (msgDicts[j].c == 1 || msgDicts[j].c == 6 || msgDicts[j].c == 7) {
169 gameover();
170 if (msgDicts[j].c == 1) {
171 logout();
172 }
173 }
174 debug(1, "Server error: " + msgDicts[j].s);
175 }
176 // A data message
177 else if (msgDicts[j].t == "d") {
178 if (msgDicts[j].n === nrecv) {
179 writeData(msgDicts[j].d);
180 nrecv++;
181 /* Process anything in the queue that's now ready. */
182 var next;
183 while ((next = msgQ.shift()) !== undefined) {
184 writeData(next.d);
185 nrecv++;
186 }
187 }
188 else if (msgDicts[j].n > nrecv) {
189 /* The current message comes after one still missing. Queue this one
190 * for later use. */
191 debug(1, "Got packet " + msgDicts[j].n + ", expected " + nrecv);
192 msgQ[msgDicts[j].n - nrecv - 1] = msgDicts[j];
193 }
194 else {
195 /* This message's number was encountered previously. */
196 debug(1, "Discarding packet " + msgDicts[j].n + ", expected " + nrecv);
197 }
198 havedata = true;
199 }
200 else if (msgDicts[j].t == "T") {
201 setTitle(msgDicts[j].d);
202 }
203 else if (msgDicts[j].t == "q") {
166 gameover(); 204 gameover();
167 if (msgDict.c == 1) {
168 logout();
169 }
170 }
171 debug(1, "Server error: " + msgDict.s);
172 }
173 else if (msgDict.t == "n") {
174 havedata = false;
175 }
176 // A data message
177 else if (msgDict.t == "d"){
178 if (msgDict.n === nrecv) {
179 writeData(msgDict.d);
180 nrecv++;
181 /* Process anything in the queue that's now ready. */
182 var next;
183 while ((next = msgQ.shift()) !== undefined) {
184 writeData(next.d);
185 nrecv++;
186 }
187 }
188 else if (msgDict.n > nrecv) {
189 /* The current message comes after one still missing. Queue this one
190 * for later use. */
191 debug(1, "Got packet " + msgDict.n + ", expected " + nrecv);
192 msgQ[msgDict.n - nrecv - 1] = msgDict;
193 } 205 }
194 else { 206 else {
195 /* This message's number was encountered previously. */ 207 debug(1, "Unrecognized server message " + msg);
196 debug(1, "Discarding packet " + msgDict.n + ", expected " + nrecv); 208 }
197 }
198 havedata = true;
199 }
200 else if (msgDict.t == "T") {
201 setTitle(msgDict.d);
202 }
203 else if (msgDict.t == "q") {
204 gameover();
205 }
206 else {
207 debug(1, "Unrecognized server message " + msg);
208 } 209 }
209 return havedata; 210 return havedata;
210 } 211 }
211 212
212 function getData() { 213 function getData() {
486 var req = new XMLHttpRequest(); 487 var req = new XMLHttpRequest();
487 req.onreadystatechange = function () { 488 req.onreadystatechange = function () {
488 if (req.readyState != 4 || req.status != 200) 489 if (req.readyState != 4 || req.status != 200)
489 return; 490 return;
490 var reply = JSON.parse(req.responseText); 491 var reply = JSON.parse(req.responseText);
491 if (reply.t == 'l') { 492 if (reply.t == 's') {
492 /* Success */ 493 /* Success */
493 termemu.sessid = reply.id; 494 termemu.sessid = reply.id;
494 termemu.resize(reply.h, reply.w); 495 termemu.resize(reply.h, reply.w);
495 setTitle("Playing as " + lname); 496 setTitle("Playing as " + lname);
496 debug(1, "Playing with id " + termemu.sessid); 497 debug(1, "Playing with id " + termemu.sessid);