Mercurial > hg > rlgwebd
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 |
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 | 2 |
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 | 8 var pty = require(path.join(localModules, "pty.js")); |
140 | 9 var child_process = require("child_process"); |
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 | 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 | 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 | 21 function TermSessionWS(conn, h, w) { |
22 var ss = this; | |
23 /* Set up the sizes. */ | |
24 w = Math.floor(Number(w)); | |
25 if (!(w > 0 && w < 256)) | |
26 w = 80; | |
27 this.w = w; | |
28 h = Math.floor(Number(h)); | |
29 if (!(h > 0 && h < 256)) | |
30 h = 25; | |
31 this.h = h; | |
32 this.conn = conn; | |
33 /* Customize the environment. */ | |
34 var childenv = {}; | |
35 for (var key in process.env) { | |
36 if (!(key in env_dontuse)) | |
37 childenv[key] = process.env[key]; | |
38 } | |
39 var spawnopts = {"env": childenv, "cwd": process.env["HOME"], | |
40 "rows": this.h, "cols": this.w}; | |
41 this.term = pty.spawn("bash", [], spawnopts); | |
42 this.alive = true; | |
43 this.term.on("data", function (datastr) { | |
44 var buf = new Buffer(datastr); | |
45 if (ss.conn.connected) | |
46 ss.conn.sendUTF(JSON.stringify({"t": "d", "d": buf.toString("hex")})); | |
47 }); | |
48 this.term.on("exit", function () { | |
49 ss.alive = false; | |
50 /* Wait for all the data to get collected */ | |
51 setTimeout(ss.cleanup, 1000); | |
52 }); | |
53 this.conn.on("message", function (msg) { | |
54 try { | |
55 var msgObj = JSON.parse(msg.utf8Data); | |
56 } | |
57 catch (e) { | |
58 return; | |
59 } | |
60 if (msgObj.t == "d") { | |
61 var hexstr = msgObj["d"].replace(/[^0-9a-f]/gi, ""); | |
62 if (hexstr.length % 2 != 0) { | |
63 return; | |
64 } | |
65 var keybuf = new Buffer(hexstr, "hex"); | |
66 ss.term.write(keybuf); | |
67 } | |
68 }); | |
69 this.conn.on("close", function (msg) { | |
70 if (ss.alive) | |
71 ss.term.kill('SIGHUP'); | |
72 console.log("WebSocket connection closed."); | |
73 }); | |
74 this.cleanup = function () { | |
75 /* Call this when the child is dead. */ | |
76 if (ss.alive) | |
77 return; | |
78 if (ss.conn.connected) { | |
79 ss.conn.sendUTF(JSON.stringify({"t": "q"})); | |
80 } | |
81 }; | |
82 this.conn.sendUTF(JSON.stringify({"t": "l", "w": w, "h": h})); | |
83 console.log("New WebSocket connection."); | |
84 } | |
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)) |