index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var http = require('http');
  2. var express = require('express');
  3. var fs = require('fs');
  4. var io = require('socket.io');
  5. var crypto = require('crypto');
  6. var app = express();
  7. var staticDir = express.static;
  8. var server = http.createServer(app);
  9. io = io(server);
  10. var opts = {
  11. port: process.env.PORT || 1948,
  12. baseDir : __dirname + '/../../'
  13. };
  14. io.on( 'connection', function( socket ) {
  15. socket.on('multiplex-statechanged', function(data) {
  16. if (typeof data.secret == 'undefined' || data.secret == null || data.secret === '') return;
  17. if (createHash(data.secret) === data.socketId) {
  18. data.secret = null;
  19. socket.broadcast.emit(data.socketId, data);
  20. };
  21. });
  22. });
  23. [ 'css', 'js', 'plugin', 'lib' ].forEach(function(dir) {
  24. app.use('/' + dir, staticDir(opts.baseDir + dir));
  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. server.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 );