Created
June 4, 2011 17:06
-
-
Save jslatts/1008072 to your computer and use it in GitHub Desktop.
Example of using connect session cookies with socket.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
rc.get(sid, function(err, data) {
what is rc ?