node.js - express-session, read vars on socket.io interaction -


i making chat customers of web page, express.js, , socket.io, , i'm trying manage sessions express-session, problem is, how read session values on socket. here's part of code.

thanks :)

var express         = require('express'); var app             = express(); var http            = require('http').server(app); var io              = require('socket.io')(http); var bodyparser      = require('body-parser'); var session         = require('express-session'); var shortid         = require('shortid');  app.use(express.static('public')); app.use( session({         secret: 'dont move',         resave: true,         saveuninitialized: true     }) ); app.use( bodyparser.urlencoded({ extended: true }) ); app.post('/chat', function(req, res){         var sess = req.session;         sess.ssid = shortid.generate();         res.render('chat', { name: "name" }); }); io.on('connection', function(socket){     socket.on('chat message', function( data ){         if( data.msg.trim() != '' ){             console.log('message: ' + data.msg);             //here want read ssid session var, can manage many chats @ same time         }     });     socket.on('disconnect', function(){         console.log('hey, disconected!');     }); }); http.listen(3000, function(){     console.log('listening on *:3000'); }); 

you can configure socket authorization

var express         = require('express'); var app             = express(); var http            = require('http').server(app); var io              = require('socket.io')(http); var bodyparser      = require('body-parser'); var session         = require('express-session'); var shortid         = require('shortid');  app.use(express.static('public')); app.use( session({         secret: 'dont move',         resave: true,         saveuninitialized: true     }) ); app.use( bodyparser.urlencoded({ extended: true }) ); app.post('/chat', function(req, res){         var sess = req.session;         sess.ssid = shortid.generate();         res.render('chat', { name: "name" }); });  var util = require('util'; var connect = require('connect'); var parsesignedcookie = connect.utils.parsesignedcookie;  io.configure(function () {   io.set('authorization', function (data, callback) {     if(data.headers.cookie) {       data.cookie = cookie.parse(data.headers.cookie);       data.sessionid = parsesignedcookie(data.cookie['ssid'], 'dont move');     }     callback(null, true);   });    io.on('connection', function(socket) {     var sessionid    = socket.handshake.sessionid;     socket.on('chat message', function( data ){         if( data.msg.trim() != '' ){             console.log('message: ' + data.msg);             // can use `sessionid`         }     });     socket.on('disconnect', function(){         console.log('hey, disconected!');     });   }); });  http.listen(3000, function(){     console.log('listening on *:3000'); }); 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -