Created
September 5, 2014 02:18
-
-
Save hagsteel/8719f4bb18fc2d208878 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
/* | |
* A rather simple way of testing out SwampDragon. | |
* Create a load of connections, connect, subscriber, update and count the number | |
* of published messages. | |
* | |
* SETUP: | |
* 1. npm install sockjs-client-node | |
* 2. curl https://raw.githubusercontent.com/jonashagstedt/swampdragon/master/swampdragon/static/swampdragon/js/swampdragon.js > swampdragon.js | |
* 3. Set router_name to the name of your router. | |
* 4. Change "sd.update_object(router_name, { value: val }, 'foo');" to call | |
* your router with create or update (one that triggers a publish) | |
* | |
* TO RUN: | |
* cat swampdragon.js test.js|node | |
* | |
**************************************************************************/ | |
var SockJS = require('sockjs-client-node'); | |
window = {}; // Adding this so it works with Node | |
var number_of_sockets = 100; | |
var _dragons = []; | |
var url = 'http://localhost:9999'; | |
var message_count = 0; | |
var channel_message_count = 0; | |
var timestamp = null; | |
var sub_count = 0; | |
var router_name = ''; | |
// Once a connection is open, open a new one, | |
// until we have have as many as number_of_sockets | |
function open(dragon) { | |
if (_dragons.length < number_of_sockets) { | |
connectDragon(); | |
} else { | |
doneConnecting(); | |
} | |
} | |
// Increment the number of messages received | |
function onSDMessage(a) { | |
message_count++; | |
} | |
// Increment a counter every time one of the connections | |
// receive a broadcast message | |
function incrementChannelMessage() { | |
channel_message_count++; | |
console.log('Messages received: ' + channel_message_count); | |
} | |
// Connect | |
function connectDragon() { | |
var sd = new SwampDragon({onopen: open, onmessage: onSDMessage, onchannelmessage: incrementChannelMessage}); | |
sd._id = _dragons.length; | |
_dragons.push(sd); | |
sd.connect(url, 'data') | |
} | |
// Send all messages | |
function sendAll(no_repeat) { | |
timestamp = new Date().getTime(); | |
for (var i in _dragons) { | |
var sd = _dragons[i]; | |
var val = parseInt(Math.random() * 1000); | |
sd.update_object(router_name, { value: val }, 'foo'); | |
} | |
console.log('messages sent'); | |
} | |
// Subscribe all connections to a channel | |
function subscribeAll() { | |
for (var i in _dragons) { | |
var sd = _dragons[i]; | |
sd.on('callback_sub', function (context, data) { | |
sub_count++; | |
if (sub_count >= _dragons.length) { | |
doneSubscribing(); | |
} | |
}); | |
sd.subscribe(router_name, {}, 'callback_sub', 'client-chan'); | |
} | |
} | |
// All the connections are now open | |
function doneConnecting() { | |
console.log("done subscribing to channels"); | |
subscribeAll(); | |
} | |
// All the connections are now subscribed to a channel | |
function doneSubscribing() { | |
console.log("sending..."); | |
sendAll(); | |
} | |
console.log("--- Adding sockets ---"); | |
connectDragon(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment