diff --git a/demo/app.js b/demo/app.js index 8661cc62..83195e62 100644 --- a/demo/app.js +++ b/demo/app.js @@ -4,6 +4,9 @@ var expressWs = require('express-ws')(app); var os = require('os'); var pty = require('pty.js'); +var terminals = {}, + logs = {}; + app.use('/src', express.static(__dirname + '/../src')); app.use('/addons', express.static(__dirname + '/../addons')); @@ -19,17 +22,43 @@ app.get('/main.js', function(req, res){ res.sendFile(__dirname + '/main.js'); }); -app.ws('/bash', function(ws, req) { - /** - * Open bash terminal and attach it - */ - var term = pty.spawn(process.platform === 'win32' ? 'cmd.exe' : 'bash', [], { - name: 'xterm-color', - cols: 80, - rows: 24, - cwd: process.env.PWD, - env: process.env +app.post('/terminals', function (req, res) { + var cols = parseInt(req.query.cols), + rows = parseInt(req.query.rows), + term = pty.spawn(process.platform === 'win32' ? 'cmd.exe' : 'bash', [], { + name: 'xterm-color', + cols: cols || 80, + rows: rows || 24, + cwd: process.env.PWD, + env: process.env + }); + + console.log('Created terminal with PID: ' + term.pid); + terminals[term.pid] = term; + logs[term.pid] = ''; + term.on('data', function(data) { + logs[term.pid] += data; }); + res.send(term.pid.toString()); + res.end(); +}); + +app.post('/terminals/:pid/size', function (req, res) { + var pid = parseInt(req.params.pid), + cols = parseInt(req.query.cols), + rows = parseInt(req.query.rows), + term = terminals[pid]; + + term.resize(cols, rows); + console.log('Resized terminal ' + pid + ' to ' + cols + ' cols and ' + rows + ' rows.'); + res.end(); +}); + +app.ws('/terminals/:pid', function (ws, req) { + var term = terminals[parseInt(req.params.pid)]; + console.log('Connected to terminal ' + term.pid); + ws.send(logs[term.pid]); + term.on('data', function(data) { try { ws.send(data); @@ -41,8 +70,11 @@ app.ws('/bash', function(ws, req) { term.write(msg); }); ws.on('close', function () { - console.log('close'); process.kill(term.pid); + console.log('Closed terminal ' + term.pid); + // Clean things up + delete terminals[term.pid]; + delete logs[term.pid]; }); }); diff --git a/demo/index.html b/demo/index.html index ff10a5cb..1d9a2189 100644 --- a/demo/index.html +++ b/demo/index.html @@ -5,20 +5,33 @@ +
-Attention: The demo should be used only for evaluation of xterm.js and not be exposed or shared to public, as this will introduce security risks for the host.