]> git.proxmox.com Git - mirror_xterm.js.git/blob - demo/app.js
Don't crash demo server on refresh
[mirror_xterm.js.git] / demo / app.js
1 var express = require('express');
2 var app = express();
3 var expressWs = require('express-ws')(app);
4 var pty = require('pty.js');
5
6 app.use('/src', express.static(__dirname + '/../src'));
7 app.use('/addons', express.static(__dirname + '/../addons'));
8
9 app.get('/', function(req, res){
10 res.sendFile(__dirname + '/index.html');
11 });
12
13 app.get('/style.css', function(req, res){
14 res.sendFile(__dirname + '/style.css');
15 });
16
17 app.get('/main.js', function(req, res){
18 res.sendFile(__dirname + '/main.js');
19 });
20
21 app.ws('/bash', function(ws, req) {
22 /**
23 * Open bash terminal and attach it
24 */
25 var term = pty.spawn('bash', [], {
26 name: 'xterm-color',
27 cols: 80,
28 rows: 24,
29 cwd: process.env.PWD,
30 env: process.env
31 });
32 term.on('data', function(data) {
33 try {
34 ws.send(data);
35 } catch (ex) {
36 // The WebSocket is not open, ignore
37 }
38 });
39 ws.on('message', function(msg) {
40 term.write(msg);
41 });
42 ws.on('close', function () {
43 console.log('close');
44 process.kill(term.pid);
45 });
46 });
47
48 var port = process.env.PORT || 3000,
49 host = '0.0.0.0';
50
51 console.log('App listening to http://' + host + ':' + port);
52 app.listen(port, host);