Sync JS time between multiple devices

后端 未结 5 890
忘了有多久
忘了有多久 2020-11-29 03:23

I\'m using the wonderful reveal.js library to create a HTML slideshow. My only problem is that I need it to synchronise across multiple devices.

At the moment I am m

5条回答
  •  孤街浪徒
    2020-11-29 04:19

    How about a different approach: who cares about time? (You're not going to reliably sync the system clock with JavaScript.)

    Instead, use a Node server with socket.io to synchronize when your clients advance the slideshow. Instead of the clients deciding when to advance, the server tells them to.

    This approach comes with the added bonus of being able to manually fiddle with the slideshow while it's running. In the example that follows, I've added a Next button that causes all connected clients to immediately advance to the next slide.

    app.js

    var express = require('express')
        , app = express.createServer()
        , io = require('socket.io').listen(app)
        , doT = require('dot')
        , slide = 0
        , slides = [
            'http://placekitten.com/700/400?image=13',
            'http://placekitten.com/700/400?image=14',
            'http://placekitten.com/700/400?image=15',
            'http://placekitten.com/700/400?image=16',
            'http://placekitten.com/700/400?image=1',
            'http://placekitten.com/700/400?image=2',
            'http://placekitten.com/700/400?image=3',
            'http://placekitten.com/700/400?image=4',
            'http://placekitten.com/700/400?image=5',
            'http://placekitten.com/700/400?image=6',
            'http://placekitten.com/700/400?image=7',
            'http://placekitten.com/700/400?image=8',
            'http://placekitten.com/700/400?image=9',
            'http://placekitten.com/700/400?image=10',
            'http://placekitten.com/700/400?image=11',
            'http://placekitten.com/700/400?image=12',
        ];
    
    app.listen(70); // listen on port 70
    
    app.register('.html', doT); // use doT to render templates
    app.set('view options', {layout:false}); // keep it simple
    doT.templateSettings.strip=false; // don't strip line endings from template file
    
    app.get('/', function(req, res) {
        res.render('index.html', { slide: slide, slides: slides });
    });
    
    app.post('/next', function(req, res) {
        next();
        res.send(204); // No Content
    });
    
    setInterval(next, 4000); // advance slides every 4 seconds
    
    function next() {
        if (++slide >= slides.length) slide = 0;
        io.sockets.emit('slide', slide);
    }
    

    views/index.html

    This file is processed as a doT template.

    
    
    
    Synchronized Slideshow
    
    
    
    
    
    
        
    {{~it.slides :url:i}}
    {{~}}

    Copy these two files into a folder, then run

    $ npm install express socket.io dot
    $ node app.js
    

    and navigate to http://localhost:70 in several different windows, then see the magic.

提交回复
热议问题