|
|
@@ -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); |