Browse Source

client automatically reloads when the server restarts, and fixed issues with caching

master
mort 8 years ago
parent
commit
869be60dc7
2 changed files with 54 additions and 6 deletions
  1. 32
    4
      index.html
  2. 22
    2
      server.js

+ 32
- 4
index.html View File

@@ -77,7 +77,7 @@
}

// Change slides with a transition
function update() {
function update(name) {
overlay().innerHTML = "";
overlay().className = "_content";
swap(main(), overlay());
@@ -86,19 +86,47 @@
.then(response => response.text())
.then(text => {
setTimeout(() => {
history.replaceState({}, "", "/"+name+"/");
main().innerHTML = text;
overlay().className = "_content hidden";
}, 1000);
})
.catch(err => console.error(err));
}

function reload() {
var i = setInterval(() => {
fetch("/")
.then(() => {
history.replaceState({}, "", "/");
location.reload();
})
.catch(() => {});
}, 1000);
}

function await() {
// Wait for the next slide change, then update again
fetch("/await")
.then(response => update())
.catch(err => { console.error(err); update(); });
.then(response => response.json())
.then(obj => {
if (obj.evt === "next") {
update(obj.args.name);
} else if (obj.evt === "reload") {
return reload();
} else {
console.log("Unknown event: "+obj.evt);
}
await();
})
.catch(err => { console.error(err); await(); });
}

update();
await();

fetch("/init")
.then(response => response.text())
.then(name => update(name));
</script>
</body>
</html>

+ 22
- 2
server.js View File

@@ -25,7 +25,8 @@ function Slide(dir) {
.on("error", err => error(res, err))
.pipe(res);
} else {
fs.createReadStream(pathlib.join(dir, parts.pathname))
var name = parts.pathname.replace(dir, "");
fs.createReadStream(pathlib.join(dir, name))
.on("error", err => error(res, err))
.pipe(res);
}
@@ -53,6 +54,13 @@ function Slideshow(dir, changeInterval) {
var currentSlide = null;
var awaiters = [];

self.sendEvent = function(evt, args) {
awaiters.forEach(res => res.end(JSON.stringify({
evt: evt,
args: args
})));
}

self.serve = function(req, res) {
var parts = urllib.parse(req.url);

@@ -60,6 +68,10 @@ function Slideshow(dir, changeInterval) {
if (parts.pathname === "/") {
res.end(index);

// /init: send initial information about current slide
} else if (parts.pathname === "/init") {
res.end(currentSlide ? currentSlide.dir : "");

// /await: long polling, request won't end before a new slide comes
} else if (parts.pathname === "/await") {
awaiters.push(res);
@@ -96,7 +108,7 @@ function Slideshow(dir, changeInterval) {
}

// End all awaiting connections to notify slide change
awaiters.forEach(res => res.end());
self.sendEvent("next", { name: currentSlide.dir });
}, changeInterval);
}
init();
@@ -106,6 +118,14 @@ function Slideshow(dir, changeInterval) {

var slideshow = Slideshow(conf.slides, conf.interval);

function onexit() {
slideshow.sendEvent("reload");
process.exit();
}
process.on("exit", onexit);
process.on("SIGINT", onexit);
process.on("SIGTERM", onexit);

http.createServer((req, res) => {
slideshow.serve(req, res);
}).listen(conf.port);

Loading…
Cancel
Save