comparison rlgterm.js @ 75:2984604ce3e6

Begin handling XMLHttpRequest errors. Add an error handler to the various XMLHttpRequest objects. Eventually it will inform the user if the server is unreachable.
author John "Elwin" Edwards <elwin@sdf.org>
date Thu, 21 Jun 2012 23:44:51 -0700
parents d084610a89b5
children a497ecd116d9
comparison
equal deleted inserted replaced
74:b8b7660c0b5d 75:2984604ce3e6
231 function getData() { 231 function getData() {
232 if (session.id == null) 232 if (session.id == null)
233 return; 233 return;
234 var datareq = new XMLHttpRequest(); 234 var datareq = new XMLHttpRequest();
235 var msg = JSON.stringify({"id": session.id, "t": "n"}); 235 var msg = JSON.stringify({"id": session.id, "t": "n"});
236 datareq.onerror = errHandler;
236 datareq.onreadystatechange = function () { 237 datareq.onreadystatechange = function () {
237 if (datareq.readyState == 4 && datareq.status == 200) { 238 if (datareq.readyState == 4 && datareq.status == 200) {
238 var wasdata = processMsg(datareq.responseText); 239 var wasdata = processMsg(datareq.responseText);
239 if (wasdata != null) { 240 if (wasdata != null) {
240 if (wasdata) 241 if (wasdata)
257 ajaxstate.posted(); 258 ajaxstate.posted();
258 return; 259 return;
259 } 260 }
260 } 261 }
261 262
263 function errHandler() {
264 debug(1, "Server unavailable?");
265 }
266
262 function sendback(str) { 267 function sendback(str) {
263 /* For responding to terminal queries. */ 268 /* For responding to terminal queries. */
264 var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": str}; 269 var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": str};
265 var datareq = new XMLHttpRequest(); 270 var datareq = new XMLHttpRequest();
271 datareq.onerror = errHandler;
266 datareq.onreadystatechange = postResponseHandler; 272 datareq.onreadystatechange = postResponseHandler;
267 datareq.open('POST', '/feed', true); 273 datareq.open('POST', '/feed', true);
268 datareq.send(JSON.stringify(msgDict)); 274 datareq.send(JSON.stringify(msgDict));
269 return; 275 return;
270 } 276 }
308 return; 314 return;
309 } 315 }
310 ev.preventDefault(); 316 ev.preventDefault();
311 var datareq = new XMLHttpRequest(); 317 var datareq = new XMLHttpRequest();
312 var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": code}; 318 var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": code};
319 datareq.onerror = errHandler;
313 datareq.onreadystatechange = postResponseHandler; 320 datareq.onreadystatechange = postResponseHandler;
314 datareq.open('POST', '/feed', true); 321 datareq.open('POST', '/feed', true);
315 datareq.send(JSON.stringify(msgDict)); 322 datareq.send(JSON.stringify(msgDict));
316 return; 323 return;
317 } 324 }
358 } 365 }
359 else 366 else
360 return; 367 return;
361 var datareq = new XMLHttpRequest(); 368 var datareq = new XMLHttpRequest();
362 var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": keystr}; 369 var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": keystr};
370 datareq.onerror = errHandler;
363 datareq.onreadystatechange = postResponseHandler; 371 datareq.onreadystatechange = postResponseHandler;
364 datareq.open('POST', '/feed', true); 372 datareq.open('POST', '/feed', true);
365 datareq.send(JSON.stringify(msgDict)); 373 datareq.send(JSON.stringify(msgDict));
366 return; 374 return;
367 } 375 }
400 return; 408 return;
401 var loginmsg = {}; 409 var loginmsg = {};
402 loginmsg["name"] = document.getElementById("input_name").value; 410 loginmsg["name"] = document.getElementById("input_name").value;
403 loginmsg["pw"] = document.getElementById("input_pw").value; 411 loginmsg["pw"] = document.getElementById("input_pw").value;
404 var req = new XMLHttpRequest(); 412 var req = new XMLHttpRequest();
413 req.onerror = errHandler;
405 req.onreadystatechange = function () { 414 req.onreadystatechange = function () {
406 if (req.readyState != 4 || req.status != 200) 415 if (req.readyState != 4 || req.status != 200)
407 return; 416 return;
408 var reply = JSON.parse(req.responseText); 417 var reply = JSON.parse(req.responseText);
409 if (reply.t == 'l') { 418 if (reply.t == 'l') {
435 } 444 }
436 if (!statInterval) { 445 if (!statInterval) {
437 statInterval = window.setInterval(getcurrent, statDelta); 446 statInterval = window.setInterval(getcurrent, statDelta);
438 } 447 }
439 var req = new XMLHttpRequest(); 448 var req = new XMLHttpRequest();
449 req.onerror = errHandler;
440 req.onreadystatechange = function () { 450 req.onreadystatechange = function () {
441 if (req.readyState != 4 || req.status != 200) 451 if (req.readyState != 4 || req.status != 200)
442 return; 452 return;
443 var reply; 453 var reply;
444 try { 454 try {
497 507
498 function getchoices() { 508 function getchoices() {
499 if (session.id != null || !session.lcred) 509 if (session.id != null || !session.lcred)
500 return; 510 return;
501 var req = new XMLHttpRequest(); 511 var req = new XMLHttpRequest();
512 req.onerror = errHandler;
502 req.onreadystatechange = function () { 513 req.onreadystatechange = function () {
503 if (req.readyState != 4 || req.status != 200) 514 if (req.readyState != 4 || req.status != 200)
504 return; 515 return;
505 var reply; 516 var reply;
506 try { 517 try {
570 smsg["key"] = session.lcred; 581 smsg["key"] = session.lcred;
571 smsg["game"] = game.uname; 582 smsg["game"] = game.uname;
572 smsg["h"] = 24; 583 smsg["h"] = 24;
573 smsg["w"] = 80; 584 smsg["w"] = 80;
574 var req = new XMLHttpRequest(); 585 var req = new XMLHttpRequest();
586 req.onerror = errHandler;
575 req.onreadystatechange = function () { 587 req.onreadystatechange = function () {
576 if (req.readyState != 4 || req.status != 200) 588 if (req.readyState != 4 || req.status != 200)
577 return; 589 return;
578 var reply = JSON.parse(req.responseText); 590 var reply = JSON.parse(req.responseText);
579 if (reply.t == 's') { 591 if (reply.t == 's') {
601 function startwatching(gamenumber) { 613 function startwatching(gamenumber) {
602 if (session.id != null) 614 if (session.id != null)
603 return; 615 return;
604 var wmsg = {"n": Number(gamenumber)}; 616 var wmsg = {"n": Number(gamenumber)};
605 var req = new XMLHttpRequest(); 617 var req = new XMLHttpRequest();
618 req.onerror = errHandler;
606 req.onreadystatechange = function () { 619 req.onreadystatechange = function () {
607 if (req.readyState != 4 || req.status != 200) 620 if (req.readyState != 4 || req.status != 200)
608 return; 621 return;
609 var reply = JSON.parse(req.responseText); 622 var reply = JSON.parse(req.responseText);
610 if (reply.t == 'w') { 623 if (reply.t == 'w') {
643 var regmsg = {}; 656 var regmsg = {};
644 regmsg["name"] = document.getElementById("regin_name").value; 657 regmsg["name"] = document.getElementById("regin_name").value;
645 regmsg["pw"] = document.getElementById("regin_pw").value; 658 regmsg["pw"] = document.getElementById("regin_pw").value;
646 regmsg["email"] = document.getElementById("regin_email").value; 659 regmsg["email"] = document.getElementById("regin_email").value;
647 var req = new XMLHttpRequest(); 660 var req = new XMLHttpRequest();
661 req.onerror = errHandler;
648 req.onreadystatechange = function () { 662 req.onreadystatechange = function () {
649 if (req.readyState != 4 || req.status != 200) 663 if (req.readyState != 4 || req.status != 200)
650 return; 664 return;
651 var reply = JSON.parse(req.responseText); 665 var reply = JSON.parse(req.responseText);
652 if (reply.t == 'r') { 666 if (reply.t == 'r') {
697 711
698 function stop() { 712 function stop() {
699 if (!session.id) 713 if (!session.id)
700 return; 714 return;
701 var req = new XMLHttpRequest(); 715 var req = new XMLHttpRequest();
716 req.onerror = errHandler;
702 req.onreadystatechange = function () { 717 req.onreadystatechange = function () {
703 if (req.readyState == 4 && req.status == 200) { 718 if (req.readyState == 4 && req.status == 200) {
704 processMsg(req.responseText); 719 processMsg(req.responseText);
705 return; 720 return;
706 } 721 }