Mercurial > hg > rlgwebd
annotate rlgwebd.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 | 43340faa061c |
rev | line source |
---|---|
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1 #!/usr/bin/env node |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
2 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
3 // If you can't quite trust node to find it on its own |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
4 var localModules = '/usr/local/lib/node_modules/'; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
5 var http = require('http'); |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
6 var net = require('net'); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
7 var url = require('url'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
8 var path = require('path'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
9 var fs = require('fs'); |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
10 var events = require('events'); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
11 var child_process = require('child_process'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
12 var daemon = require(path.join(localModules, "daemon")); |
87
bd2cf6dda28d
RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents:
85
diff
changeset
|
13 var pty = require(path.join(localModules, "pty.js")); |
100
3dbfdaf62623
RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
94
diff
changeset
|
14 var WebSocketServer = require(path.join(localModules, "websocket")).server; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
15 |
30
b5570a594266
rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents:
29
diff
changeset
|
16 /* Configuration variables */ |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
17 // These first two files are NOT in the chroot. |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
18 var ctlsocket = "/var/local/rlgwebd/ctl"; |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
19 var logfile = "/var/local/rlgwebd/log"; |
30
b5570a594266
rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents:
29
diff
changeset
|
20 var httpPort = 8080; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
21 var chrootDir = "/var/dgl/"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
22 var dropToUID = 501; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
23 var dropToGID = 501; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
24 var serveStaticRoot = "/var/www/"; // inside the chroot |
36
a0387f112bcf
RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents:
34
diff
changeset
|
25 var playtimeout = 3600000; // Idle time before games are autosaved, in ms |
30
b5570a594266
rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents:
29
diff
changeset
|
26 |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
27 /* Data on the games available. */ |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
28 var games = { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
29 "rogue3": { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
30 "name": "Rogue V3", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
31 "uname": "rogue3", |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
32 "suffix": ".r3sav", |
60
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
33 "path": "/bin/rogue3", |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
34 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
35 }, |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
36 "rogue4": { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
37 "name": "Rogue V4", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
38 "uname": "rogue4", |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
39 "suffix": ".r4sav", |
60
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
40 "path": "/bin/rogue4", |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
41 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
42 }, |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
43 "rogue5": { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
44 "name": "Rogue V5", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
45 "uname": "rogue5", |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
46 "suffix": ".r5sav", |
60
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
47 "path": "/bin/rogue5", |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
48 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
49 }, |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
50 "srogue": { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
51 "name": "Super-Rogue", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
52 "uname": "srogue", |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
53 "suffix": ".srsav", |
60
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
54 "path": "/bin/srogue", |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
55 "clear": new Buffer([27, 91, 72, 27, 91, 74]) // CSI H CSI J |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
56 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
57 }; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
58 |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
59 /* Global state */ |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
60 var logins = {}; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
61 var sessions = {}; |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
62 var clients = {}; |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
63 var allowlogin = true; |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
64 var nextsession = 0; |
104
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
65 var gamemux = new events.EventEmitter(); |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
66 |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
67 /* Constructor. A TermSession handles a pty and the game running on it. |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
68 * game: (String) Name of the game to launch. |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
69 * lkey: (String, key) The user's id, a key into logins. |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
70 * dims: (Array [Number, Number]) Height and width of the pty. |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
71 * handlers: (Object) Key-value pairs, event names and functions to |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
72 * install to handle them. |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
73 * Events: |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
74 * "open": Emitted on startup. Parameters: success (Boolean) |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
75 * "data": Data generated by child. Parameters: buf (Buffer) |
87
bd2cf6dda28d
RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents:
85
diff
changeset
|
76 * "exit": Child terminated. Parameters: none |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
77 */ |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
78 function TermSession(game, lkey, dims, handlers) { |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
79 var ss = this; |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org> |