Note that there are some explanatory texts on larger screens.

plurals
  1. POnode.js / socket.io, cookies only working locally
    primarykey
    data
    text
    <p>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...</p> <p>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...</p> <p>The error I see in Chrome is:</p> <blockquote> <p>socket.io.js:1632GET <a href="http://192.168.0.6:8080/socket.io/1/?t=1334431940273" rel="noreferrer">http://192.168.0.6:8080/socket.io/1/?t=1334431940273</a> 500 (Internal Server Error)</p> <p>Socket.handshake ------- socket.io.js:1632</p> <p>Socket.connect ------- socket.io.js:1671</p> <p>Socket ------- socket.io.js:1530</p> <p>io.connect ------- socket.io.js:91</p> <p>(anonymous function) ------- /socket-test/:9</p> <p>jQuery.extend.ready ------- jquery.js:438</p> </blockquote> <p>And in the console for the server I see:</p> <blockquote> <p>debug - served static content /socket.io.js</p> <p>debug - authorized</p> <p>warn - handshake error No cookie</p> </blockquote> <p>My server is:</p> <pre><code>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); }); }); </code></pre> <p>My page JS is:</p> <pre><code>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: '' }); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload