简体   繁体   English

socket.io-stream 在 socket 客户端和服务器中访问时也面临 404 问题

[英]socket.io-stream facing 404 issue when accessing in socket client and server also

I was trying to create an mp3 stream (already added the .mp3 file in my machine) using socket.io-stream and access it using the client.html file.我试图使用 socket.io-stream 创建一个 mp3 流(已经在我的机器中添加了 .mp3 文件)并使用 client.html 文件访问它。

After installing the socket.io-stream with npm and started, getting the below error in chrome console:使用 npm 安装socket.io-stream并启动后,在 chrome 控制台中出现以下错误:

http://localhost:5001/socket.io-stream/socket.io-stream.js net::ERR_ABORTED 404 (Not Found) http://localhost:5001/socket.io-stream/socket.io-stream.js net::ERR_ABORTED 404(未找到)

and

Uncaught ReferenceError: ss is not defined未捕获的 ReferenceError:ss 未定义

Here is my client.html file:这是我的 client.html 文件:

 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>bb</title> </head> <body> <script src="/socket.io/socket.io.js"></script> <script src="/socket.io-stream/socket.io-stream.js"></script> <h1>Audio Testing 1 2 3</h1> <audio id="audio" controls> <source src="" type="audio/mpeg"> Your browser does not support the audio element. </audio> <br> <script> var socket = io('http://localhost:' + window.location.port); var audio = document.getElementById('audioSource'); console.log("hoi"); socket.on('start', function (data) { console.log("start"); console.log(data); // socket.emit('my other event', { my: 'data' }); socket.emit('stream', { my: 'data' }); console.log(""); ss(socket).on('audio-stream', function(stream, data) { parts = []; console.log("DATA -->> ") stream.on('data', (chunk) => { console.log(chunk); parts.push(chunk); }); stream.on('end', function () { var audio = document.getElementById('audio'); audio.src = (window.URL || window.webkitURL).createObjectURL(new Blob(parts)); audio.play(); }); }); }); </script> </body> </html>

Here is my server.js file这是我的 server.js 文件

 var express = require('express'); var app = express(); var server = require('http').Server(app); var fs = require('fs'); var io = require('socket.io')(server); var ss = require('socket.io-stream'); app.use(express.static(`${__dirname}/html`)); server.listen('5001'); app.get('/', function (req, res) { res.sendFile(__dirname + '/index2.html'); }); io.on('connection', function (socket) { socket.emit('start', { hello: 'world' }); socket.on('stream', function (data) { console.log(data); var stream = ss.createStream(); var filename = __dirname + '/audio/musicfile.mp3' ; ss(socket).emit('audio-stream', stream, { name: filename }); fs.createReadStream(filename).pipe(stream); }); });

Also please suggest any pointers (latest tutorials/articles related to socket audio streaming).还请提出任何建议(与套接字音频流相关的最新教程/文章)。

you need to expose the file to the public directory您需要将文件公开到公共目录

$ cp node_modules/socket.io-stream/socket.io-stream.js somewhere/public/

see: https://github.com/nkzawa/socket.io-stream#browser见: https : //github.com/nkzawa/socket.io-stream#browser

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

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