简体   繁体   English

在浏览器中模拟linux终端

[英]Simulating linux terminal in browser

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

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.js which was derived from Fabrice Bellard's emulator. 在浏览器中,我们使用https://github.com/chjj/term.js ,它来自Fabrice Bellard的模拟器。 It handles the output, and also the keystroke capture. 它处理输出,以及击键捕获。

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. 但是,您可以生成一个shell并使用Web套接字或XMLHttpRequests将按键推送到生成的服务器进程。 Here's a working example of one that runs on windows. 这是一个在Windows上运行的示例。 Unfortunately, I didn't get around to hooking up / figuring out Ctrl+c. 不幸的是,我没有绕过/搞清楚Ctrl + c。 But, you should get the gist of it. 但是,你应该得到它的要点。

  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. 但是,它应该给你一般的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM