References:
Last active
December 10, 2015 21:28
-
-
Save alvinsj/4495226 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <script src="http://localhost:3000/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('error', function (reason){ | |
| console.error('Unable to connect Socket.IO', reason); | |
| }); | |
| tick.on('connect', function (){ | |
| console.info('successfully established a working and authorized connection'); | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| Open the browser console to see tick-tocks! | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "nodechat", | |
| "description": "nodechat by node", | |
| "version": "0.0.1", | |
| "private": true, | |
| "dependencies": { | |
| "connect-redis": "1.x", | |
| "redis": "0.6.x", | |
| "express": "3.x", | |
| "express-namespace": "0.1.1", | |
| "cookie": "0.0.x", | |
| "connect": "2.7.x", | |
| "socket.io": "0.9.x", | |
| "mongoose": "3.5.x", | |
| "jade": "0.27.x" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Imports | |
| var io = require('socket.io') | |
| , http = require('http') | |
| , express = require('express') | |
| , cookie = require('cookie') | |
| , connect = require('connect') | |
| , RedisStore = require('connect-redis')(express) | |
| , session_store = new RedisStore(); | |
| // Create Express | |
| var app = express(); | |
| // Configure Express app with: | |
| // * Cookie parser | |
| // * Session manager | |
| app.configure(function () { | |
| app.use(express.cookieParser()); | |
| app.use(express.static('public')); | |
| app.use(express.session({ secret: 'top_secret', key: 'nodechat.ssid', store: session_store })); | |
| app.engine('jade', require('jade').__express); | |
| }); | |
| // Configture GET '/' to return index.html | |
| app.get('/', function (req, res) { | |
| res.sendfile(__dirname + '/public/index.html'); | |
| }); | |
| app.get('/test', function(req, res) { | |
| var old = req.session.email; | |
| req.session.email = "alvinsj@email.com"; | |
| req.session.user = "alvinsj"; | |
| req.session.save(); | |
| res.header('Content-Type', 'text/plain'); | |
| res.send("Email was '" + old + "', now is '" + req.session.email + "'."); | |
| }); | |
| // Create HTTP server on port 3000 and register socket.io as listener | |
| server = http.createServer(app) | |
| server.listen(3000); | |
| io = io.listen(server); | |
| // Configure global authorization handling. handshakeData will contain | |
| // the request data associated with the handshake request sent by | |
| // the socket.io client. 'accept' is a callback function used to either | |
| // accept or reject the connection attempt. | |
| // We will use the session id (attached to a cookie) to authorize the user. | |
| // in this case, if the handshake contains a valid session id, the user will be authorized. | |
| io.set('authorization', function (handshakeData, accept) { | |
| // check if there's a cookie header | |
| if (handshakeData.headers.cookie) { | |
| // if there is, parse the cookie | |
| handshakeData.cookie = cookie.parse(handshakeData.headers.cookie); | |
| // the cookie value should be signed using the secret configured above (see line 17). | |
| // use the secret to to decrypt the actual session id. | |
| if('nodechat.ssid' in handshakeData.cookie) { | |
| handshakeData.sessionID = connect.utils.parseSignedCookie(handshakeData.cookie['nodechat.ssid'], 'top_secret'); | |
| // if the session id matches the original value of the cookie, this means that | |
| // we failed to decrypt the value, and therefore it is a fake. | |
| if (handshakeData.cookie['nodechat.ssid'] == handshakeData.sessionID) { | |
| // reject the handshake | |
| console.log("=========== cookie decrypt failure"); | |
| return accept('Cookie is invalid.', false); | |
| }else { | |
| console.log("=========== cookie decrypt SUCCESS: "+handshakeData.sessionID); | |
| return accept(null, true); | |
| } | |
| } | |
| else { | |
| console.log("=========== session cookie not found"); | |
| return accept(null, false); | |
| } | |
| } else { | |
| // if there isn't, turn down the connection with a message | |
| // and leave the function. | |
| console.log("=========== no cookie"); | |
| return accept('No cookie transmitted.', false); | |
| } | |
| // accept the incoming connection | |
| accept(null, false); | |
| }); | |
| // upon connection, start a periodic task that emits (every 1s) the current timestamp | |
| io.on('connection', function (socket) { | |
| var sender = setInterval(function () { | |
| socket.emit('data', new Date().getTime()); | |
| session_store.get(socket.handshake.sessionID, function(err, session){ | |
| if(session){ | |
| socket.emit('user', session.user); | |
| socket.emit('email', session.email); | |
| } | |
| }); | |
| }, 1000) | |
| socket.on('disconnect', function() { | |
| clearInterval(sender); | |
| }) | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment