Last active
April 20, 2022 11:47
Revisions
-
steinwaywhw revised this gist
Apr 3, 2014 . 4 changed files with 72 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ var client = {}; client.run = function (options) { options = options || {}; var socket = io.connect(options.remote || "http://localhost:8080"); socket.on('connect', function() { var term = new Terminal({ cols: 120, rows: 60, useStyle: true, screenKeys: true }); term.on('data', function(data) { socket.emit('data', data); }); socket.on('data', function(data) { term.write(data); }); term.open(options.parent || document.body); term.write('WELCOME!\r\n'); socket.on('disconnect', function() { term.destroy(); }); // for displaying the first command line socket.emit('data', '\n'); }); }; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ <!doctype html> <html> <head> <script src="http://cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></script> <script src="term.js"></script> <script src="client.js"></script> <style type="text/css"> .terminal { font-family: "DejaVu Sans Mono", "Liberation Mono", monospace; font-size: 11px; color: rgb(240, 240, 240); background: #383734 !important; padding: 3px; border: none !important; } </style> </head> <body> <div id="terminal"></div> <script> var e = document.getElementById("terminal"); client.run({ parent: e, remote: "http://localhost:8080/" }) </script> </body> </html> This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ var server = require('./server.js'); server.run(); This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,3 @@ var server = {} var http = require('http'); -
steinwaywhw created this gist
Apr 1, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,76 @@ var server = {} var http = require('http'); var express = require('express'); var io = require('socket.io'); var pty = require('pty.js'); var terminal = require('term.js'); var socket; var term; var buff = []; server.run = function(options) { // create shell process term = pty.fork( process.env.SHELL || 'sh', [], { name: require('fs').existsSync('/usr/share/terminfo/x/xterm-256color') ? 'xterm-256color' : 'xterm', cols: 80, rows: 24, cwd: process.env.HOME } ); // store term's output into buffer or emit through socket term.on('data', function(data) { return !socket ? buff.push(data) : socket.emit('data', data); }); console.log('Created shell with pty master/slave pair (master: %d, pid: %d)', term.fd, term.pid); var app = express(); var server = http.createServer(app); // let term.js handle req/res app.use(terminal.middleware()); // let server listen on the port options = options || {}; server.listen(options.port || 8080); // let socket.io handle sockets io = io.listen(server, {log: false}); io.sockets.on('connection', function(s) { // when connect, store the socket socket = s; // handle incoming data (client -> server) socket.on('data', function(data) { term.write(data); }); // handle connection lost socket.on('disconnect', function() { socket = null; }); // send buffer data to client while (buff.length) { socket.emit('data', buff.shift()); }; }); }; //server.run({port: 8080}); // export server object module.exports = server;