diff 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
line wrap: on
line diff
--- a/rlgterm.js	Thu Jun 21 22:15:20 2012 -0700
+++ b/rlgterm.js	Thu Jun 21 23:44:51 2012 -0700
@@ -233,6 +233,7 @@
     return;
   var datareq = new XMLHttpRequest();
   var msg = JSON.stringify({"id": session.id, "t": "n"});
+  datareq.onerror = errHandler;
   datareq.onreadystatechange = function () {
     if (datareq.readyState == 4 && datareq.status == 200) {
       var wasdata = processMsg(datareq.responseText);
@@ -259,10 +260,15 @@
   }
 }
 
+function errHandler() {
+  debug(1, "Server unavailable?");
+}
+
 function sendback(str) {
   /* For responding to terminal queries. */
   var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": str};
   var datareq = new XMLHttpRequest();
+  datareq.onerror = errHandler;
   datareq.onreadystatechange = postResponseHandler;
   datareq.open('POST', '/feed', true);
   datareq.send(JSON.stringify(msgDict));
@@ -310,6 +316,7 @@
   ev.preventDefault();
   var datareq = new XMLHttpRequest();
   var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": code};
+  datareq.onerror = errHandler;
   datareq.onreadystatechange = postResponseHandler;
   datareq.open('POST', '/feed', true);
   datareq.send(JSON.stringify(msgDict));
@@ -360,6 +367,7 @@
     return;
   var datareq = new XMLHttpRequest();
   var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": keystr};
+  datareq.onerror = errHandler;
   datareq.onreadystatechange = postResponseHandler;
   datareq.open('POST', '/feed', true);
   datareq.send(JSON.stringify(msgDict));
@@ -402,6 +410,7 @@
   loginmsg["name"] = document.getElementById("input_name").value;
   loginmsg["pw"] = document.getElementById("input_pw").value;
   var req = new XMLHttpRequest();
+  req.onerror = errHandler;
   req.onreadystatechange = function () {
     if (req.readyState != 4 || req.status != 200) 
       return;
@@ -437,6 +446,7 @@
     statInterval = window.setInterval(getcurrent, statDelta);
   }
   var req = new XMLHttpRequest();
+  req.onerror = errHandler;
   req.onreadystatechange = function () {
     if (req.readyState != 4 || req.status != 200) 
       return;
@@ -499,6 +509,7 @@
   if (session.id != null || !session.lcred)
     return;
   var req = new XMLHttpRequest();
+  req.onerror = errHandler;
   req.onreadystatechange = function () {
     if (req.readyState != 4 || req.status != 200) 
       return;
@@ -572,6 +583,7 @@
   smsg["h"] = 24;
   smsg["w"] = 80;
   var req = new XMLHttpRequest();
+  req.onerror = errHandler;
   req.onreadystatechange = function () {
     if (req.readyState != 4 || req.status != 200) 
       return;
@@ -603,6 +615,7 @@
     return;
   var wmsg = {"n": Number(gamenumber)};
   var req = new XMLHttpRequest();
+  req.onerror = errHandler;
   req.onreadystatechange = function () {
     if (req.readyState != 4 || req.status != 200) 
       return;
@@ -645,6 +658,7 @@
   regmsg["pw"] = document.getElementById("regin_pw").value;
   regmsg["email"] = document.getElementById("regin_email").value;
   var req = new XMLHttpRequest();
+  req.onerror = errHandler;
   req.onreadystatechange = function () {
     if (req.readyState != 4 || req.status != 200) 
       return;
@@ -699,6 +713,7 @@
   if (!session.id)
     return;
   var req = new XMLHttpRequest();
+  req.onerror = errHandler;
   req.onreadystatechange = function () {
     if (req.readyState == 4 && req.status == 200) {
       processMsg(req.responseText);