multiplexing work with socket.io 1.0+ #1281 #1276

This commit is contained in:
Hakim El Hattab 2015-09-29 10:38:34 +02:00
parent b16bc6fc2e
commit c1a7e83d75
3 changed files with 33 additions and 53 deletions

View file

@ -8,6 +8,6 @@
if (data.socketId !== socketId) { return; } if (data.socketId !== socketId) { return; }
if( window.location.host === 'localhost:1947' ) return; if( window.location.host === 'localhost:1947' ) return;
Reveal.slide(data.indexh, data.indexv, data.indexf, 'remote'); Reveal.setState(data.state);
}); });
}()); }());

View file

@ -1,32 +1,32 @@
var http = require('http');
var express = require('express'); var express = require('express');
var fs = require('fs'); var fs = require('fs');
var io = require('socket.io'); var io = require('socket.io');
var crypto = require('crypto'); var crypto = require('crypto');
var app = express.createServer(); var app = express();
var staticDir = express.static; var staticDir = express.static;
var server = http.createServer(app);
io = io.listen(app); io = io(server);
var opts = { var opts = {
port: process.env.PORT || 1948, port: process.env.PORT || 1948,
baseDir : __dirname + '/../../' baseDir : __dirname + '/../../'
}; };
io.sockets.on('connection', function(socket) { io.on( 'connection', function( socket ) {
socket.on('slidechanged', function(slideData) { socket.on('multiplex-statechanged', function(data) {
if (typeof slideData.secret == 'undefined' || slideData.secret == null || slideData.secret === '') return; if (typeof data.secret == 'undefined' || data.secret == null || data.secret === '') return;
if (createHash(slideData.secret) === slideData.socketId) { if (createHash(data.secret) === data.socketId) {
slideData.secret = null; data.secret = null;
socket.broadcast.emit(slideData.socketId, slideData); socket.broadcast.emit(data.socketId, data);
}; };
}); });
}); });
app.configure(function() { [ 'css', 'js', 'plugin', 'lib' ].forEach(function(dir) {
[ 'css', 'js', 'plugin', 'lib' ].forEach(function(dir) {
app.use('/' + dir, staticDir(opts.baseDir + dir)); app.use('/' + dir, staticDir(opts.baseDir + dir));
});
}); });
app.get("/", function(req, res) { app.get("/", function(req, res) {
@ -47,7 +47,7 @@ var createHash = function(secret) {
}; };
// Actually listen // Actually listen
app.listen(opts.port || null); server.listen( opts.port || null );
var brown = '\033[33m', var brown = '\033[33m',
green = '\033[32m', green = '\033[32m',

View file

@ -1,51 +1,31 @@
(function() { (function() {
// Don't emit events from inside of notes windows // Don't emit events from inside of notes windows
if ( window.location.search.match( /receiver/gi ) ) { return; } if ( window.location.search.match( /receiver/gi ) ) { return; }
var multiplex = Reveal.getConfig().multiplex; var multiplex = Reveal.getConfig().multiplex;
var socket = io.connect(multiplex.url); var socket = io.connect( multiplex.url );
var notify = function( slideElement, indexh, indexv, origin ) { function post() {
if( typeof origin === 'undefined' && origin !== 'remote' ) {
var nextindexh;
var nextindexv;
var fragmentindex = Reveal.getIndices().f; var messageData = {
if (typeof fragmentindex == 'undefined') { state: Reveal.getState(),
fragmentindex = 0;
}
if (slideElement.nextElementSibling && slideElement.parentNode.nodeName == 'SECTION') {
nextindexh = indexh;
nextindexv = indexv + 1;
} else {
nextindexh = indexh + 1;
nextindexv = 0;
}
var slideData = {
indexh : indexh,
indexv : indexv,
indexf : fragmentindex,
nextindexh : nextindexh,
nextindexv : nextindexv,
secret: multiplex.secret, secret: multiplex.secret,
socketId : multiplex.id socketId: multiplex.id
}; };
socket.emit('slidechanged', slideData); socket.emit( 'multiplex-statechanged', messageData );
}
}
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 ); // Monitor events that trigger a change in state
Reveal.addEventListener( 'fragmenthidden', fragmentNotify ); 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 );
}()); }());