webtty: switch all server responses to JSON.

This commit is contained in:
John "Elwin" Edwards 2012-05-11 13:33:48 -07:00
parent 71d43603a9
commit b6bf7038ca
2 changed files with 38 additions and 36 deletions

View file

@ -39,6 +39,7 @@ function TermSession(sessid, h, w) {
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 serveStatic(req, res, fname) {
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 @@ var errorcodes = [ "Generic Error", "Not logged in", "Invalid data" ];
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 @@ function handler(req, res) {
cterm.write(keybuf);
}
readFeed(res, cterm);
res.end();
}
else if (target == "/login") {
login(req, res, formdata);
@ -326,7 +330,6 @@ function handler(req, res) {
return;
}
readFeed(res, cterm);
res.end();
}
/* Default page, create a new term */
/* FIXME New term not created anymore, is a special case still needed? */