diff rlgterm.js @ 112:4f2b89e6fde2

RLG-Web: improvements to choices and status messaging. Server-side, have gamemux 'end' events include the name and game, so they can be sent to WebSockets connected to /status. This means a WebSocket client only needs to update its choice list when it gets a begin or end message with its own username. So it only needs to check /pstatus/<name> at those times and can stop polling.
author John "Elwin" Edwards <elwin@sdf.org>
date Mon, 16 Jul 2012 08:23:51 -0700
parents f56fdfeed01a
children d7d7cdcba3b4
line wrap: on
line diff
--- a/rlgterm.js	Sun Jul 15 22:33:44 2012 -0700
+++ b/rlgterm.js	Mon Jul 16 08:23:51 2012 -0700
@@ -533,6 +533,10 @@
     }
     return;
   }
+  if (session.lcred) {
+    /* When starting the socket, the choices list might not be initialized. */
+    getchoices();
+  }
   if (statsock)
     return; 
   statsock = new WebSocket("ws://" + window.location.host + "/status");
@@ -544,10 +548,12 @@
       if (e instanceof SyntaxError)
         return;
     }
-    if (msg.t != "t") {
-      return;
+    if (msg.t == "t") {
+      tableCurrent(msg.g);
     }
-    tableCurrent(msg.g);
+    else if ((msg.t == "b" || msg.t == "e") && msg.p == session.lname) {
+      getchoices();
+    }
   };
   statsock.onclose = function (ev) {
     statsock = null;
@@ -555,6 +561,9 @@
 }
 
 function getcurrent(clear) {
+  if (WebSocket) {
+    return;
+  }
   if (session.id || clear) {
     if (statInterval) {
       window.clearInterval(statInterval);
@@ -567,9 +576,6 @@
   }
   if (session.lcred)
     getchoices();
-  if (WebSocket) {
-    return;
-  }
   var req = new XMLHttpRequest();
   req.onerror = errHandler;
   req.onreadystatechange = function () {