changeset 11:481dcee353c9

webtty: switch all server responses to JSON.
author John "Elwin" Edwards <elwin@sdf.org>
date Fri, 11 May 2012 13:33:48 -0700
parents d051aad3e95f
children 9e1d83f50c9e
files shterm.js webtty.js
diffstat 2 files changed, 38 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/shterm.js	Wed May 09 16:36:11 2012 -0700
+++ b/shterm.js	Fri May 11 13:33:48 2012 -0700
@@ -119,24 +119,21 @@
   var datareq = new XMLHttpRequest();
   datareq.onreadystatechange = function () {
     if (datareq.readyState == 4 && datareq.status == 200) {
-      var datalines = datareq.responseText.split("\n");
-      if (!datalines[0]) {
+      var response = JSON.parse(this.responseText);
+      if (!response.t)
         return;
-      }
-      else if (datalines[0] == "E1") {
-        termemu.alive = false;
-        return;
+      else if (response.t == "E") {
+        if (response.c == 1) {
+          termemu.alive = false;
+          debug(1, "Server error: " + response.s);
+        }
       }
-      else if (datalines[0].charAt(0) != 'd') {
-        return;
-      }
-      if (datalines[1]) {
-        writeData(datalines[1]);
+      else if (response.t == "n")
+        ajaxstate.gotnothing();
+      else if (response.t == "d") {
+        writeData(response.d);
         ajaxstate.gotdata();
       }
-      else {
-        ajaxstate.gotnothing();
-      }
       return;
     }
   };
@@ -147,18 +144,21 @@
 
 function postResponseHandler() {
   if (this.readyState == 4 && this.status == 200) {
-    var datalines = this.responseText.split("\n");
-    if (!datalines[0])
+    var response = JSON.parse(this.responseText);
+    if (!response.t || response.t == "n")
       return;
-    else if (datalines[0] == "E1") {
-      termemu.alive = false;
+    else if (response.t == "E") {
+      if (response.c == 1) {
+        termemu.alive = false;
+        debug(1, "Server error: " + response.s);
+      }
       return;
     }
-    else if (datalines[0].charAt(0) != "d")
+    else if (response.t != "d")
       return;
     /* It is a data message */
-    if (datalines[1]) {
-      writeData(datalines[1]);
+    if (response.d) {
+      writeData(response.d);
     }
     ajaxstate.posted();
     return;
@@ -301,7 +301,6 @@
   var req = new XMLHttpRequest();
   req.onreadystatechange = function () {
     if (req.readyState == 4 && req.status == 200) {
-      var datalines = req.responseText.split("\n");
       var logindict = JSON.parse(req.responseText);
       if (logindict.login) {
         /* Success */
@@ -324,8 +323,8 @@
   var req = new XMLHttpRequest();
   req.onreadystatechange = function () {
     if (req.readyState == 4 && req.status == 200) {
-      var datalines = req.responseText.split("\n");
       /* Figure out whether or not it worked. */
+      /* FIXME the server might respond with output. */
       termemu.alive = false;
       return;
     }
--- a/webtty.js	Wed May 09 16:36:11 2012 -0700
+++ b/webtty.js	Fri May 11 13:33:48 2012 -0700
@@ -39,6 +39,7 @@
   this.sessid = sessid;
   this.alive = true;
   this.data = [];
+  this.nsend = 0;
   this.child.stdout.on("data", function (buf) {
     ss.data.push(buf);
   });
@@ -244,16 +245,21 @@
 function readFeed(res, term) {
   res.writeHead(200, { "Content-Type": "text/plain" });
   if (term) {
+    var answer = {};
     var result = term.read();
-    if (result == null)
-      resultstr = "";
-    else
-      resultstr = result.toString("hex");
-    res.write("d" + resultstr.length.toString() + "\n" + resultstr + "\n");
+    if (result == null) {
+      answer["t"] = "n";
+    }
+    else {
+      answer["t"] = "d";
+      answer["d"] = result.toString("hex");
+      answer["n"] = term.nsend++;
+    }
+    res.write(JSON.stringify(answer));
+    res.end();
   }
   else {
-    //console.log("Where's the term?");
-    res.write("d0\n\n");
+    sendError(res, 1);
   }
 }
 
@@ -261,10 +267,9 @@
 
 function sendError(res, ecode) {
   res.writeHead(200, { "Content-Type": "text/plain" });
-  if (ecode < errorcodes.length && ecode > 0)
-    res.write("E" + ecode + '\n' + errorcodes[ecode] + '\n');
-  else
-    res.write("E0\nGeneric Error\n");
+  if (!(ecode >= 0 && ecode < errorcodes.length))
+    ecode = 0;
+  res.write(JSON.stringify({"t": "E", "c": ecode, "s": errorcodes[ecode]}));
   res.end();
 }
 
@@ -309,7 +314,6 @@
           cterm.write(keybuf);
         }
         readFeed(res, cterm);
-        res.end();
       }
       else if (target == "/login") {
         login(req, res, formdata);
@@ -326,7 +330,6 @@
           return;
         }
         readFeed(res, cterm);
-        res.end();
       }
       /* Default page, create a new term */
       /* FIXME New term not created anymore, is a special case still needed? */