diff shterm.js @ 153:c4a32007d2dc

WebTTY: remove polling. Communication now uses WebSockets only.
author John "Elwin" Edwards
date Mon, 27 Jan 2014 16:02:27 -0800
parents 789c094675f4
children
line wrap: on
line diff
--- a/shterm.js	Sun Jan 26 19:56:02 2014 -0800
+++ b/shterm.js	Mon Jan 27 16:02:27 2014 -0800
@@ -3,53 +3,8 @@
  */
 
 var isalive = false; // Whether the session is currently active.
-var nsend = 0; // The number of the next packet to send.
-var nrecv = 0; // The next packet expected.
-var msgQ = []; // Queue for out-of-order messages.
 var conn = null; // WebSocket
 
-// A state machine that keeps track of polling the server.
-var ajaxstate = {
-  state: 0,
-  timerID: null,
-  clear: function () {
-    if (this.timerID != null) {
-      window.clearTimeout(this.timerID);
-      this.timerID = null;
-    }
-  },
-  set: function (ms) {
-    this.clear();
-    this.timerID = window.setTimeout(getData, ms);
-  },
-  gotdata: function () {
-    this.set(100);
-    this.state = 0;
-  },
-  gotnothing: function () {
-    if (this.state == 0) {
-      this.set(100);
-      this.state = 1;
-    }
-    else if (this.state == 1) {
-      this.set(300);
-      this.state = 2;
-    }
-    else if (this.state == 2) {
-      this.set(1000);
-      this.state = 3;
-    }
-    else {
-      this.set(5000);
-      this.state = 3;
-    }
-  },
-  posted: function () {
-    this.set(100);
-    this.state = 0;
-  }
-};
-
 function writeData(hexstr) {
   var codenum;
   var codes = [];
@@ -119,88 +74,6 @@
   return;
 }
 
-function processMsg(response) {
-  if (response.t != "d" || typeof(response.d) != "string")
-    return;
-  if (response.n === nrecv) {
-    writeData(response.d);
-    nrecv++;
-    var next;
-    /* msgQ must be shifted every time nrecv is incremented, but the process
-     * stops whenever an empty space, corresponding to an unarrived message,
-     * is encountered.
-     */
-    while ((next = msgQ.shift()) !== undefined) {
-      writeData(next.d);
-      nrecv++;
-    }
-  }
-  else if (response.n > nrecv) {
-    /* The current message comes after one still missing.  Queue this one
-     * for later use.
-     */
-    debug(1, "Got packet " + response.n + ", expected " + nrecv);
-    msgQ[response.n - nrecv - 1] = response;
-  }
-  else {
-    /* This message's number was encountered previously. */
-    debug(1, "Discarding packet " + response.n + ", expected " + nrecv);
-  }
-}
-
-function getData() {
-  if (!isalive)
-    return;
-  var datareq = new XMLHttpRequest();
-  datareq.onreadystatechange = function () {
-    if (datareq.readyState == 4 && datareq.status == 200) {
-      var response = JSON.parse(this.responseText);
-      if (!response.t)
-        return;
-      else if (response.t == "E") {
-        if (response.c == 1) {
-          isalive = false;
-          debug(1, "Server error: " + response.s);
-        }
-      }
-      else if (response.t == "n")
-        ajaxstate.gotnothing();
-      else if (response.t == "d") {
-        processMsg(response);
-        ajaxstate.gotdata();
-      }
-      return;
-    }
-  };
-  datareq.open('GET', '/feed', true);
-  datareq.send(null);
-  return;
-}
-
-function postResponseHandler() {
-  if (this.readyState == 4 && this.status == 200) {
-    var response = JSON.parse(this.responseText);
-    if (!response.t || response.t == "n")
-      return;
-    else if (response.t == "E") {
-      if (response.c == 1) {
-        isalive = false;
-        debug(1, "Server error: " + response.s);
-      }
-      return;
-    }
-    else if (response.t != "d")
-      return;
-    /* It is a data message */
-    if (response.d) {
-      processMsg(response);
-      //debug(1, "Got packet " + response.n);
-    }
-    ajaxstate.posted();
-    return;
-  }
-}
-
 function sendback(str) {
   /* For responding to terminal queries. */
   if (conn) {
@@ -208,11 +81,7 @@
     conn.send(JSON.stringify(msgObj));
   }
   else {
-    var formdata = {"t": "d", "n": nsend++, "d": str};
-    var datareq = new XMLHttpRequest();
-    datareq.onreadystatechange = postResponseHandler;
-    datareq.open('POST', '/feed', true);
-    datareq.send(JSON.stringify(formdata));
+    debug(1, "No connection: cannot send " + str);
   }
   return;
 }
