This repository has been archived by the owner on Dec 19, 2020. It is now read-only.
forked from xtermjs/xterm.js
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add debug target for demo server, merge 2 start processes into one
- Loading branch information
Showing
4 changed files
with
111 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,106 @@ | ||
var express = require('express'); | ||
var app = express(); | ||
var expressWs = require('express-ws')(app); | ||
var expressWs = require('express-ws'); | ||
var os = require('os'); | ||
var pty = require('node-pty'); | ||
|
||
var terminals = {}, | ||
logs = {}; | ||
|
||
app.use('/build', express.static(__dirname + '/../build')); | ||
|
||
app.get('/', function(req, res){ | ||
res.sendFile(__dirname + '/index.html'); | ||
}); | ||
|
||
app.get('/style.css', function(req, res){ | ||
res.sendFile(__dirname + '/style.css'); | ||
}); | ||
|
||
app.get('/dist/client-bundle.js', function(req, res){ | ||
res.sendFile(__dirname + '/dist/client-bundle.js'); | ||
}); | ||
|
||
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; | ||
function startServer() { | ||
var app = express(); | ||
expressWs(app); | ||
|
||
var terminals = {}, | ||
logs = {}; | ||
|
||
app.use('/build', express.static(__dirname + '/../build')); | ||
|
||
app.get('/', function(req, res){ | ||
res.sendFile(__dirname + '/index.html'); | ||
}); | ||
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]); | ||
|
||
function buffer(socket, timeout) { | ||
let s = ''; | ||
let sender = null; | ||
return (data) => { | ||
s += data; | ||
if (!sender) { | ||
sender = setTimeout(() => { | ||
socket.send(s); | ||
s = ''; | ||
sender = null; | ||
}, timeout); | ||
} | ||
}; | ||
} | ||
const send = buffer(ws, 5); | ||
|
||
term.on('data', function(data) { | ||
try { | ||
send(data); | ||
} catch (ex) { | ||
// The WebSocket is not open, ignore | ||
} | ||
|
||
app.get('/style.css', function(req, res){ | ||
res.sendFile(__dirname + '/style.css'); | ||
}); | ||
ws.on('message', function(msg) { | ||
term.write(msg); | ||
|
||
app.get('/dist/client-bundle.js', function(req, res){ | ||
res.sendFile(__dirname + '/dist/client-bundle.js'); | ||
}); | ||
ws.on('close', function () { | ||
term.kill(); | ||
console.log('Closed terminal ' + term.pid); | ||
// Clean things up | ||
delete terminals[term.pid]; | ||
delete logs[term.pid]; | ||
|
||
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(); | ||
}); | ||
}); | ||
|
||
var port = process.env.PORT || 3000, | ||
host = os.platform() === 'win32' ? '127.0.0.1' : '0.0.0.0'; | ||
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]); | ||
|
||
function buffer(socket, timeout) { | ||
let s = ''; | ||
let sender = null; | ||
return (data) => { | ||
s += data; | ||
if (!sender) { | ||
sender = setTimeout(() => { | ||
socket.send(s); | ||
s = ''; | ||
sender = null; | ||
}, timeout); | ||
} | ||
}; | ||
} | ||
const send = buffer(ws, 5); | ||
|
||
term.on('data', function(data) { | ||
try { | ||
send(data); | ||
} catch (ex) { | ||
// The WebSocket is not open, ignore | ||
} | ||
}); | ||
ws.on('message', function(msg) { | ||
term.write(msg); | ||
}); | ||
ws.on('close', function () { | ||
term.kill(); | ||
console.log('Closed terminal ' + term.pid); | ||
// Clean things up | ||
delete terminals[term.pid]; | ||
delete logs[term.pid]; | ||
}); | ||
}); | ||
|
||
var port = process.env.PORT || 3000, | ||
host = os.platform() === 'win32' ? '127.0.0.1' : '0.0.0.0'; | ||
|
||
console.log('App listening to http://' + host + ':' + port); | ||
app.listen(port, host); | ||
} | ||
|
||
console.log('App listening to http://' + host + ':' + port); | ||
app.listen(port, host); | ||
module.exports = startServer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters