From ee8782bbf4b1827d938f7915af0410cfa455db60 Mon Sep 17 00:00:00 2001 From: "John \"Elwin\" Edwards" Date: Fri, 9 Jan 2015 08:55:38 -0500 Subject: [PATCH] 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. --- rlgwebd.js | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/rlgwebd.js b/rlgwebd.js index bfe992c..7efb08d 100755 --- a/rlgwebd.js +++ b/rlgwebd.js @@ -73,7 +73,6 @@ var gamemux = new events.EventEmitter(); * 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 @@ function TermSession(game, lkey, dims, handlers) { this.game = games[game]; } else { - this.emit('open', false); + this.failed = true; return; } if (lkey in logins) { @@ -96,7 +95,7 @@ function TermSession(game, lkey, dims, handlers) { 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 @@ function TermSession(game, lkey, dims, handlers) { "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 wsWatcher(conn, session) { } 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 @@ function wsPlay(wsReq, game, lkey, dims) { 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 @@ function wsPlay(wsReq, game, lkey, dims) { 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) {