Skip to content

Instantly share code, notes, and snippets.

@jslatts
Created June 4, 2011 17:06
Show Gist options
  • Save jslatts/1008072 to your computer and use it in GitHub Desktop.
Save jslatts/1008072 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");
});
});
@tegila
Copy link

tegila commented Aug 26, 2015

rc.get(sid, function(err, data) { what is rc ?

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