在浏览器中模拟linux终端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19561188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simulating linux terminal in browser
提问by DevC
I have read about Fabrice Bellard's linux simulation in browser.
我在浏览器中阅读了 Fabrice Bellard 的 linux 模拟。
How does Linux emulator in Javascript by Fabrice Bellard work?
Fabrice Bellard 编写的 Javascript 中的 Linux 模拟器如何工作?
Today I stumbled upon this site, where they are simulating full linux terminal in browser, I am able to run python, perl etc. I know they are running their site on node.js, but I couldn't figure out how they exactly simulating the terminal.
今天我偶然发现了这个网站,他们在浏览器中模拟完整的 linux 终端,我能够运行 python、perl 等。我知道他们在 node.js 上运行他们的网站,但我无法弄清楚他们是如何精确模拟的终点站。
http://runnable.com/UWRl3KlLuONCAACG/read-files-from-filesystem-in-python
http://runnable.com/UWRl3KlLuONCAACG/read-files-from-filesystem-in-python
采纳答案by generalhenry
The full linux is http://docker.io, the rest is https://github.com/Runnable/dockworker
完整的 linux 是http://docker.io,其余的是https://github.com/Runnable/dockworker
We're not simulating the terminal but as Kyle says, replicating the terminal over websockets (with an ajax fallback).
我们不是在模拟终端,而是正如 Kyle 所说,通过 websockets 复制终端(带有 ajax 回退)。
In the browser we're using https://github.com/chjj/term.jswhich was derived from Fabrice Bellard's emulator. It handles the output, and also the keystroke capture.
在浏览器中,我们使用源自 Fabrice Bellard 的模拟器的https://github.com/chjj/term.js。它处理输出以及击键捕获。
回答by Kyle Mueller
Let me prefix this by saying it is NOT a good idea to do this.
让我先说这样做不是一个好主意。
But, You can spawn a shell and use web-sockets or XMLHttpRequests to push keypresses to the spawned server process. Here's a working example of one that runs on windows. Unfortunately, I didn't get around to hooking up / figuring out Ctrl+c. But, you should get the gist of it.
但是,您可以生成一个 shell 并使用 web-sockets 或 XMLHttpRequests 将按键推送到生成的服务器进程。这是在 Windows 上运行的一个工作示例。不幸的是,我没有去连接/弄清楚 Ctrl+c。但是,您应该了解它的要点。
require("underscore");
var Server = {},
express = require("express"),
path = require("path"),
sys = require("sys"),
application_root = __dirname;
global.Server = Server;
Server.root = application_root;
global.app = express();
Server.setup = require("./lib/setup.js").setup({
//redis: require("./lib/redis-client").createClient(),
app: app,
//mongoose : require("mongoose"),
io : require("socket.io"),
express : express,
port: 1773,
paths : {
views : path.join(application_root,"app","views"),
root : path.join(application_root,"public"),
controllers : path.join(application_root,"app","controllers"),
models : path.join(application_root,"app","models")
}
});
var proc = require('child_process'),
cmd;
app.socket.on('connection', function(socket) {
if (!cmd) {
//console.log('spawning cmd');
cmd = proc.spawn('cmd');
//console.log(cmd?'CMD started':'CMD not started');
if (cmd.stdout) {
//console.log('stdout present');
cmd.stdout.on('data',function(data) {
if (data) {
//console.log("data: "+data);
socket.emit('cmd', ""+data);
}
});
}
if (cmd.stderr) {
cmd.stderr.on('data', function(data) {
//console.log('stderr present');
if (data) {
socket.emit('cmd', ""+data);
}
});
}
cmd.on('exit', function() {
//console.log('cmd exited');
socket.emit('cmd', '[CMD Shutdown]');
if (cmd) {
cmd.kill();
cmd = null;
}
});
}
socket.on('sendCmd', function(data) {
if (data && data.buffer) {
var kB = data.buffer.replace("\r","\n");
if (cmd && cmd.stdin) {
cmd.stdin.write(kB);
}
}
});
socket.on('disconnect', function() {
console.log('connection closed');
if (cmd) {
cmd.stdin.end(); //.kill();
if (cmd) {
cmd.kill();
cmd = null;
}
}
});
});
Edit: Actually, this is a portion of a working example. It's missing the client side where you capture and send the keystrokes to the server. But, it should give you the general idea.
编辑:实际上,这是一个工作示例的一部分。它缺少您捕获击键并将其发送到服务器的客户端。但是,它应该给你一个大致的想法。