index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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("/token", function(req,res) {
  30. var ts = new Date().getTime();
  31. var rand = Math.floor(Math.random()*9999999);
  32. var secret = ts.toString() + rand.toString();
  33. res.send({secret: secret, socketId: createHash(secret)});
  34. });
  35. var createHash = function(secret) {
  36. var cipher = crypto.createCipher('blowfish', secret);
  37. return(cipher.final('hex'));
  38. };
  39. // Actually listen
  40. app.listen(opts.port || null);