Last active
December 18, 2015 09:49
-
-
Save timoxley/5763982 to your computer and use it in GitHub Desktop.
pausing a stream in shoejs
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 http = require('http'); | |
var ecstatic = require('ecstatic')(__dirname + '/static'); | |
var shoe = require('../../'); | |
var through = require('through') | |
var net = require('net') | |
// Goal: | |
// pipe connection on port 10000 to port 9999 | |
// but do some async operation | |
// before doing so | |
net.createServer(function(socket) { | |
var pause = through().pause() | |
socket.pipe(pause) | |
setTimeout(function() { | |
pause.pipe(net.connect(9999)).pipe(socket) | |
pause.resume() | |
}, 1000) | |
}).listen(10000) | |
var server = http.createServer(ecstatic); | |
server.listen(9999); | |
var sock = shoe(function (stream) { | |
var iv = setInterval(function () { | |
stream.write(Math.floor(Math.random() * 2)); | |
}, 250); | |
stream.on('end', function () { | |
clearInterval(iv); | |
}); | |
stream.pipe(process.stdout, { end : false }); | |
}); | |
sock.install(server, '/invert'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment