Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Take a look at this article: <a href="http://howtonode.org/socket-io-auth" rel="nofollow">Session-based Authorization with Socket.IO</a></p> <p>Your code works fine, but need 2 improvements to do what you want (send session data to clients from server):</p> <ol> <li>it extracts <code>sessionID</code> during <strong>authorization</strong> only</li> <li>it extracts session data from store by this <code>sessionID</code> during <strong>connection</strong> where you can send data from server to clients in an interval.</li> </ol> <p>Here's the improved code:</p> <pre><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 += '&lt;p&gt;First time visiting? view this page in several browsers :)&lt;/p&gt;'; } res.send(body + '&lt;p&gt;viewed &lt;strong&gt;' + req.session.views + '&lt;/strong&gt; times.&lt;/p&gt;'); }); var sio = require('socket.io').listen(app.listen(3000)); sio.set('authorization', function (data, accept) { // check if there's a cookie header if (data.headers.cookie) { // if there is, parse the cookie var rawCookies = cookie.parse(data.headers.cookie); data.sessionID = connect.utils.parseSignedCookie(rawCookies['express.sid'],'secret'); // it checks if the session id is unsigned successfully if (data.sessionID == rawCookies['express.sid']) { accept('cookie is invalid', false); } } else { // if there isn't, turn down the connection with a message // and leave the function. return accept('No cookie transmitted.', false); } // accept the incoming connection accept(null, true); }); sio.sockets.on('connection', function (socket) { //console.log(socket); console.log('A socket with sessionID ' + socket.handshake.sessionID + ' connected!'); // it 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); }); }); </code></pre> <p>Then you can have a client page under <code>public/client.html</code> at <code>http://localhost:3000/client.html</code> to see the session data populated from <code>http://localhost:3000</code>:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script src="/socket.io/socket.io.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; 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 to connect Socket.IO', reason); }); tick.on('connect', function (){ console.info('successfully established a working and authorized connection'); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; Open the browser console to see tick-tocks! &lt;p&gt;This session is viewed &lt;b&gt;&lt;span id="views"&gt;&lt;/span&gt;&lt;/b&gt; times.&lt;/p&gt; &lt;/body&gt; </code></pre> <p></p>
    singulars
    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.
    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