Created
June 28, 2011 08:30
-
-
Save alfredwesterveld/1050729 to your computer and use it in GitHub Desktop.
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
var express = require('express'), | |
app = express.createServer(), | |
sio = require('socket.io'), | |
redis = require("redis"), | |
client = redis.createClient(), | |
io = null; | |
/** | |
* Used to parse cookie | |
*/ | |
function parse_cookies(_cookies) { | |
var cookies = {}; | |
_cookies && _cookies.split(';').forEach(function( cookie ) { | |
var parts = cookie.split('='); | |
cookies[ parts[ 0 ].trim() ] = ( parts[ 1 ] || '' ).trim(); | |
}); | |
return cookies; | |
} | |
app.listen(3000, "localhost"); | |
io = sio.listen(app); | |
io.configure(function () { | |
function auth (data, fn) { | |
var cookies = parse_cookies(data.headers.cookie); | |
console.log('PHPSESSID: ' + cookies.PHPSESSID); | |
client.sismember('sid', cookies.PHPSESSID, function (err , reply) { | |
fn(null, reply); | |
}); | |
}; | |
io.set('authorization', auth); | |
}); | |
io.sockets.on('connection', function (socket) { | |
socket.emit('access', 'granted'); | |
}); |
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
<?php | |
session_start(); | |
echo "<h1>SID: " . session_id() . "</h1>"; | |
?> | |
<html> | |
<head> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> | |
<script src="http://localhost:3000/socket.io/socket.io.js"></script> | |
</head> | |
<body> | |
<p id="text">access denied</p> | |
<script> | |
var socket = io.connect('http://localhost:3000/'); | |
socket.on('access', function (data) { | |
$("#text").html(data); | |
}); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment