簡體   English   中英

實時照片流 node.js

[英]Live Photo Streaming node.js

在編程方面,我完全是個菜鳥。 我很快就要結婚了,我正在嘗試用 Instructables 很酷的 Live Photo Streamer 制作一個照片盒; https://www.instructables.com/Online-Live-Photo-Streaming-From-Any-SD-Card-Enabl/

但是,在嘗試運行代碼時出現此錯誤,我不知道這意味着什么。 我在下面粘貼了我的代碼。

錯誤:在此處輸入圖像描述

 // 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");

請幫助:我真的很想讓這個工作 - 太酷了! :)

這只是一個語法錯誤。 第 20 行應該是:

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

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