-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathapp.js
executable file
·103 lines (79 loc) · 2.74 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env node
// parse the command line
var program = require('commander')
program
.option('-r, --rows <n>', 'Number of rows in the broadcasting terminal', parseInt, 25)
.option('-c, --columns <n>', 'Number of columns in the broadcasting terminal', parseInt, 80)
.option('-s, --size <CxR>', 'Size of the terminal (shorthand for combination of -c and -r)')
.option('-C, --current', 'Use the current terminal\'s size')
.parse(process.argv)
var rows = program.rows
, cols = program.columns
if (program.size) {
var m = program.size.match(/^(\d+)x(\d+)$/i)
if (!m) {
console.log('Invalid size specified! Must be in form CxR')
program.help()
}
cols = parseInt(m[1], 10)
rows = parseInt(m[2], 10)
}
if (program.current) {
rows = process.stdout.rows
cols = process.stdout.columns
}
// create the server and require other libraries
var connect = require('connect')
, app = connect.createServer()
, server = require('http').createServer(app)
, path = require('path')
, send = require('send')
, HeadlessTerminal = require('headless-terminal')
, ScreenBuffer = HeadlessTerminal.ScreenBuffer
// create socket.io server
var io = require('socket.io').listen(server)
// serve static files
app.use(connect.static(__dirname + '/static'))
// create a terminal emulator
var term = new HeadlessTerminal(cols, rows)
console.log('creating a terminal: %dx%d', cols, rows)
// pipe the data to this terminal
term.open()
process.stdin.resume()
process.stdin.on('data', function(buf) {
var str = buf.toString('utf8')
try { term.write(str) } catch (e) { console.log(e); console.log(e.stack) }
})
process.stdin.on('end', function() {
console.log('died')
process.exit(1)
})
// the display as seen by clients
var buffer = new ScreenBuffer()
// when a client is connected, it is initialized with an empty buffer.
// we patch its buffer to our current state
io.sockets.on('connection', function(sock) {
sock.emit('terminal-data', ScreenBuffer.diff(new ScreenBuffer(), buffer))
})
// when the terminal's screen buffer is changed,
// we patch our buffer to match with the terminal's buffer,
// and broadcast the patch
var timeout = null
, jsonSize = 0
term.on('change', function() {
if (timeout == null) timeout = setTimeout(broadcast, 1000 / 30)
})
function broadcast() {
timeout = null
var operations = ScreenBuffer.diff(buffer, term.displayBuffer)
if (operations.length === 0) return
io.sockets.emit('terminal-data', operations)
ScreenBuffer.patch(buffer, operations)
}
// listen
server.listen(Number(process.env.PORT) || 13377, function() {
var address = server.address()
console.log('ttycast listening on %s port %s', address.address, address.port)
})
// export the socket so that it can be re-used by other apps
module.exports.io = io