Pictures!
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

server.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. var fs = require("fs");
  2. var http = require("http");
  3. var crypto = require("crypto");
  4. var pathlib = require("path");
  5. var urllib = require("url");
  6. var conf = JSON.parse(fs.readFileSync("conf.json"));
  7. var index = fs.readFileSync("index.html", "utf-8")
  8. .replace("{{transition_time}}", conf.transition_time);
  9. function error(res, err) {
  10. console.trace(err);
  11. res.end(err.toString());
  12. }
  13. // The individual slide
  14. function Slide(dir) {
  15. var self = {};
  16. self.dir = dir;
  17. function serve(parts, res) {
  18. if (parts.pathname === "/slide") {
  19. fs.createReadStream(pathlib.join(dir, "index.html"))
  20. .on("error", err => error(res, err))
  21. .pipe(res);
  22. } else {
  23. fs.createReadStream(pathlib.join(dir, parts.pathname))
  24. .on("error", err => error(res, err))
  25. .pipe(res);
  26. }
  27. }
  28. self.serve = function(parts, res) {
  29. try {
  30. serve(parts, res);
  31. } catch (err) {
  32. if (err.code && err.code === "ENOENT")
  33. res.writeHead(404);
  34. error(res, err);
  35. }
  36. }
  37. return self;
  38. }
  39. // The slideshow, whose job it is to manage all slides
  40. // and tell the client whether it has to update or not
  41. function Slideshow(dir, changeInterval) {
  42. var self = {};
  43. var currentSlide = null;
  44. var awaiters = [];
  45. self.serve = function(req, res) {
  46. var parts = urllib.parse(req.url);
  47. // /: Send the base site to the client
  48. if (parts.pathname === "/") {
  49. res.end(index);
  50. // /await: long polling, request won't end before a new slide comes
  51. } else if (parts.pathname === "/await") {
  52. awaiters.push(res);
  53. // There's a current slide: leave serving files up to the slide
  54. } else if (currentSlide) {
  55. currentSlide.serve(parts, res);
  56. // There's no current slide show
  57. } else {
  58. res.end("No current slideshow.");
  59. }
  60. }
  61. // This function starts the slideshow and goes through the slides
  62. // one by one. When done, it starts again by calling this function again.
  63. function init() {
  64. var slides = fs.readdirSync(dir)
  65. .sort()
  66. .map(file => Slide(pathlib.join(dir, file)));
  67. var slideIndex = 0;
  68. currentSlide = slides[slideIndex];
  69. var interval = setInterval(() => {
  70. slideIndex += 1;
  71. // Go to the next slide, or restart
  72. if (slideIndex >= slides.length) {
  73. clearInterval(interval);
  74. init();
  75. } else {
  76. currentSlide = slides[slideIndex];
  77. }
  78. // End all awaiting connections to notify slide change
  79. awaiters.forEach(res => res.end());
  80. }, changeInterval);
  81. }
  82. init();
  83. return self;
  84. }
  85. var slideshow = Slideshow(conf.slides, conf.interval);
  86. http.createServer((req, res) => {
  87. slideshow.serve(req, res);
  88. }).listen(conf.port);