diff webtty.js @ 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 bf7c26d0b66d
line wrap: on
line diff
--- 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? */