Skip to content

Instantly share code, notes, and snippets.

@dominictarr
Created August 1, 2013 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominictarr/6135418 to your computer and use it in GitHub Desktop.
Save dominictarr/6135418 to your computer and use it in GitHub Desktop.
does server.on('close') happen after all streams have closed? or when you call close
var net = require('net')
var port = 89899
var streams = 0
var server = net.createServer(function (stream) {
var i = 0
var int = setInterval(function () {
stream.write( i++ + '\n')
if(i > 5) {
stream.end()
clearInterval(int)
}
}, 1000)
streams ++
stream.on('close', function () {
streams --
if(streams === 0)
server.emit('quiet')
})
}).listen(port, function () {
console.log('CONNECT')
net.connect(port).on('data',console.log)
})
setTimeout(function () {
console.log('please close')
server.close(function () {
console.log('CLOSE')
})
}, 1000)
server.on('quiet', function () {
console.log('QUIET')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment