From c1a7e83d75a917307320acfdcb87a5702109e979 Mon Sep 17 00:00:00 2001 From: Hakim El Hattab Date: Tue, 29 Sep 2015 10:38:34 +0200 Subject: [PATCH] multiplexing work with socket.io 1.0+ #1281 #1276 --- plugin/multiplex/client.js | 2 +- plugin/multiplex/index.js | 28 +++++++++---------- plugin/multiplex/master.js | 56 ++++++++++++-------------------------- 3 files changed, 33 insertions(+), 53 deletions(-) diff --git a/plugin/multiplex/client.js b/plugin/multiplex/client.js index e6179f6..3ffd1e0 100644 --- a/plugin/multiplex/client.js +++ b/plugin/multiplex/client.js @@ -8,6 +8,6 @@ if (data.socketId !== socketId) { return; } if( window.location.host === 'localhost:1947' ) return; - Reveal.slide(data.indexh, data.indexv, data.indexf, 'remote'); + Reveal.setState(data.state); }); }()); diff --git a/plugin/multiplex/index.js b/plugin/multiplex/index.js index af058ed..40c1661 100644 --- a/plugin/multiplex/index.js +++ b/plugin/multiplex/index.js @@ -1,32 +1,32 @@ +var http = require('http'); var express = require('express'); var fs = require('fs'); var io = require('socket.io'); var crypto = require('crypto'); -var app = express.createServer(); -var staticDir = express.static; +var app = express(); +var staticDir = express.static; +var server = http.createServer(app); -io = io.listen(app); +io = io(server); var opts = { port: process.env.PORT || 1948, baseDir : __dirname + '/../../' }; -io.sockets.on('connection', function(socket) { - socket.on('slidechanged', function(slideData) { - if (typeof slideData.secret == 'undefined' || slideData.secret == null || slideData.secret === '') return; - if (createHash(slideData.secret) === slideData.socketId) { - slideData.secret = null; - socket.broadcast.emit(slideData.socketId, slideData); +io.on( 'connection', function( socket ) { + socket.on('multiplex-statechanged', function(data) { + if (typeof data.secret == 'undefined' || data.secret == null || data.secret === '') return; + if (createHash(data.secret) === data.socketId) { + data.secret = null; + socket.broadcast.emit(data.socketId, data); }; }); }); -app.configure(function() { - [ 'css', 'js', 'plugin', 'lib' ].forEach(function(dir) { - app.use('/' + dir, staticDir(opts.baseDir + dir)); - }); +[ 'css', 'js', 'plugin', 'lib' ].forEach(function(dir) { + app.use('/' + dir, staticDir(opts.baseDir + dir)); }); app.get("/", function(req, res) { @@ -47,7 +47,7 @@ var createHash = function(secret) { }; // Actually listen -app.listen(opts.port || null); +server.listen( opts.port || null ); var brown = '\033[33m', green = '\033[32m', diff --git a/plugin/multiplex/master.js b/plugin/multiplex/master.js index b6a7eb7..4becad0 100644 --- a/plugin/multiplex/master.js +++ b/plugin/multiplex/master.js @@ -1,51 +1,31 @@ (function() { + // Don't emit events from inside of notes windows if ( window.location.search.match( /receiver/gi ) ) { return; } var multiplex = Reveal.getConfig().multiplex; - var socket = io.connect(multiplex.url); + var socket = io.connect( multiplex.url ); - var notify = function( slideElement, indexh, indexv, origin ) { - if( typeof origin === 'undefined' && origin !== 'remote' ) { - var nextindexh; - var nextindexv; + function post() { - var fragmentindex = Reveal.getIndices().f; - if (typeof fragmentindex == 'undefined') { - fragmentindex = 0; - } + var messageData = { + state: Reveal.getState(), + secret: multiplex.secret, + socketId: multiplex.id + }; - if (slideElement.nextElementSibling && slideElement.parentNode.nodeName == 'SECTION') { - nextindexh = indexh; - nextindexv = indexv + 1; - } else { - nextindexh = indexh + 1; - nextindexv = 0; - } + socket.emit( 'multiplex-statechanged', messageData ); - var slideData = { - indexh : indexh, - indexv : indexv, - indexf : fragmentindex, - nextindexh : nextindexh, - nextindexv : nextindexv, - secret: multiplex.secret, - socketId : multiplex.id - }; - - socket.emit('slidechanged', slideData); - } - } - - Reveal.addEventListener( 'slidechanged', function( event ) { - notify( event.currentSlide, event.indexh, event.indexv, event.origin ); - } ); - - var fragmentNotify = function( event ) { - notify( Reveal.getCurrentSlide(), Reveal.getIndices().h, Reveal.getIndices().v, event.origin ); }; - Reveal.addEventListener( 'fragmentshown', fragmentNotify ); - Reveal.addEventListener( 'fragmenthidden', fragmentNotify ); + // Monitor events that trigger a change in state + Reveal.addEventListener( 'slidechanged', post ); + Reveal.addEventListener( 'fragmentshown', post ); + Reveal.addEventListener( 'fragmenthidden', post ); + Reveal.addEventListener( 'overviewhidden', post ); + Reveal.addEventListener( 'overviewshown', post ); + Reveal.addEventListener( 'paused', post ); + Reveal.addEventListener( 'resumed', post ); + }()); \ No newline at end of file