javascript - express.session.MemoryStore not returning session? -


it easy setting sessions , using them in php. website need deal websockets. facing problem set sessions in node.js. can push data without using sessions , work fine when more 1 tab opened new socket.id created , opened tabs wont function properly. have been working on sessions , had problem accessing sessionstore, logging session not grabbed. have tried session.load no luck

how session object , use in way opening other tabs wouldnt effect functionality , push data server client on tabs?

var express=require('express'); var http = require('http'); var io = require('socket.io'); var cookie = require("cookie"); var connect = require("connect"), memorystore = express.session.memorystore, sessionstore = new memorystore();  var app = express();  app.configure(function () {     app.use(express.cookieparser());     app.use(express.session({store: sessionstore         , secret: 'secret'         , key: 'express.sid'}));     app.use(function (req, res) {         res.end('<h2>hello, session id ' + req.sessionid + '</h2>');     }); });  server = http.createserver(app); server.listen(3000);  sio = io.listen(server);  var session = require('connect').middleware.session.session;  sio.set('authorization', function (data, accept) {     // check if there's cookie header     if (data.headers.cookie) {         // if there is, parse cookie         data.cookie = connect.utils.parsesignedcookies(cookie.parse(data.headers.cookie),'secret');         // note need use same key grad         // session id, specified in express setup.         data.sessionid = data.cookie['express.sid'];         sessionstore.get(data.sessionid, function (err, session) {             if (err || !session) {                 // if cannot grab session, turn down connection                 console.log("session not grabbed");                 accept('error', false);             } else {                 // save session data , accept connection                 console.log("session grabbed");                 data.session = session;                 accept(null, true);             }         });     } else {        // if there isn't, turn down connection message        // , leave function.        return accept('no cookie transmitted.', false);     }     // accept incoming connection     accept(null, true); });  sio.sockets.on('connection', function (socket) {     console.log('a socket sessionid ' + socket.handshake.sessionid          + ' connected!'); }); 

take @ article: session-based authorization socket.io

your code works fine, need 2 improvements want (send session data clients server):

  1. it extracts sessionid during authorization only
  2. it extracts session data store sessionid during connection can send data server clients in interval.

here's improved code:

var express = require('express'); var connect = require('connect'); var cookie = require('cookie'); var sessionstore = new express.session.memorystore();      var app = express();      app.use(express.logger('dev')); app.use(express.cookieparser()); app.use(express.session({store: sessionstore, secret: "secret", key: 'express.sid'})); // web page app.use(express.static('public'));      app.get('/', function(req, res) {   var body = '';   if (req.session.views) {     ++req.session.views;   } else {     req.session.views = 1;     body += '<p>first time visiting? view page in several browsers :)</p>';   }   res.send(body + '<p>viewed <strong>' + req.session.views + '</strong> times.</p>'); });      var sio = require('socket.io').listen(app.listen(3000));     sio.set('authorization', function (data, accept) {     // check if there's cookie header     if (data.headers.cookie) {         // if there is, parse cookie         var rawcookies = cookie.parse(data.headers.cookie);         data.sessionid = connect.utils.parsesignedcookie(rawcookies['express.sid'],'secret');             // checks if session id unsigned         if (data.sessionid == rawcookies['express.sid']) {             accept('cookie invalid', false);         }     } else {        // if there isn't, turn down connection message        // , leave function.        return accept('no cookie transmitted.', false);     }     // accept incoming connection     accept(null, true); });      sio.sockets.on('connection', function (socket) {     //console.log(socket);     console.log('a socket sessionid ' + socket.handshake.sessionid + ' connected!');     // sets data every 5 seconds     var handle = setinterval(function() {         sessionstore.get(socket.handshake.sessionid, function (err, data) {             if (err || !data) {                 console.log('no session data yet');             } else {                 socket.emit('views', data);             }         });     }, 5000);          socket.on('disconnect', function() {         clearinterval(handle);     }); }); 

then can have client page under public/client.html @ http://localhost:3000/client.html see session data populated http://localhost:3000:

<html> <head>     <script src="/socket.io/socket.io.js" type="text/javascript"></script>     <script type="text/javascript">         tick = io.connect('http://localhost:3000/');         tick.on('data', function (data) {             console.log(data);         });          tick.on('views', function (data) {             document.getelementbyid('views').innertext = data.views;         });          tick.on('error', function (reason){           console.error('unable connect socket.io', reason);         });          tick.on('connect', function (){           console.info('successfully established working , authorized connection');         });     </script> </head> <body>     open browser console see tick-tocks!     <p>this session viewed <b><span id="views"></span></b> times.</p> </body> 


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo