简体   繁体   English

实时照片流 node.js

[英]Live Photo Streaming node.js

I am a total noob when it comes to programming.在编程方面,我完全是个菜鸟。 I am getting married soon and I am trying to make a photo box with Instructables cool Live Photo Streamer;我很快就要结婚了,我正在尝试用 Instructables 很酷的 Live Photo Streamer 制作一个照片盒; https://www.instructables.com/Online-Live-Photo-Streaming-From-Any-SD-Card-Enabl/ https://www.instructables.com/Online-Live-Photo-Streaming-From-Any-SD-Card-Enabl/

However, I am getting this error when trying to run the code and I have no idea what it means.但是,在尝试运行代码时出现此错误,我不知道这意味着什么。 I have pasted my code below.我在下面粘贴了我的代码。

Error: enter image description here错误:在此处输入图像描述

 // Include the http module (built-in to NodeJS) var http = require('http'); // Include Express to pick up web requests var express = require('express'), // Start Express up app = express(); // Create a new server using our express app var server = http.createServer(app); //set path to static files app.use(express.static(__dirname + '/public')); // Include File System Module var fs = require('fs'); // Start a new websocket on the server's address var io = require('socket.io').listen(server); // Create an array to store open socket connections with var sockets = []; // Create an array to store the name of images we've already displayed var images = []; // Import the directory watching module var watch = require('node-watch'); // An interval to make sure we don't send out the link prior to upload completion var checkCompletionInterval; /* * Start watching the image directory for changes. * When we detect a change, notify the clients */ watch('public/images', function(filename) { // Find the new image that triggered this var newImg = findNewImageName(); // If we do have a new image (and it was just deleted or something) if (newImg) { // Print out its name to the terminal console.log("New Image Name: " + newImg); // If we have an interval going, cancel it if (checkCompletionInterval) clearInterval(checkCompletionInterval); // Send the image out to all of the clients in 500 ms checkCompletionInterval = setInterval(sendImgToClients, 500, newImg); // Add it to the list of displayed images images.push(newImg); } }); /* Send the image to each client web socket */ function sendImgToClients(newImg) { // For each open socket for (var i = 0; i < sockets.length; i++) { // Send the message to the socket sockets[i].emit('newImage', {newImage: "/images/" + newImg}); } // Clear the completion interval clearInterval(checkCompletionInterval); } /* * If we get a request to our root route, send the html page back */ app.get('/', function(req, res) { // Read the html file into a text buffer fs.readFile(__dirname + '/public/livePhoto.html', 'utf8', function(err, text){ // Send out the buffer res.send(text); }); }) /* * Create new socket connections * and handle teardowns */ io.sockets.on('connection', function (socket) { // If there is a socket if (socket) { // Set our global socket to it for later sockets.push(socket); } // When a socket disconnects socket.on('disconnect', function () { // Remove that socket from our array delete sockets[socket]; }); }); /* Scan the array of images until we find one we haven't accounted for yet. */ function findNewImageName() { // Grab the list if images in the directory var files = fs.readdirSync('public/images'); // Iterate through the images for (var i = 0; i < files.length; i++) { // If the image name isn't in our array of account for images // it's the new one, so return it. if (images.indexOf(files[i]) == -1 && files[i].indexOf('.DS') == -1) { return files[i]; } } } /* Add the pre-existing images to our image array so that we don't confuse it with a new one. */ function logExistingImages() { // Grab all the current images var files = fs.readdirSync('public/images'); // For each file in the directory for (var i = 0; i < files.length; i++) { // Add it to the array images.push(files[i]); } } // Catalog all existing images logExistingImages(); // Start the server on port 3000 server.listen(3000); //Write out the port just for fun console.log("listening on port 3000");

Please please help: I really want to get this working - it would be so cool!请帮助:我真的很想让这个工作 - 太酷了! :) :)

It's just a syntax error.这只是一个语法错误。 Line 20 should be:第 20 行应该是:

var io = require('socket.io')(server);

https://socket.io/docs/v3/server-api/#new-Server-httpServer-options https://socket.io/docs/v3/server-api/#new-Server-httpServer-options

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM