mirror of
https://git.proxmox.com/git/mirror_xterm.js
synced 2025-10-04 07:33:45 +00:00
Implement fully featured terminal demo w/ static fallback
This commit is contained in:
parent
180a7af75d
commit
53e8df40bb
4
Dockerfile
Normal file
4
Dockerfile
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
FROM node:4-onbuild
|
||||||
|
MAINTAINER Paris Kasidiaris <paris@sourcelair.com>
|
||||||
|
|
||||||
|
EXPOSE 3000
|
@ -1 +1 @@
|
|||||||
web: ./bin/server
|
web: npm start
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#! /usr/bin/env sh
|
#! /usr/bin/env sh
|
||||||
|
|
||||||
# Development server for xterm.js
|
# Development server for xterm.js
|
||||||
browser-sync start --server --port 8000 --files="**" --no-ui --index index.html --directory
|
node demo/app.js
|
||||||
|
49
demo/app.js
Normal file
49
demo/app.js
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
var express = require('express');
|
||||||
|
var app = express();
|
||||||
|
var expressWs = require('express-ws')(app);
|
||||||
|
var pty = require('pty.js');
|
||||||
|
|
||||||
|
app.use('/src', express.static(__dirname + '/../src'));
|
||||||
|
app.use('/addons', express.static(__dirname + '/../addons'));
|
||||||
|
|
||||||
|
app.get('/', function(req, res){
|
||||||
|
res.sendFile(__dirname + '/index.html');
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/style.css', function(req, res){
|
||||||
|
res.sendFile(__dirname + '/style.css');
|
||||||
|
});
|
||||||
|
|
||||||
|
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('bash', [], {
|
||||||
|
name: 'xterm-color',
|
||||||
|
cols: 80,
|
||||||
|
rows: 24,
|
||||||
|
cwd: process.env.PWD,
|
||||||
|
env: process.env
|
||||||
|
});
|
||||||
|
|
||||||
|
term.on('data', function(data) {
|
||||||
|
ws.send(data);
|
||||||
|
});
|
||||||
|
ws.on('message', function(msg) {
|
||||||
|
term.write(msg);
|
||||||
|
});
|
||||||
|
ws.on('close', function () {
|
||||||
|
console.log('close');
|
||||||
|
process.kill(term.pid);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
var port = process.env.PORT || 3000,
|
||||||
|
host = '0.0.0.0';
|
||||||
|
|
||||||
|
console.log('App listening to ' + host + ':' + port);
|
||||||
|
app.listen(port, host);
|
@ -6,6 +6,7 @@
|
|||||||
<link rel="stylesheet" href="../addons/fullscreen/fullscreen.css" />
|
<link rel="stylesheet" href="../addons/fullscreen/fullscreen.css" />
|
||||||
<link rel="stylesheet" href="style.css" />
|
<link rel="stylesheet" href="style.css" />
|
||||||
<script src="../src/xterm.js" ></script>
|
<script src="../src/xterm.js" ></script>
|
||||||
|
<script src="../addons/attach/attach.js" ></script>
|
||||||
<script src="../addons/fit/fit.js" ></script>
|
<script src="../addons/fit/fit.js" ></script>
|
||||||
<script src="../addons/fullscreen/fullscreen.js" ></script>
|
<script src="../addons/fullscreen/fullscreen.js" ></script>
|
||||||
<script src="main.js" defer ></script>
|
<script src="main.js" defer ></script>
|
||||||
@ -16,4 +17,4 @@
|
|||||||
</h1>
|
</h1>
|
||||||
<div id="terminal-container"></div>
|
<div id="terminal-container"></div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
72
demo/main.js
72
demo/main.js
@ -1,38 +1,60 @@
|
|||||||
var terminalContainer = document.getElementById('terminal-container'),
|
var terminalContainer = document.getElementById('terminal-container'),
|
||||||
term = new Terminal(),
|
term = new Terminal(),
|
||||||
shellprompt = '$ ';
|
protocol = (location.protocol === 'https:') ? 'wss://' : 'ws://',
|
||||||
|
socketURL = protocol + location.hostname + ((location.port) ? (':' + location.port) : '') + '/bash',
|
||||||
|
socket = new WebSocket(socketURL);
|
||||||
|
|
||||||
term.open(terminalContainer);
|
term.open(terminalContainer);
|
||||||
term.fit();
|
term.fit();
|
||||||
|
|
||||||
term.prompt = function () {
|
function runRealTerminal() {
|
||||||
term.write('\r\n' + shellprompt);
|
term.attach(socket);
|
||||||
};
|
term._initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
term.writeln('Welcome to xterm.js');
|
function runFakeTerminal() {
|
||||||
term.writeln('Just type some keys in the prompt below.');
|
if (term._initialized) {
|
||||||
term.writeln('');
|
return;
|
||||||
term.prompt();
|
}
|
||||||
|
|
||||||
term.on('key', function (key, ev) {
|
term._initialized = true;
|
||||||
var printable = (
|
|
||||||
!ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey
|
|
||||||
);
|
|
||||||
|
|
||||||
if (ev.keyCode == 13) {
|
var shellprompt = '$ ';
|
||||||
term.prompt();
|
|
||||||
} else if (ev.keyCode == 8) {
|
term.prompt = function () {
|
||||||
/*
|
term.write('\r\n' + shellprompt);
|
||||||
|
};
|
||||||
|
|
||||||
|
term.writeln('Welcome to xterm.js');
|
||||||
|
term.writeln('This is a local terminal emulation, without a real terminal in the back-end.');
|
||||||
|
term.writeln('Type some keys and commands to play around.');
|
||||||
|
term.writeln('');
|
||||||
|
term.prompt();
|
||||||
|
|
||||||
|
term.on('key', function (key, ev) {
|
||||||
|
var printable = (
|
||||||
|
!ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey
|
||||||
|
);
|
||||||
|
|
||||||
|
if (ev.keyCode == 13) {
|
||||||
|
term.prompt();
|
||||||
|
} else if (ev.keyCode == 8) {
|
||||||
|
/*
|
||||||
* Do not delete the prompt
|
* Do not delete the prompt
|
||||||
*/
|
*/
|
||||||
if (term.x > 2) {
|
if (term.x > 2) {
|
||||||
term.write('\b \b');
|
term.write('\b \b');
|
||||||
|
}
|
||||||
|
} else if (printable) {
|
||||||
|
term.write(key);
|
||||||
}
|
}
|
||||||
} else if (printable) {
|
});
|
||||||
term.write(key);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
term.on('paste', function (data, ev) {
|
term.on('paste', function (data, ev) {
|
||||||
term.write(data);
|
term.write(data);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
socket.onopen = runRealTerminal;
|
||||||
|
socket.onclose = runFakeTerminal;
|
||||||
|
socket.onerror = runFakeTerminal;
|
||||||
|
10
package.json
10
package.json
@ -4,5 +4,13 @@
|
|||||||
"ignore": ["demo", "test", ".gitignore"],
|
"ignore": ["demo", "test", ".gitignore"],
|
||||||
"main": "src/xterm.js",
|
"main": "src/xterm.js",
|
||||||
"repository": "https://github.com/sourcelair/xterm.js",
|
"repository": "https://github.com/sourcelair/xterm.js",
|
||||||
"license": "MIT"
|
"license": "MIT",
|
||||||
|
"devDependencies": {
|
||||||
|
"express": "4.13.4",
|
||||||
|
"express-ws": "2.0.0-rc.1",
|
||||||
|
"pty.js": "0.3.0"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"start": "bash bin/server"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user