Skip to content

Instantly share code, notes, and snippets.

@fabware
Forked from jslatts/gist:1008072
Created June 5, 2011 12:33
Show Gist options
  • Save fabware/1008924 to your computer and use it in GitHub Desktop.
Save fabware/1008924 to your computer and use it in GitHub Desktop.
Example of using connect session cookies with socket.io
socket.on('connection', function(client){
// helper function that goes inside your socket connection
client.connectSession = function(fn) {
if (!client.request || !client.request.headers || !client.request.headers.cookie) {
disconnectAndRedirectClient(client,function() {
console.log('Null request/header/cookie!');
});
return;
}
console.log('Cookie is' + client.request.headers.cookie);
var match = client.request.headers.cookie.match(/connect\.sid=([^;]+)/);
if (!match || match.length < 2) {
disconnectAndRedirectClient(client,function() {
console.log('Failed to find connect.sid in cookie')
});
return;
}
var sid = unescape(match[1]);
rc.get(sid, function(err, data) {
fn(err, JSON.parse(data));
});
};
client.connectSession(function(err, data) {
if(err) {
console.log('Error on connectionSession: ' + err);
return;
}
var connectedUser = getConnectedUser(data, client);
if(connectedUser) {
client.on('message', function(msg){message(client, socket, msg)});
sendInitialDataToClient(client);
}
else
console.log("Failed to connect user");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment