Skip to content

Instantly share code, notes, and snippets.

@dshaw
Created July 8, 2010 17:30
Show Gist options
  • Select an option

  • Save dshaw/468335 to your computer and use it in GitHub Desktop.

Select an option

Save dshaw/468335 to your computer and use it in GitHub Desktop.
Quick and dirty psuedo-ish code for a Node.js Web Socket connection throttler
var maxThrottles = 10;
connectionManager[connection.id].lastMessage = +new Date();
connectionManager[connection.id].timeOutUntil = connectionManager[connection.id].lastMessage + 1000; // one second, too long?
connectionManager[connection.id].throttledCount = 0;
connection.addListener("message", function( message ) {
var messageTime = +new Date();
if (messageTime < connectionManager[connection.id].timeOutUntil) {
(throttleCount < maxThrottles) ? connectionManager[connection.id].throttledCount++ : kickMe(connection.id);
}
// call method on connection manager to update lastMessage and reset derivative values.
// continue handling message
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment