Skip to content

Instantly share code, notes, and snippets.

@mikaelbr
Last active August 29, 2015 14:07
Show Gist options
  • Save mikaelbr/e602b7163fa666c63b71 to your computer and use it in GitHub Desktop.
Save mikaelbr/e602b7163fa666c63b71 to your computer and use it in GitHub Desktop.
Duplex stream chat through TCP
var net = require('net');
var port = 8001;
var socket = new net.Socket();
socket.connect(port, function () {
console.log("Connected to", port);
});
process.stdin.pipe(socket).pipe(process.stdout);
var net = require('net');
var through = require('through2');
var port = 8001;
net.createServer(function (connection) {
process.stdin
.pipe(connection)
.pipe(through(prefix))
.pipe(connection)
.pipe(process.stdout);
process.stdin.pipe(through(prefix)).pipe(process.stdout);
}).listen(port, function () {
console.log('Listening on port', port);
});
function prefix (data, enc, cb) {
this.push(new Buffer('You said: ' + data.toString()));
cb();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment