Skip to content

Instantly share code, notes, and snippets.

@lionep
Created August 28, 2015 21:37
Show Gist options
  • Save lionep/65cb587135edd9ff9110 to your computer and use it in GitHub Desktop.
Save lionep/65cb587135edd9ff9110 to your computer and use it in GitHub Desktop.
'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