webtty: switch upward protocol to JSON

Switch the client-to-server messages from the HTML forms format to
JSON (for the webtty app).  Message numbers are sent but not yet used.
This commit is contained in:
John "Elwin" Edwards 2012-05-13 20:50:13 -07:00
parent 32127f8b77
commit 090e02ed59
2 changed files with 24 additions and 23 deletions

View file

@ -2,6 +2,9 @@
* is running a shell via the webtty.js server.
*/
/* The number of the next packet to send. */
var nsend = 0;
// A state machine that keeps track of polling the server.
var ajaxstate = {
state: 0,
@ -169,10 +172,11 @@ function postResponseHandler() {
function sendback(str) {
/* For responding to terminal queries. */
var formdata = {"t": "d", "n": nsend++, "d": str};
var datareq = new XMLHttpRequest();
datareq.onreadystatechange = postResponseHandler;
datareq.open('POST', '/feed', true);
datareq.send("keys=" + str);
datareq.send(JSON.stringify(formdata));
return;
}
@ -214,10 +218,11 @@ function sendkey(ev) {
}
if (termemu.alive)
ev.preventDefault();
var formdata = {"t": "d", "n": nsend++, "d": code};
var datareq = new XMLHttpRequest();
datareq.onreadystatechange = postResponseHandler;
datareq.open('POST', '/feed', true);
datareq.send("keys=" + code);
datareq.send(JSON.stringify(formdata));
//dkey(code);
return;
}
@ -263,10 +268,11 @@ function vkey(c) {
else
return;
//writeData("Sending " + keystr);
var formdata = {"t": "d", "n": nsend++, "d": code};
var datareq = new XMLHttpRequest();
datareq.onreadystatechange = postResponseHandler;
datareq.open('POST', '/feed', true);
datareq.send("keys=" + keystr);
datareq.send(JSON.stringify(formdata));
return;
}
@ -300,6 +306,7 @@ function togglectrl() {
function login(h, w) {
if (termemu.alive)
return;
params = {"login": true, "h": h, "w": w};
var req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (req.readyState == 4 && req.status == 200) {
@ -308,6 +315,7 @@ function login(h, w) {
/* Success */
termemu.resize(logindict.h, logindict.w);
termemu.alive = true;
nsend = 0;
setTitle("Logged in");
debug(1, "Logged in with id " + logindict.id);
getData();
@ -317,7 +325,8 @@ function login(h, w) {
}
};
req.open('POST', '/login', true);
req.send("login=login&h=" + String(h) + "&w=" + String(w));
req.send(JSON.stringify(params));
//req.send("login=login&h=" + String(h) + "&w=" + String(w));
return;
}
@ -332,7 +341,7 @@ function stop() {
}
};
req.open('POST', '/feed', true);
req.send("quit=quit");
req.send(JSON.stringify({"t": "q", "n": nsend++}));
return;
}