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.
This commit is contained in:
parent
2369fa45d4
commit
ee8782bbf4
1 changed files with 22 additions and 21 deletions
43
rlgwebd.js
43
rlgwebd.js
|
|
@ -73,7 +73,6 @@ var gamemux = new events.EventEmitter();
|
||||||
* handlers: (Object) Key-value pairs, event names and functions to
|
* handlers: (Object) Key-value pairs, event names and functions to
|
||||||
* install to handle them.
|
* install to handle them.
|
||||||
* Events:
|
* Events:
|
||||||
* "open": Emitted on startup. Parameters: success (Boolean)
|
|
||||||
* "data": Data generated by child. Parameters: buf (Buffer)
|
* "data": Data generated by child. Parameters: buf (Buffer)
|
||||||
* "exit": Child terminated. Parameters: none
|
* "exit": Child terminated. Parameters: none
|
||||||
*/
|
*/
|
||||||
|
|
@ -88,7 +87,7 @@ function TermSession(game, lkey, dims, handlers) {
|
||||||
this.game = games[game];
|
this.game = games[game];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.emit('open', false);
|
this.failed = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (lkey in logins) {
|
if (lkey in logins) {
|
||||||
|
|
@ -96,7 +95,7 @@ function TermSession(game, lkey, dims, handlers) {
|
||||||
this.pname = logins[lkey].name;
|
this.pname = logins[lkey].name;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.emit('open', false);
|
this.failed = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
/* Grab a spot in the sessions table. */
|
/* Grab a spot in the sessions table. */
|
||||||
|
|
@ -118,7 +117,7 @@ function TermSession(game, lkey, dims, handlers) {
|
||||||
"name": "xterm-256color"};
|
"name": "xterm-256color"};
|
||||||
this.term = pty.spawn(this.game.path, args, spawnopts);
|
this.term = pty.spawn(this.game.path, args, spawnopts);
|
||||||
tslog("%s playing %s (pid %d)", this.pname, this.game.uname, this.term.pid);
|
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);
|
gamemux.emit('begin', this.game.uname, this.pname);
|
||||||
/* Set up the lockfile and ttyrec */
|
/* Set up the lockfile and ttyrec */
|
||||||
this.lasttime = new Date();
|
this.lasttime = new Date();
|
||||||
|
|
@ -324,6 +323,8 @@ function wsWatcher(conn, session) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function wsPlay(wsReq, game, lkey, dims) {
|
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 conn;
|
||||||
var session;
|
var session;
|
||||||
/* Listeners on the WebSocket */
|
/* Listeners on the WebSocket */
|
||||||
|
|
@ -345,21 +346,6 @@ function wsPlay(wsReq, game, lkey, dims) {
|
||||||
session.close();
|
session.close();
|
||||||
}
|
}
|
||||||
/* These listen on the TermSession. */
|
/* 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) {
|
function dataH(chunk) {
|
||||||
var msg = {};
|
var msg = {};
|
||||||
msg.t = "d";
|
msg.t = "d";
|
||||||
|
|
@ -370,12 +356,27 @@ function wsPlay(wsReq, game, lkey, dims) {
|
||||||
if (conn.connected)
|
if (conn.connected)
|
||||||
conn.sendUTF(JSON.stringify({"t": "q"}));
|
conn.sendUTF(JSON.stringify({"t": "q"}));
|
||||||
conn.close();
|
conn.close();
|
||||||
session.removeListener('open', openH);
|
|
||||||
session.removeListener('data', dataH);
|
session.removeListener('data', dataH);
|
||||||
session.removeListener('exit', exitH);
|
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);
|
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) {
|
function wsStart(wsReq) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue