Created
December 8, 2014 09:03
-
-
Save lpinca/a60966aed7f25efcf55a to your computer and use it in GitHub Desktop.
Primus GH-321
This file contains hidden or 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
{ | |
"name": "gh-321", | |
"version": "1.0.0", | |
"description": "", | |
"main": "server.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "MIT", | |
"dependencies": { | |
"async": "^0.9.0", | |
"faye-websocket": "^0.8.1", | |
"primus": "^2.4.12" | |
} | |
} |
This file contains hidden or 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 Primus = require('primus') | |
, http = require('http'); | |
var server = http.createServer(); | |
var primus = new Primus(server, { transformer: 'faye' }); | |
primus.on('connection', function connection(spark) { | |
spark.on('data', function echo(data) { | |
spark.write(data); | |
}); | |
}); | |
server.listen(3000, function listening() { | |
var bound = server.address(); | |
console.log('listening on %s:%s', bound.address, bound.port); | |
}); |
This file contains hidden or 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 Primus = require('primus') | |
, async = require('async'); | |
var Socket = Primus.createSocket({ transformer: 'faye' }); | |
function test(messages, done) { | |
var responses = 0 | |
, i; | |
(function connect() { | |
var primus = new Socket('http://localhost:3000'); | |
primus.on('open', function open() { | |
for (i = 0; i < messages - responses; i++) primus.write('data'); | |
}); | |
primus.on('data', function message() { | |
if (++responses === messages) primus.end(); | |
}); | |
primus.on('end', function close() { | |
if (responses === messages) return done(); | |
connect(); | |
}); | |
})(); | |
} | |
var queue = async.queue(function worker(task, done) { | |
test(100, done); | |
}, 50); | |
for (var i = 0; i < 10000; i++) { | |
queue.push(); | |
} | |
queue.drain = function drain() { | |
console.log('bye.'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment