Last active
August 29, 2015 14:07
-
-
Save mikaelbr/e602b7163fa666c63b71 to your computer and use it in GitHub Desktop.
Duplex stream chat through TCP
This file contains 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 characters
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); |
This file contains 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 characters
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