changeset 168:0ceaca924b4c

Remove the TermSession 'open' event. Failure is now signaled with a TermSession.failed flag. Playing two games at the same time no longer causes crashes, but it does send the same output to both players.
author John "Elwin" Edwards
date Fri, 09 Jan 2015 08:55:38 -0500
parents fba1b34e7554
children 6f4b7e1b32e8
files rlgwebd.js
diffstat 1 files changed, 22 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/rlgwebd.js	Wed Jan 07 13:49:37 2015 -0500
+++ b/rlgwebd.js	Fri Jan 09 08:55:38 2015 -0500
@@ -73,7 +73,6 @@
  *   handlers: (Object) Key-value pairs, event names and functions to
  *           install to handle them.
  *  Events:
- *   "open": Emitted on startup.  Parameters: success (Boolean)
  *   "data": Data generated by child. Parameters: buf (Buffer)
  *   "exit": Child terminated. Parameters: none
  */
@@ -88,7 +87,7 @@
     this.game = games[game];
   }
   else {
-    this.emit('open', false);
+    this.failed = true;
     return;
   }
   if (lkey in logins) {
@@ -96,7 +95,7 @@
     this.pname = logins[lkey].name;
   }
   else {
-    this.emit('open', false);
+    this.failed = true;
     return;
   }
   /* Grab a spot in the sessions table. */
@@ -118,7 +117,7 @@
                    "name": "xterm-256color"};
   this.term = pty.spawn(this.game.path, args, spawnopts);
   tslog("%s playing %s (pid %d)", this.pname, this.game.uname, this.term.pid);
-  this.emit('open', true, this.game.uname, this.pname);
+  this.failed = false;
   gamemux.emit('begin', this.game.uname, this.pname);
   /* Set up the lockfile and ttyrec */
   this.lasttime = new Date();
@@ -324,6 +323,8 @@
 }
 
 function wsPlay(wsReq, game, lkey, dims) {
+  tslog("wsPlay: running for %s/%s", game, logins[lkey].name);
+  tslog("Request is for %s", logins[wsReq.resourceURL.query["key"]].name);
   var conn;
   var session;
   /* Listeners on the WebSocket */
@@ -345,21 +346,6 @@
     session.close();
   }
   /* These listen on the TermSession. */
-  function openH(success, gname, pname) {
-    if (success) {
-      var tag = gname + "/" + pname;
-      var reply = {"t": "s", "tag": tag, "w": sessions[tag].w, "h": 
-                   sessions[tag].h, "p": pname, "g": gname};
-      conn = wsReq.accept(null, wsReq.origin);
-      conn.sendUTF(JSON.stringify(reply));
-      conn.on('message', messageH);
-      conn.on('close', closeH);
-    }
-    else {
-      wsReq.reject(500, errorcodes[5]);
-      tslog("Unable to allocate TTY for %s", game);
-    }
-  }
   function dataH(chunk) {
     var msg = {};
     msg.t = "d";
@@ -370,12 +356,27 @@
     if (conn.connected)
       conn.sendUTF(JSON.stringify({"t": "q"}));
     conn.close();
-    session.removeListener('open', openH);
     session.removeListener('data', dataH);
     session.removeListener('exit', exitH);
   }
-  var handlers = {'open': openH, 'data': dataH, 'exit': exitH};
+  var handlers = {'data': dataH, 'exit': exitH};
   session = new TermSession(game, lkey, dims, handlers);
+  if (!session.failed) {
+    var tag = session.game.uname + "/" + session.pname;
+    var reply = {"t": "s", "tag": tag, "w": session.w, "h": session.h, 
+                 "p": session.pname, "g": session.game.uname};
+    tslog("Accepting for %s", tag);
+    tslog("Request is for %s", logins[wsReq.resourceURL.query["key"]].name);
+    tslog("Session is for %s", session.pname);
+    conn = wsReq.accept(null, wsReq.origin);
+    conn.sendUTF(JSON.stringify(reply));
+    conn.on('message', messageH);
+    conn.on('close', closeH);
+  }
+  else {
+    wsReq.reject(500, errorcodes[5]);
+    tslog("Unable to allocate TTY for %s", game);
+  }
 }
 
 function wsStart(wsReq) {