Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Node.js + Socket.io + Bash. A collaborative terminal for your browser.

View server.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
//
// This server will start a bash shell and expose it
// over socket.io to a browser. See ./term.html for the
// client side.
//
// You should probably:
//
// npm install socket.io
// curl -O https://github.com/LearnBoost/Socket.IO/raw/master/socket.io.min.js
//
// To get socket.io in the node_modules directory and
// the socket.io.min.js file needed for the client.
//
// To start the server:
//
// node server.js
//
// And then load up your term!
//
// open http://`hostname`:8080/term.html
//
// You can even share the url with a friend on your
// local network. Be sure they're a friend though :-)
//
 
var http = require('http'),
url = require('url'),
path = require('path'),
fs = require('fs'),
io = require('./node_modules/socket.io'),
sys = require('sys'),
util = require('util'),
spawn = require('child_process').spawn;
 
var sh = spawn('bash');
 
sh.stdout.on('data', function(data) {
listener.broadcast(data);
});
 
sh.stderr.on('data', function(data) {
listener.broadcast(data);
});
 
sh.on('exit', function (code) {
listener.broadcast('** Shell exited: '+code+' **');
});
 
server = http.createServer(function(request, response){
var uri = url.parse(request.url).pathname;
var filename = path.join(process.cwd(), uri);
path.exists(filename, function(exists) {
if (!exists) {
response.writeHead(404, {'Content-Type':'text/plain'});
response.end("Can''t find it...");
}
fs.readFile(filename, 'binary',function(err, file){
if (err) {
response.writeHead(500, {'Content-Type':'text/plain'});
response.end(err + "\n");
return;
}
response.writeHead(200);
response.write(file, 'binary');
response.end();
 
});
});
}
);
 
server.listen(8080);
 
var listener = io.listen(server);
 
listener.on('connection', function(client){
client.on('message', function(data){
sh.stdin.write(data+"\n");
listener.broadcast(new Buffer("> "+data));
});
});
View server.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="socket.io.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
function add_content(str){
console.log(str);
$('div').append('<p>' + $('<div/>').text(str).html() + '</p>');
$(window).scrollTop($('body').height() - $(window).height() + 80);
}
var socket = new io.Socket(null, {rememberTransport: false, port: 8080});
socket.connect();
socket.addEvent('message', function(data) {
add_content($.map(data, function(e,i) {
return String.fromCharCode(e);
}).join(''));
});
$(function(){
$('form').submit(function(e){
var input = $('input');
socket.send(input.val());
input.val('');
return false;
});
$('input').focus();
});
</script>
<style type="text/css">
body, p, input {
font-family: fixed;
font-size: 13px;
border: none;
}
p { white-space: pre; }
p, form, input { margin:0; padding:0; }
input:focus { outline: none; border: none; }
</style>
</head>
<body>
<div></div>
<form>
&gt; <input>
</form>
</body>
</html>

path.exists is now called fs.exists
server.js line 52

each command sent through term.html results in: info - unhandled socket.io url
http://stackoverflow.com/questions/7069202/info-unhandled-socket-io-url suggested it's a socket.io API update, but haven't figured it out

any suggestions mixonic?

Anyway, a good piece of code.

All I get when I try to run node server.js is this error message:
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:1011:11)
at Process.ChildProcess._handle.onexit (child_process.js:802:34)

any idea what's not working right?
(followed the instructions in the comment of server.js

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.