Skip to content

Instantly share code, notes, and snippets.

@timoxley
Last active December 18, 2015 09:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timoxley/5763982 to your computer and use it in GitHub Desktop.
Save timoxley/5763982 to your computer and use it in GitHub Desktop.
pausing a stream in shoejs
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