index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 (createHash(slideData.secret) === slideData.socketId) {
  15. slideData.secret = null;
  16. socket.broadcast.emit(slideData.socketId, slideData);
  17. };
  18. });
  19. });
  20. app.configure(function() {
  21. [ 'css', 'js', 'plugin', 'lib' ].forEach(function(dir) {
  22. app.use('/' + dir, staticDir(opts.baseDir + dir));
  23. });
  24. });
  25. app.get("/", function(req, res) {
  26. fs.createReadStream(opts.baseDir + '/index.html').pipe(res);
  27. });
  28. app.get("/token", function(req,res) {
  29. var ts = new Date().getTime();
  30. var rand = Math.floor(Math.random()*9999999);
  31. var secret = ts.toString() + rand.toString();
  32. res.send({secret: secret, socketId: createHash(secret)});
  33. });
  34. var createHash = function(secret) {
  35. var cipher = crypto.createCipher('blowfish', secret);
  36. return(cipher.final('hex'));
  37. };
  38. // Actually listen
  39. app.listen(opts.port || null);
  40. var brown = '\033[33m',
  41. green = '\033[32m',
  42. reset = '\033[0m';
  43. console.log( brown + "reveal.js:" + reset + " Multiplex running on port " + green + opts.port + reset );