Skip to content

Commit

Permalink
[api] Working on the WebSocket spawning
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd-Eden committed Apr 4, 2013
1 parent 1f5f9b2 commit eced998
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
31 changes: 31 additions & 0 deletions bin/thor
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,35 @@ var cluster = require('cluster')
cluster.setupMaster({ exec: '../thunderbolt.js', silent: true });
while (workers--) cluster.fork();

Object.keys(cluster.workers).forEach(function each(id) {
var worker = cluster.workers[id];

worker.on('message', function message(data) {
switch (data.type) {
case 'open':
case 'close':
case 'error':
case 'message':
}
});
});

//
// Start.
//
var url = (cli.secure ? 'wss' : 'ws') +'://'+ cli.host +':'+ cli.port;
[
''
, 'Thor: version: '+ cli._version
, ''
, 'God of Thunder, son of Odin and smasher of WebSockets!'
, ''
, 'Thou shall:'
, '- Spawn '+ cli.workers +' workers.'
, '- Create '+ cli.concurrent + ' concurrent connections.'
, '- Smash '+ cli.amount +' connections with the mighty Mjölnir.'
, ''
, 'Prepare to suffer, '+ url + ' thy infidel.'
].forEach(function stdout(line) {
console.log(line);
});
31 changes: 28 additions & 3 deletions thunderbolt.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,44 @@ var Socket = require('ws')
, collection = [];

process.on('message', function message(task) {
var now = Date.now();

//
// Write a new message to the socket. The message should have a size of x
//
if ('write' in task) collection.forEach(function write(socket) {
var start = Date.now();
var start = socket.last = now;

socket.send(task.message, function sending(err) {
var duration = Date.now() - start;

if (err) process.send({ type: 'error', message: err.message });
});
});

if (task.shutdown) collection.forEach(function shutdown(socket) {
socket.close();
});

// End of the line, we are gonna start generating new connections.
if (!task.url) return;

var socket = new Socket(task.url);

socket.on('open', function open() {
process.send({ type: 'open', duration: Date.now() - now });
});

socket.on('message', function message(data) {
process.send({ type: 'message', latency: Date.now() - socket.last });
});

socket.on('close', function close() {
process.send({ type: 'close' });
});

socket.on('error', function error(err) {
process.send({ type: 'error', message: err.message });
});

// Adding a new socket to our socket collection.
collection.push(socket);
});

0 comments on commit eced998

Please sign in to comment.