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