8

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: '' });
    });

});
1
  • I am having the same problem, however, only when using Chrome. My app works fine in Safari. Do you have the same situation? I'm not sure what the solution is.
    – WindChimes
    Commented Jan 23, 2015 at 14:29

1 Answer 1

1

Check your system clock, if the server and client have different clocks, the cookie may be immediately expiring right after you set it.

2
  • 1
    Checked the clocks and they seem fine - cookies are usually fine with other stuff too Commented May 6, 2012 at 18:33
  • Well it seems that data.headers.cookie does not exist. Can you dump the request headers to see what is going on?
    – kcbanner
    Commented May 6, 2012 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.