1

I have been implementing the socket concept as in the https://github.com/techpines/express.io/tree/master/examples/sessions, I am able to display the session data that is stored once the page is initialised. But nevertheless , when a session data keeps on changing over an interval , i get that session data emitted as undefined..

But similar concept is well done using socket.io authorization & handshake session provided the link http://www.danielbaulig.de/socket-ioexpress/

Client Side code :

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect();

  // Emit ready event.
  setInterval(function(){
    socket.emit('ready', 'test');
  },2000);

  // Listen for get-feelings event.
  socket.on('get-feelings', function () {
  socket.emit('send-feelings', 'Good');
  })

  // Listen for session event.
  socket.on('session', function(data) {
  document.getElementById('count').value = data.count;
  })
</script>

<input type="text" id="count" />

Server Side Code :

express = require('express.io')
app = express().http().io()

// Setup your sessions, just like normal.
app.use(express.cookieParser())
app.use(express.session({secret: 'monkey'}))

// Session is automatically setup on initial request.
app.get('/', function(req, res) {
    var i = 0;
setInterval(function(){
   req.session.count = i++;
   console.log('Count Incremented as '+ req.session.count);
},1000);

res.sendfile(__dirname + '/client.html')
})

// Setup a route for the ready event, and add session data.
app.io.route('ready', function(req) {

 req.session.reload( function () {
        // "touch" it (resetting maxAge and lastAccess)
        // and save it back again.
        req.session.touch().save(function() {
           req.io.emit('get-feelings')
        });
    });
 /*
req.session.save(function() {
    req.io.emit('get-feelings')
})*/
})

// Send back the session data.
app.io.route('send-feelings', function(req) {
 console.log('Count Emitted as '+ req.session.count); // it is undefined in console

 req.session.reload( function () {
        // "touch" it (resetting maxAge and lastAccess)
        // and save it back again.
        req.session.save(function() {
          req.io.emit('session', req.session)
      });
    });

})

app.listen(7076);

in console , it is printed as undefined in every emit ... i want to why socket session is not updated as the original user session keeps changing ... ???

Do i need to put any extra configuaration in the server side to handle changing session data ???

user2749751
  • 405
  • 7
  • 22

0 Answers0