@@ -255,17 +124,8 @@
     return;
   }
   ev.preventDefault();
-  if (conn) {
-    var msgObj = {"t": "d", "d": code};
-    conn.send(JSON.stringify(msgObj));
-  }
-  else {
-    var formdata = {"t": "d", "n": nsend++, "d": code};
-    var datareq = new XMLHttpRequest();
-    datareq.onreadystatechange = postResponseHandler;
-    datareq.open('POST', '/feed', true);
-    datareq.send(JSON.stringify(formdata));
-  }
+  var msgObj = {"t": "d", "d": code};
+  conn.send(JSON.stringify(msgObj));
   //dkey(code);
   return;
 }
@@ -320,17 +180,8 @@
   else
     return;
   //writeData("Sending " + keystr);
-  if (conn) {
-    var msgObj = {"t": "d", "d": keystr};
-    conn.send(JSON.stringify(msgObj));
-  }
-  else {
-    var formdata = {"t": "d", "n": nsend++, "d": keystr};
-    var datareq = new XMLHttpRequest();
-    datareq.onreadystatechange = postResponseHandler;
-    datareq.open('POST', '/feed', true);
-    datareq.send(JSON.stringify(formdata));
-  }
+  var msgObj = {"t": "d", "d": keystr};
+  conn.send(JSON.stringify(msgObj));
   return;
 }
 
@@ -361,9 +212,13 @@
   return;
 }
 
-function loginWS(h, w) {
+function login(h, w) {
   if (conn)
     return;
+  if (!(window.WebSocket)) {
+    debug(1, "Cannot connect: WebSockets not supported");
+    return;
+  }
   var sockurl = "ws://" + window.location.host + "/sock?w=" + w + "&h=" + h;
   conn = new WebSocket(sockurl);
   conn.onopen = function (event) {
@@ -393,54 +248,13 @@
   }
 }
 
-function login(h, w) {
-  if (isalive)
-    return;
-  if (window.WebSocket) {
-    loginWS(h, w);
-    return;
-  }
-  params = {"login": true, "h": h, "w": w};
-  var req = new XMLHttpRequest();
-  req.onreadystatechange = function () {
-    if (req.readyState == 4 && req.status == 200) {
-      var logindict = JSON.parse(req.responseText);
-      if (logindict.login) {
-        /* Success */
-        termemu.resize(logindict.h, logindict.w);
-        isalive = true;
-        nsend = 0;
-        nrecv = 0;
-	setTitle("Logged in");
-        debug(1, "Logged in with id " + logindict.id);
-        getData();
-        return;
-      }
-      return;
-    }
-  };
-  req.open('POST', '/login', true);
-  req.send(JSON.stringify(params));
-  //req.send("login=login&h=" + String(h) + "&w=" + String(w));
-  return;
-}
-
 function stop() {
   if (conn) {
     conn.close();
-    return;
   }
-  var req = new XMLHttpRequest();
-  req.onreadystatechange = function () {
-    if (req.readyState == 4 && req.status == 200) {
-      /* Figure out whether or not it worked. */
-      /* FIXME the server might respond with output. */
-      isalive = false;
-      return;
-    }
-  };
-  req.open('POST', '/feed', true);
-  req.send(JSON.stringify({"t": "q", "n": nsend++}));
+  else {
+    debug(1, "Cannot stop: connection already closed");
+  }
   return;
 }