Skip to content

Instantly share code, notes, and snippets.

@sreekumar-kr
Forked from slickplaid/socket.js
Created July 27, 2012 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sreekumar-kr/3190441 to your computer and use it in GitHub Desktop.
Save sreekumar-kr/3190441 to your computer and use it in GitHub Desktop.
bored!
var visitors = 50;
var sockets = [];
(function($) {
$('body').append('<h1>Our Sockets: <span class="ourSockets">0</span></h1>');
socket.on('countChanged', autoVisit);
function autoVisit(data) {
if(data.count < visitors) {
addVisitor(1);
} else if(data.count > visitors) {
removeVisitor(1);
}
var ourClients = sockets.length;
$('.ourSockets').text(ourClients);
};
function addVisitor(count) {
sockets.push(io.connect('http://sreekumar.node.jit.su', {'force new connection':true}));
count--;
if(count > 0) {
addVisitor(count);
}
}
function removeVisitor(count) {
if(sockets.length === 0) return false;
var removed = sockets.pop();
removed.disconnect();
count--;
if(count > 0) {
removeVisitor(count);
}
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment