1

My first post here.

I'm totally new to nodejs, javascript, coding...

For a theater performance I want to create a server on a local network. The audience would connect on this network and, during the show, will receive different video on their smartphone.

At first it sounded kind of impossible, but I finally managed, to get something close to what I want.

Everything is copy/paste from different tutorial, articles... found on the internet. Thanks especially to:

http://cjihrig.com/blog/server-sent-events-in-node-js/

http://www.gianlucaguarini.com/blog/nodejs-and-a-simple-push-notification-server/

and mattdlockyer here

http://stackoverflow.com/questions/5235145/changing-source-on-html5-video-tag

I have now a nodejs server sending server-sent event to the browsers based on the changes on a xml file which contain the name of the video to load. My problem is that the browser says:

"NetworkError: 404 Not Found - http://ipaddress5:8000/video3.mp4"

It seems that connect would solve the problem, but as I don't fully understand what I already did......

Any help would be greatly appreciated ;)

Here are my files:

server.js

var parser = require('xml2json');
var http = require("http");
var fs = require("fs");
var chokidar = require('chokidar');
var file;
var watcher = chokidar.watch('example.xml', {ignored: /[\/\\]\./, persistent: true});
watcher.on('change', function(path) {
  fs.readFile(__dirname + '/example.xml', function(err, data) {
  if (err) throw err;
  file = JSON.parse(parser.toJson(data)).test.sample;
  console.log(file);
  return file;
  });
  });

http.createServer(function (req, res) {
  var index = "./sse.htm";
  var fileName;
  var interval;

  if (req.url === "/")
    fileName = index;
  else
    fileName = "." + req.url;

  if (fileName === "./stream") {
    res.writeHead(200, {"Content-Type":"text/event-stream", "Cache-Control":"no-cache", "Connection":"keep-alive"});
    res.write("retry: 3000\n");
    res.write("event: connecttime\n");
    interval = setInterval(function() {
      res.write("data: " + (file) + "\n\n");
  }, 3000);

  }

  else if (fileName === index) {
    fs.exists(fileName, function(exists) {
      if (exists) {
        fs.readFile(fileName, function(error, content) {
          if (error) {
            res.writeHead(500);
            res.end();
          } else {
            res.writeHead(200, {"Content-Type":"text/html"});
            res.end(content, "utf-8");
          }
        });
      } 

      else {
        res.writeHead(404);
        res.end();
      }
    });
  }

  else {
    res.writeHead(404);
    res.end();
  }

}).listen(8000);



console.log("Server running");

sse.htm

<!DOCTYPE html>
<html lang="en">
<head>
  <title>JIH</title>
  <meta charset="UTF-8" />

</head>

<body>
 <div id="content"></div>
 <video id="video" autobuffer width="320" height="240" video.controls=false></video>

 <script>
 var video = document.getElementById('video');
 var sourceV = document.createElement('source');
 var source = new EventSource('stream');
 source.onmessage = function(e) {
 document.getElementById('content').innerHTML = e.data;
 sourceV.setAttribute('src', e.data);
 video.appendChild(sourceV);
 video.pause();
 video.load();
 video.play();
 };
 </script>
</body>
</html>

and example.xml

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<test>
<sample>video3.mp4</sample>
</test>
keyframed
  • 11
  • 1

0 Answers0