75838de6eeb070e3d52fb9810e70f9890cce0435
[functest.git] / docs / com / plugin / notes-server / index.js
1 var http      = require('http');
2 var express   = require('express');
3 var fs        = require('fs');
4 var io        = require('socket.io');
5 var _         = require('underscore');
6 var Mustache  = require('mustache');
7
8 var app       = express();
9 var staticDir = express.static;
10 var server    = http.createServer(app);
11
12 io = io(server);
13
14 var opts = {
15         port :      1947,
16         baseDir :   __dirname + '/../../'
17 };
18
19 io.on( 'connection', function( socket ) {
20
21         socket.on( 'new-subscriber', function( data ) {
22                 socket.broadcast.emit( 'new-subscriber', data );
23         });
24
25         socket.on( 'statechanged', function( data ) {
26                 socket.broadcast.emit( 'statechanged', data );
27         });
28
29 });
30
31 [ 'css', 'js', 'images', 'plugin', 'lib' ].forEach( function( dir ) {
32         app.use( '/' + dir, staticDir( opts.baseDir + dir ) );
33 });
34
35 app.get('/', function( req, res ) {
36
37         res.writeHead( 200, { 'Content-Type': 'text/html' } );
38         fs.createReadStream( opts.baseDir + '/index.html' ).pipe( res );
39
40 });
41
42 app.get( '/notes/:socketId', function( req, res ) {
43
44         fs.readFile( opts.baseDir + 'plugin/notes-server/notes.html', function( err, data ) {
45                 res.send( Mustache.to_html( data.toString(), {
46                         socketId : req.params.socketId
47                 }));
48         });
49
50 });
51
52 // Actually listen
53 server.listen( opts.port || null );
54
55 var brown = '\033[33m',
56         green = '\033[32m',
57         reset = '\033[0m';
58
59 var slidesLocation = 'http://localhost' + ( opts.port ? ( ':' + opts.port ) : '' );
60
61 console.log( brown + 'reveal.js - Speaker Notes' + reset );
62 console.log( '1. Open the slides at ' + green + slidesLocation + reset );
63 console.log( '2. Click on the link your JS console to go to the notes page' );
64 console.log( '3. Advance through your slides and your notes will advance automatically' );