简体   繁体   中英

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.

After installing the socket.io-stream with npm and started, getting the below error in chrome console:

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

and

Uncaught ReferenceError: ss is not defined

Here is my client.html file:

 <!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

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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