Created
August 28, 2015 21:37
-
-
Save lionep/65cb587135edd9ff9110 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
var net = require('net'); | |
var kue = require('kue'); | |
var app = {}; | |
app.$queue = kue.createQueue(); | |
app.$queue.process('stream-demo', function(job, done) { | |
var ti; | |
var randomSocketName = 'kue.' + Math.floor(Math.random() * 100000) + '.' + job.id + '.sock'; | |
var oneClient = false; | |
var clientGone = false; | |
var server = net.createServer(function(c) { | |
if (oneClient) { | |
console.log('Another client is trying to connect...'); | |
c.end(); | |
return; | |
} | |
oneClient = true; | |
console.log('client connected'); | |
c.on('end', function() { | |
console.log('client disconnected'); | |
clientGone = true; | |
clearTimeout(ti); | |
server.close(); | |
}); | |
// Simulate write | |
ti = setInterval(function() { | |
c.write('toto'); | |
}, 10); | |
// Simulate end of stream... | |
setTimeout(function() { | |
if (clientGone) return; | |
clearTimeout(ti); | |
c.end(); | |
}, 20000); | |
}).listen('/tmp/' + randomSocketName, function() { | |
done(null, '/tmp/' + randomSocketName); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment