Skip to content

Instantly share code, notes, and snippets.

@jslatts
Created April 4, 2011 23:36
Show Gist options
  • Save jslatts/902715 to your computer and use it in GitHub Desktop.
Save jslatts/902715 to your computer and use it in GitHub Desktop.
Calling functions for redis callback example
function purgatory() {
var inPurgatory = true;
return {
tryToGetOut: function (message, client, cb) {
auth.authenticateUserByHash(message.user, message.hash, function(err, data) {
if (err) {
winston.info('[purgatory] Bad auth. Client still in purgatory');
inPurgatory = true;
}
else {
winston.info('[purgatory] out of purgatory');
inPurgatory = false;
//Once we are sure the client is who s/he claims to be, attach name and hash for future use.
client.user = message.user;
client.hash = message.hash;
if (cb) {
return cb();
}
}
});
}
, stillInPurgatory: function() {
winston.info('[purgatory] status ' + inPurgatory);
return inPurgatory;
}
}
}
// Handle the new connection event for socket by putting the client in purgatory until they auth.
// TODO - figure out how to clear purgatory listener
socket.on('connection', function (client) {
var clientPurgatory = purgatory();
client.socket = socket; //Once in awhile, we want to reference the socket for broadcasts
client.on('message', function(message) {
if (clientPurgatory.stillInPurgatory() && message.event === 'clientauthrequest') {
//If we can get out of purgatory, set up the client for pubsub
clientPurgatory.tryToGetOut(message, client, function () {
channelmanager.setupClientForSubscriptions(client, function () {
winston.info('Client ' + client.sessionId + ' setup for pub/sub');
});
channelmanager.subscribeClientToChannel(client, 'main', function (){
winston.info('Client ' + client.sessionId + ' subcribed to main topic');
});
usermanager.newUserConnection(client, function() {
winston.info('Client ' + client.sessionId + ' connection setup.');
});
client.on('disconnect', function () {
clientDisconnect(client);
});
});
}
else {
messagerouter.handleMessage(message, client, function (err, data) {});
}
});
});
@jslatts
Copy link
Author

jslatts commented Apr 4, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment