I'm trying to use cookie based sessions, however it'll only work on the local machine, not over the network. If I remove the session related stuff, it will however work just great over the network...
You'll have to forgive the lack of quality code here, I'm just starting out with node/socket etc etc, and finding any clear guides is tough going, so I'm in n00b territory right now. Basically this is so far hacked together from various snippets with about 10% understanding of what I'm actually doing...
The error I see in Chrome is:
socket.io.js:1632GET http://192.168.0.6:8080/socket.io/1/?t=1334431940273 500 (Internal Server Error)
Socket.handshake ------- socket.io.js:1632
Socket.connect ------- socket.io.js:1671
Socket ------- socket.io.js:1530
io.connect ------- socket.io.js:91
(anonymous function) ------- /socket-test/:9
jQuery.extend.ready ------- jquery.js:438
And in the console for the server I see:
debug - served static content /socket.io.js
debug - authorized
warn - handshake error No cookie
My server is:
var express = require('express')
, app = express.createServer()
, io = require('socket.io').listen(app)
, connect = require('express/node_modules/connect')
, parseCookie = connect.utils.parseCookie
, RedisStore = require('connect-redis')(express)
, sessionStore = new RedisStore();
app.listen(8080, '192.168.0.6');
app.configure(function()
{
app.use(express.cookieParser());
app.use(express.session(
{
secret: 'YOURSOOPERSEKRITKEY',
store: sessionStore
}));
});
io.configure(function()
{
io.set('authorization', function(data, callback)
{
if(data.headers.cookie)
{
var cookie = parseCookie(data.headers.cookie);
sessionStore.get(cookie['connect.sid'], function(err, session)
{
if(err || !session)
{
callback('Error', false);
}
else
{
data.session = session;
callback(null, true);
}
});
}
else
{
callback('No cookie', false);
}
});
});
var users_count = 0;
io.sockets.on('connection', function (socket)
{
console.log('New Connection');
var session = socket.handshake.session;
++users_count;
io.sockets.emit('users_count', users_count);
socket.on('something', function(data)
{
io.sockets.emit('doing_something', data['data']);
});
socket.on('disconnect', function()
{
--users_count;
io.sockets.emit('users_count', users_count);
});
});
My page JS is:
jQuery(function($){
var socket = io.connect('http://192.168.0.6', { port: 8080 } );
socket.on('users_count', function(data)
{
$('#client_count').text(data);
});
socket.on('doing_something', function(data)
{
if(data == '')
{
window.setTimeout(function()
{
$('#target').text(data);
}, 3000);
}
else
{
$('#target').text(data);
}
});
$('#textbox').keydown(function()
{
socket.emit('something', { data: 'typing' });
});
$('#textbox').keyup(function()
{
socket.emit('something', { data: '' });
});
});