]> git.proxmox.com Git - mirror_xterm.js.git/blame - demo/app.js
Merge branch 'master' into 553_find_api
[mirror_xterm.js.git] / demo / app.js
CommitLineData
53e8df40
PK
1var express = require('express');
2var app = express();
3var expressWs = require('express-ws')(app);
15cf76c7 4var os = require('os');
bebe43e5 5var pty = require('node-pty');
53e8df40 6
f57a40ee
PK
7var terminals = {},
8 logs = {};
9
6cc66a0b 10app.use('/build', express.static(__dirname + '/../build'));
53e8df40
PK
11
12app.get('/', function(req, res){
13 res.sendFile(__dirname + '/index.html');
14});
15
16app.get('/style.css', function(req, res){
17 res.sendFile(__dirname + '/style.css');
18});
19
20app.get('/main.js', function(req, res){
21 res.sendFile(__dirname + '/main.js');
22});
23
f57a40ee
PK
24app.post('/terminals', function (req, res) {
25 var cols = parseInt(req.query.cols),
26 rows = parseInt(req.query.rows),
27 term = pty.spawn(process.platform === 'win32' ? 'cmd.exe' : 'bash', [], {
28 name: 'xterm-color',
29 cols: cols || 80,
30 rows: rows || 24,
31 cwd: process.env.PWD,
32 env: process.env
33 });
34
35 console.log('Created terminal with PID: ' + term.pid);
36 terminals[term.pid] = term;
37 logs[term.pid] = '';
38 term.on('data', function(data) {
39 logs[term.pid] += data;
53e8df40 40 });
f57a40ee
PK
41 res.send(term.pid.toString());
42 res.end();
43});
44
45app.post('/terminals/:pid/size', function (req, res) {
46 var pid = parseInt(req.params.pid),
47 cols = parseInt(req.query.cols),
48 rows = parseInt(req.query.rows),
49 term = terminals[pid];
50
51 term.resize(cols, rows);
52 console.log('Resized terminal ' + pid + ' to ' + cols + ' cols and ' + rows + ' rows.');
53 res.end();
54});
55
56app.ws('/terminals/:pid', function (ws, req) {
57 var term = terminals[parseInt(req.params.pid)];
58 console.log('Connected to terminal ' + term.pid);
59 ws.send(logs[term.pid]);
60
53e8df40 61 term.on('data', function(data) {
7a80efd8
DI
62 try {
63 ws.send(data);
64 } catch (ex) {
65 // The WebSocket is not open, ignore
66 }
53e8df40
PK
67 });
68 ws.on('message', function(msg) {
69 term.write(msg);
70 });
71 ws.on('close', function () {
43558fa1 72 term.kill();
f57a40ee
PK
73 console.log('Closed terminal ' + term.pid);
74 // Clean things up
75 delete terminals[term.pid];
76 delete logs[term.pid];
53e8df40
PK
77 });
78});
79
80var port = process.env.PORT || 3000,
15cf76c7 81 host = os.platform() === 'win32' ? '127.0.0.1' : '0.0.0.0';
53e8df40 82
47b01786 83console.log('App listening to http://' + host + ':' + port);
53e8df40 84app.listen(port, host);