Note that there are some explanatory texts on larger screens.

plurals
  1. POnode, socket.io - authorization fail
    text
    copied!<p>This is a skeleton of my app.js file.<br> What I don't understand is way it doesn't read and print "log authorisation". I followed this <a href="https://github.com/LearnBoost/socket.io/wiki/Authorizing" rel="nofollow">https://github.com/LearnBoost/socket.io/wiki/Authorizing</a></p> <pre><code>var express = require('express'); mongoose = require('mongoose'); var MongoStore = require('connect-mongo')(express); app = express(); // costants var SITE_SECRET = 'xxx'; // mongoose mongoose.connect('mongodb://127.0.0.1/test'); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function callback () { console.log('Mongoose connection is now opened'); }); // pass same objects from Express to Socket.IO so they match var parseCookie = express.cookieParser(SITE_SECRET); var store = new MongoStore({ mongoose_connection: mongoose.connection, db: mongoose.connections[0].db }); app.configure(function(){ app.set('host', '127.0.0.1'); app.set('port', 1111); // init cookie-session app.use(parseCookie); app.use(express.session({ secret : SITE_SECRET ,store : store ,cookie: { maxAge: new Date(Date.now() + (1000*60*60*24*30*12)) } })); }); // create and start server var server = require('http').createServer(app) server.listen(app.get('port')); // socket.io var io = require('socket.io').listen(8080); io.configure(function() { console.log('log configure &lt;-- it read this. ok!'); io.set('authorization', function(handshake, callback) { console.log("log authorisation &lt;-- but it doesn't read this. why??"); if (handshake.headers.cookie) { parseCookie(handshake, null, function() { handshake.sessionID = handshake.signedCookies['connect.sid']; store.get(handshake.sessionID, function(err, session) { callback(null, true); }); }); } else { // they client has no session yet, don't let them connect callback('No session.', false); } }); }); clients = {}; io.sockets.on('connection', function (socket) { // save to a global object console.log('save session'); var session = socket.handshake.sessionID; clients[session] = socket; socket.on('disconnect', function() { console.log('remove session'); delete clients[session]; }); }); // routes app.get('/on', function(req, res){ var socket = clients[req.sessionID]; socket.on('test', function (data) { console.log(data); }); }); app.get('/emit', function(req, res){ var socket = clients[req.sessionID]; socket.emit('test', "ciao"); console.log("emit"); }); </code></pre> <p>when I start the server on the terminal I see this:</p> <pre><code>starting `node app.js` Mongoose connection is now opened info - socket.io started log configure </code></pre> <p>And if I load the page "/on" the error is:</p> <pre><code>TypeError: Cannot call method 'on' of undefined </code></pre> <p>I'm stack here from too much... What's wrong in this?<br> Maybe someone had the same problem...</p>
 

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