annotate webtty.js @ 140:789c094675f4

WebTTY: use WebSockets when possible.
author John "Elwin" Edwards
date Mon, 22 Jul 2013 07:51:53 -0700
parents f14e92f6d955
children c4a32007d2dc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
137
f14e92f6d955 webtty: don't hardcode paths.
John "Elwin" Edwards
parents: 86
diff changeset
2
f14e92f6d955 webtty: don't hardcode paths.
John "Elwin" Edwards
parents: 86
diff changeset
3 var localModules = '/usr/lib/node_modules/';
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
4 var http = require('http');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
5 var url = require('url');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
6 var path = require('path');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
7 var fs = require('fs');
137
f14e92f6d955 webtty: don't hardcode paths.
John "Elwin" Edwards
parents: 86
diff changeset
8 var pty = require(path.join(localModules, "pty.js"));
140
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
9 var child_process = require("child_process");
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
10 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
11
137
f14e92f6d955 webtty: don't hardcode paths.
John "Elwin" Edwards
parents: 86
diff changeset
12 var serveStaticRoot = fs.realpathSync(".");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
13 var sessions = {};
140
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
14 var sessionsWS = {};
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
15
2
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
16 var env_dontuse = {"TMUX": true, "TMUX_PANE": true};
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
17
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
18 /* Constructor for TermSessions. Note that it opens the terminal and
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
19 * adds itself to the sessions dict.
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
20 */
140
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
21 function TermSessionWS(conn, h, w) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
22 var ss = this;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
23 /* Set up the sizes. */
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
24 w = Math.floor(Number(w));
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
25 if (!(w > 0 && w < 256))
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
26 w = 80;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
27 this.w = w;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
28 h = Math.floor(Number(h));
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
29 if (!(h > 0 && h < 256))
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
30 h = 25;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
31 this.h = h;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
32 this.conn = conn;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
33 /* Customize the environment. */
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
34 var childenv = {};
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
35 for (var key in process.env) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
36 if (!(key in env_dontuse))
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
37 childenv[key] = process.env[key];
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
38 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
39 var spawnopts = {"env": childenv, "cwd": process.env["HOME"],
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
40 "rows": this.h, "cols": this.w};
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
41 this.term = pty.spawn("bash", [], spawnopts);
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
42 this.alive = true;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
43 this.term.on("data", function (datastr) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
44 var buf = new Buffer(datastr);
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
45 if (ss.conn.connected)
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
46 ss.conn.sendUTF(JSON.stringify({"t": "d", "d": buf.toString("hex")}));
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
47 });
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
48 this.term.on("exit", function () {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
49 ss.alive = false;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
50 /* Wait for all the data to get collected */
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
51 setTimeout(ss.cleanup, 1000);
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
52 });
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
53 this.conn.on("message", function (msg) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
54 try {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
55 var msgObj = JSON.parse(msg.utf8Data);
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
56 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
57 catch (e) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
58 return;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
59 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
60 if (msgObj.t == "d") {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
61 var hexstr = msgObj["d"].replace(/[^0-9a-f]/gi, "");
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
62 if (hexstr.length % 2 != 0) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
63 return;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
64 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
65 var keybuf = new Buffer(hexstr, "hex");
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
66 ss.term.write(keybuf);
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
67 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
68 });
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
69 this.conn.on("close", function (msg) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
70 if (ss.alive)
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
71 ss.term.kill('SIGHUP');
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
72 console.log("WebSocket connection closed.");
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
73 });
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
74 this.cleanup = function () {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
75 /* Call this when the child is dead. */
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
76 if (ss.alive)
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
77 return;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
78 if (ss.conn.connected) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
79 ss.conn.sendUTF(JSON.stringify({"t": "q"}));
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
80 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
81 };
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
82 this.conn.sendUTF(JSON.stringify({"t": "l", "w": w, "h": h}));
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
83 console.log("New WebSocket connection.");
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
84 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
85
9
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
86 function TermSession(sessid, h, w) {
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
87 /* Set up the sizes. */
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
88 w = Math.floor(Number(w));
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
89 if (!(w > 0 && w < 256))
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
90 w = 80;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
91 this.w = w;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
92 h = Math.floor(Number(h));
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
93 if (!(h > 0 && h < 256))
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
94 h = 25;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
95 this.h = h;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
96 /* Customize the environment. */
2
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
97 var childenv = {};
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
98 for (var key in process.env) {
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
99 if (!(key in env_dontuse))
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
100 childenv[key] = process.env[key];
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
101 }
86
ad4229cf8321 WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
102 var spawnopts = {"env": childenv, "cwd": process.env["HOME"],
ad4229cf8321 WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
103 "rows": this.h, "cols": this.w};
ad4229cf8321 WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
104 this.term = pty.spawn("bash", [], spawnopts);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
105 var ss = this;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
106 /* Eventually we'll need to make sure the sessid isn't in use yet. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
107 this.sessid = sessid;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
108 this.alive = true;
15
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
109 this.data = []; // Buffer for the process' output.
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
110 this.nsend = 0; // Number to use for the next message sent.
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
111 this.nrecv = 0; // Number expected on the next message received.
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
112 this.msgQ = []; // Queue for messages that arrived out of order.
86
ad4229cf8321 WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
113 this.term.on("data", function (buf) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
114 ss.data.push(buf);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
115 });
86
ad4229cf8321 WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
116 this.term.on("exit", function () {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
117 ss.alive = false;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
118 /* Wait for all the data to get collected */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
119 setTimeout(ss.cleanup, 1000);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
120 });
15
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
121 this.write = function (data, n) {
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
122 if (!this.alive) {
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
123 /* Throw some kind of exception? */
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
124 return;
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
125 }
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
126 if (n !== this.nrecv) {
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
127 console.log("Session " + this.sessid + ": Expected message " + this.nrecv + ", got " + n);
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
128 }
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
129 this.nrecv = n + 1;
86
ad4229cf8321 WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
130 this.term.write(data);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
131 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
132 this.read = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
133 if (this.data.length == 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
134 return null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
135 var pos = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
136 var i = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
137 for (i = 0; i < this.data.length; i++)
86
ad4229cf8321 WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
138 pos += Buffer.byteLength(this.data[i]);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
139 var nbuf = new Buffer(pos);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
140 var tptr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
141 pos = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
142 while (this.data.length > 0) {
86
ad4229cf8321 WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
143 tptr = new Buffer(this.data.shift());
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
144 tptr.copy(nbuf, pos);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
145 pos += tptr.length;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
146 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
147 return nbuf;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
148 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
149 this.close = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
150 if (this.alive)
86
ad4229cf8321 WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
151 this.term.kill('SIGHUP');
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
152 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
153 this.cleanup = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
154 /* Call this when the child is dead. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
155 if (this.alive)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
156 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
157 /* Give the client a chance to read any leftover data. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
158 if (ss.data.length > 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
159 setTimeout(ss.remove, 8000);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
160 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
161 ss.remove();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
162 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
163 this.remove = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
164 delete sessions[ss.sessid];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
165 console.log("Session " + this.sessid + " removed.");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
166 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
167 sessions[sessid] = this;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
168 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
169
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
170 function randkey() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
171 rnum = Math.floor(Math.random() * 65536 * 65536);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
172 hexstr = rnum.toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
173 while (hexstr.length < 8)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
174 hexstr = "0" + hexstr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
175 return hexstr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
176 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
177
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
178 /* Returns a list of the cookies in the request, obviously. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
179 function getCookies(req) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
180 cookies = [];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
181 if ("cookie" in req.headers) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
182 cookstrs = req.headers["cookie"].split("; ");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
183 for (var i = 0; i < cookstrs.length; i++) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
184 eqsign = cookstrs[i].indexOf("=");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
185 if (eqsign > 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
186 name = cookstrs[i].slice(0, eqsign).toLowerCase();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
187 val = cookstrs[i].slice(eqsign + 1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
188 cookies[name] = val;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
189 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
190 else if (eqsign < 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
191 cookies[cookstrs[i]] = null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
192 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
193 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
194 return cookies;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
195 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
196
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
197 function urlDec(encstr) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
198 var decstr = "";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
199 var tnum;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
200 for (var i = 0; i < encstr.length; i++)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
201 {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
202 if (encstr.charAt(i) == "+")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
203 decstr += " ";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
204 else if (encstr.charAt(i) == "%")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
205 {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
206 tnum = Number("0x" + encstr.slice(i + 1, 2));
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
207 if (!isNaN(tnum) && tnum >= 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
208 decstr += String.fromCharCode(tnum);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
209 i += 2;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
210 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
211 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
212 decstr += encstr.charAt(i);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
213 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
214 return decstr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
215 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
216
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
217 /* Returns the contents of a form */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
218 function getFormValues(formtext) {
13
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
219 var jsonobj;
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
220 try {
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
221 jsonobj = JSON.parse(formtext);
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
222 } catch (e) {
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
223 if (e instanceof SyntaxError)
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
224 return null;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
225 }
13
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
226 return jsonobj;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
227 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
228
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
229 function login(req, res, formdata) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
230 var resheaders = {'Content-Type': 'text/plain'};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
231 var sessid = randkey();
9
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
232 /* The TermSession constructor will check these thoroughly too, but
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
233 * you can't be too suspicious of client-supplied data. */
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
234 var w = 80;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
235 var h = 25;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
236 var t;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
237 if ("w" in formdata) {
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
238 t = Math.floor(Number(formdata["w"]));
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
239 if (t > 0 && t < 256)
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
240 w = t;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
241 }
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
242 if ("h" in formdata) {
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
243 t = Math.floor(Number(formdata["h"]));
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
244 if (t > 0 && t < 256)
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
245 h = t;
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
246 }
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 7
diff changeset
247 var nsession = new TermSession(sessid, h, w);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
248 resheaders["Set-Cookie"] = "ID=" + sessid;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
249 res.writeHead(200, resheaders);
10
d051aad3e95f webtty: begin experimenting with JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
250 var logindict = {"login": true, "id": sessid, "w": w, "h": h};
d051aad3e95f webtty: begin experimenting with JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
251 res.write(JSON.stringify(logindict));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
252 res.end();
86
ad4229cf8321 WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
253 console.log("Started new session with key " + sessid + ", pid " + nsession.term.pid);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
254 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
255 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
256
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
257 function findTermSession(req) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
258 var cookies = getCookies(req);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
259 if ("id" in cookies) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
260 var sessid = cookies["id"];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
261 if (sessid in sessions) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
262 return sessions[sessid];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
263 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
264 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
265 return null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
266 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
267
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
268 function serveStatic(req, res, fname) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
269 var nname = path.normalize(fname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
270 if (nname == "" || nname == "/")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
271 nname = "index.html";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
272 if (nname.match(/\/$/))
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
273 path.join(nname, "index.html"); /* it was a directory */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
274 var realname = path.join(serveStaticRoot, nname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
275 var extension = path.extname(realname);
81
e4773ac5d4d5 Switch to node v0.8.
John "Elwin" Edwards <elwin@sdf.org>
parents: 15
diff changeset
276 fs.exists(realname, function (exists) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
277 var resheaders = {};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
278 if (!exists || !extension || extension == ".html")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
279 resheaders["Content-Type"] = "text/html";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
280 else if (extension == ".png")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
281 resheaders["Content-Type"] = "image/png";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
282 else if (extension == ".css")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
283 resheaders["Content-Type"] = "text/css";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
284 else if (extension == ".js")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
285 resheaders["Content-Type"] = "text/javascript";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
286 else if (extension == ".svg")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
287 resheaders["Content-Type"] = "image/svg+xml";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
288 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
289 resheaders["Content-Type"] = "application/octet-stream";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
290 if (exists) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
291 /* Not nice, not sensible. First see if it's readable, then respond
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
292 * 200 or 500. Don't throw nasty errors. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
293 res.writeHead(200, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
294 fs.readFile(realname, function (error, data) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
295 if (error) throw error;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
296 res.write(data);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
297 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
298 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
299 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
300 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
301 res.writeHead(404, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
302 res.write("<html><head><title>" + nname + "</title></head>\n<body><h1>" + nname + " Not Found</h1></body></html>\n");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
303 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
304 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
305 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
306 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
307 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
308
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
309 function readFeed(res, term) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
310 res.writeHead(200, { "Content-Type": "text/plain" });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
311 if (term) {
11
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
312 var answer = {};
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
313 var result = term.read();
11
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
314 if (result == null) {
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
315 answer["t"] = "n";
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
316 }
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
317 else {
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
318 answer["t"] = "d";
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
319 answer["d"] = result.toString("hex");
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
320 answer["n"] = term.nsend++;
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
321 }
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
322 res.write(JSON.stringify(answer));
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
323 res.end();
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
324 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
325 else {
11
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
326 sendError(res, 1);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
327 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
328 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
329
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
330 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data" ];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
331
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
332 function sendError(res, ecode) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
333 res.writeHead(200, { "Content-Type": "text/plain" });
11
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
334 if (!(ecode >= 0 && ecode < errorcodes.length))
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
335 ecode = 0;
481dcee353c9 webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents: 10
diff changeset
336 res.write(JSON.stringify({"t": "E", "c": ecode, "s": errorcodes[ecode]}));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
337 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
338 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
339
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
340 function handler(req, res) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
341 /* default headers for the response */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
342 var resheaders = {'Content-Type': 'text/html'};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
343 /* The request body will be added to this as it arrives. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
344 var reqbody = "";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
345 var formdata;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
346
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
347 /* Register a listener to get the body. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
348 function moredata(chunk) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
349 reqbody += chunk;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
350 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
351 req.on('data', moredata);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
352
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
353 /* This will send the response once the whole request is here. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
354 function respond() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
355 var target = url.parse(req.url).pathname;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
356 var cterm = findTermSession(req);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
357 /* First figure out if the client is POSTing to a command interface. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
358 if (req.method == 'POST') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
359 formdata = getFormValues(reqbody);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
360 if (target == '/feed') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
361 if (!cterm) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
362 sendError(res, 1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
363 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
364 }
13
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
365 if (formdata["t"] == "q") {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
366 /* The client wants to quit. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
367 // FIXME need to send a message back to the client
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
368 cterm.close();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
369 }
13
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
370 else if (formdata["t"] == "d" && typeof(formdata["d"]) == "string") {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
371 /* process the keys */
13
bf7c26d0b66d webtty: switch upward protocol to JSON
John "Elwin" Edwards <elwin@sdf.org>
parents: 11
diff changeset
372 hexstr = formdata["d"].replace(/[^0-9a-f]/gi, "");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
373 if (hexstr.length % 2 != 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
374 sendError(res, 2);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
375 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
376 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
377 keybuf = new Buffer(hexstr, "hex");
15
7466927c17a5 webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents: 13
diff changeset
378 cterm.write(keybuf, formdata["n"]);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
379 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
380 readFeed(res, cterm);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
381 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
382 else if (target == "/login") {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
383 login(req, res, formdata);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
384 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
385 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
386 res.writeHead(405, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
387 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
388 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
389 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
390 else if (req.method == 'GET' || req.method == 'HEAD') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
391 if (target == '/feed') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
392 if (!cterm) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
393 sendError(res, 1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
394 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
395 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
396 readFeed(res, cterm);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
397 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
398 /* Default page, create a new term */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
399 /* FIXME New term not created anymore, is a special case still needed? */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
400 else if (target == '/') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
401 serveStatic(req, res, "/");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
402 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
403 else /* Go look for it in the filesystem */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
404 serveStatic(req, res, target);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
405 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
406 else { /* Some other method */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
407 res.writeHead(501, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
408 res.write("<html><head><title>501</title></head>\n<body><h1>501 Not Implemented</h1></body></html>\n");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
409 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
410 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
411 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
412 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
413 req.on('end', respond);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
414
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
415 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
416
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
417 process.on("exit", function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
418 for (var sessid in sessions) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
419 if (sessions[sessid].alive)
86
ad4229cf8321 WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
420 sessions[sessid].term.kill('SIGHUP');
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
421 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
422 console.log("Quitting...");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
423 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
424 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
425
140
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
426 function wsRespond(req) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
427 var w, h, conn;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
428 if (req.resourceURL.pathname == "/sock") {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
429 w = parseInt(req.resourceURL.query.w);
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
430 if (isNaN(w) || w <= 0 || w > 256)
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
431 w = 80;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
432 h = parseInt(req.resourceURL.query.h);
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
433 if (isNaN(h) || h <= 0 || h > 256)
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
434 h = 25;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
435 conn = req.accept(null, req.origin);
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
436 new TermSessionWS(conn, h, w);
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
437 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
438 else {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
439 req.reject(404, "No such resource.");
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
440 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
441 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
442
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
443 /* The pty.js module doesn't wait for the processes it spawns, so they
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
444 * become zombies, which leads to unpleasantness when the system runs
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
445 * out of process table entries. But if the child_process module is
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
446 * initialized and a child spawned, node will continue waiting for any
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
447 * children.
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
448 * Someday, some developer will get the bright idea of tracking how many
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
449 * processes the child_process module has spawned, and not waiting if
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
450 * it's zero. Until then, the following useless line will protect us
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
451 * from the zombie hordes.
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
452 * Figuring this out was almost as interesting as the Rogue bug where
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
453 * printf debugging altered whether the high score list was checked.
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
454 */
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
455 child_process.spawn("/bin/true");
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
456
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
457 process.env["TERM"] = "xterm-256color";
140
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
458 var webServer = http.createServer(handler);
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
459 webServer.listen(8080, "127.0.0.1");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
460 console.log('Server running at http://127.0.0.1:8080/');
140
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
461 var wsServer = new webSocketServer({"httpServer": webServer});
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
462 wsServer.on("request", wsRespond);
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
463 console.log('WebSockets online');