index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var express = require('express');
  2. var fs = require('fs');
  3. var io = require('socket.io');
  4. var crypto = require('crypto');
  5. var app = express.createServer();
  6. var staticDir = express.static;
  7. io = io.listen(app);
  8. var opts = {
  9. port: 1948,
  10. baseDir : __dirname + '/../../'
  11. };
  12. io.sockets.on('connection', function(socket) {
  13. socket.on('slidechanged', function(slideData) {
  14. if (typeof slideData.secret == 'undefined' || slideData.secret == null || slideData.secret === '') return;
  15. if (createHash(slideData.secret) === slideData.socketId) {
  16. slideData.secret = null;
  17. socket.broadcast.emit(slideData.socketId, slideData);
  18. };
  19. });
  20. });
  21. app.configure(function() {
  22. [ 'css', 'js', 'plugin', 'lib' ].forEach(function(dir) {
  23. app.use('/' + dir, staticDir(opts.baseDir + dir));
  24. });
  25. });
  26. app.get("/", function(req, res) {
  27. res.writeHead(200, {'Content-Type': 'text/html'});
  28. fs.createReadStream(opts.baseDir + '/index.html').pipe(res);
  29. });
  30. app.get("/token", function(req,res) {
  31. var ts = new Date().getTime();
  32. var rand = Math.floor(Math.random()*9999999);
  33. var secret = ts.toString() + rand.toString();
  34. res.send({secret: secret, socketId: createHash(secret)});
  35. });
  36. var createHash = function(secret) {
  37. var cipher = crypto.createCipher('blowfish', secret);
  38. return(cipher.final('hex'));
  39. };
  40. // Actually listen
  41. app.listen(opts.port || null);
  42. var brown = '\033[33m',
  43. green = '\033[32m',
  44. reset = '\033[0m';
  45. console.log( brown + "reveal.js:" + reset + " Multiplex running on port " + green + opts.port + reset );