Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Running a "full-featured" REPL using a net.Server and net.Socket [FIX: http://nodejs.org/api/stream.html#stream_compatibility_with_older_node_versions]
var net = require('net');
var socket = net.connect(2333);
process.stdin.pipe(socket);
/// For backwards compatibility with Node program older than v0.10,
/// readable streams switch into "flowing mode" when a 'data' event handler
/// is added, or when the pause() or resume() methods are called.
process.stdin.on('data', function (buffer) {
if (buffer.length === 1 && buffer[0] === 0x04) { // EOT
process.stdin.emit('end'); // process.stdin will be destroyed
process.stdin.setRawMode(false);
process.stdin.pause(); // stop emitting 'data' event
}
});
/// this event won't be fired if REPL is exited by '.exit' command
process.stdin.on('end', function () {
console.log('.exit');
socket.destroy();
});
socket.pipe(process.stdout);
socket.on('connect', function () {
console.log('Connected.');
//process.stdin.resume(); // already in flowing mode
process.stdin.setRawMode(true);
});
socket.on('close', function close() {
console.log('Disconnected.');
socket.removeListener('close', close);
});
var repl = require('repl');
var net = require('net');
net.createServer(function (socket) {
var r = repl.start({
prompt: 'socket ' + socket.remoteAddress + ':' + socket.remotePort + '> ',
input: socket,
output: socket,
terminal: true,
useGlobal: false
});
r.on('exit', function () {
socket.end();
});
r.context.socket = socket;
}).listen(2333);
@JigneshPansuriaDeveloper

I am getting socket is undefined error ..

@JigneshPansuriaDeveloper

var repl = require('repl')
var net = require('net')

net.createServer(function (socket) {
var r = repl.start({
prompt: 'socket '+socket.remoteAddress+':'+socket.remotePort+'> '
, input: socket
, output: socket
, terminal: true
, useGlobal: false
})
r.on('exit', function () {
socket.end()
})
r.context.socket = socket
}).listen(1337)

it is not entering into createServer() function ..

@hoegaarden
Copy link

Just wanted to let you know, that i stole your client ... i hope this is ok ... ?

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