Skip to content

Instantly share code, notes, and snippets.

@omega
Forked from mixonic/server.js
Created July 7, 2011 11:59
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omega/1069367 to your computer and use it in GitHub Desktop.
Save omega/1069367 to your computer and use it in GitHub Desktop.
Node.js + Socket.io + Bash. A collaborative terminal for your browser - updated for socket.io 0.7
//
// 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('socket.io'),
sys = require('sys'),
util = require('util'),
spawn = require('child_process').spawn;
var sh = spawn('bash');
sh.stdout.on('data', function(data) {
listener.sockets.emit("stdout", data);
});
sh.stderr.on('data', function(data) {
listener.sockets.emit("stderr", data);
});
sh.on('exit', function (code) {
listener.sockets.emit("exit", '** 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.sockets.on('connection', function(client){
client.on('message', function(data){
sh.stdin.write(data+"\n");
client.broadcast.send(new Buffer("> "+data));
});
});
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="/socket.io/socket.io.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});
var socket = io.connect('/');
socket.on('stdout', 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>
@ninadpchaudhari
Copy link

Yup !
works perfectly.
Thanks!

@Hubbitus
Copy link

$ npm list -g socket.io
npm WARN unmet dependency /lib/node_modules/block-stream requires inherits@'~2.0.0' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /lib/node_modules/fstream requires inherits@'~2.0.0' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /lib/node_modules/fstream-ignore requires inherits@'2' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /lib/node_modules/fstream-npm requires inherits@'2' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /lib/node_modules/glob requires inherits@'2' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /lib/node_modules/npmconf requires inherits@'~2.0.0' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /lib/node_modules/tar requires inherits@'2' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
 -> /usr/lib /lib
└── socket.io@1.3.6

But it still faile to start:

$ node server.js 

module.js:340
    throw err;
          ^
Error: Cannot find module 'socket.io'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/pasha/Projects/Outer/bash-node.js/server.js:30:13)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

@krzysiuszcze
Copy link

Hi, (sorry for my poor English)
I'm tying to use your example, starting Server.js is good, but when i trying to connect term.html in a browser, this error appeared :

/var/www/socket/server.js:52
path.exists(filename, function(exists) {
^
TypeError: undefined is not a function
at Server. (/var/www/socket/server.js:52:10)
at Server. (/var/www/socket/node_modules/socket.io/node_modules/engine.io/lib/server.js:369:22)
at Server. (/var/www/socket/node_modules/socket.io/lib/index.js:258:16)
at Server.emit (events.js:110:17)
at HTTPParser.parserOnIncoming as onIncoming
at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
at Socket.socketOnData (_http_server.js:343:22)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)

Help please, i don't understand why
Thank's for your answers, i continue trying....

Krzysiu

@krzysiuszcze
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment