index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. console.log(slideData);
  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. fs.createReadStream(opts.baseDir + '/index.html').pipe(res);
  28. });
  29. app.get("/fixed.html", function(req, res) {
  30. fs.createReadStream(opts.baseDir + '/fixed.html').pipe(res);
  31. });
  32. app.get("/fixedmaster.html", function(req, res) {
  33. fs.createReadStream(opts.baseDir + '/fixedmaster.html').pipe(res);
  34. });
  35. app.get("/token", function(req,res) {
  36. var ts = new Date().getTime();
  37. var rand = Math.floor(Math.random()*9999999);
  38. var secret = ts.toString() + rand.toString();
  39. res.send({secret: secret, socketId: createHash(secret)});
  40. });
  41. var createHash = function(secret) {
  42. var cipher = crypto.createCipher('blowfish', secret);
  43. return(cipher.final('hex'));
  44. };
  45. // Actually listen
  46. app.listen(opts.port || null);