将音频从用户麦克风流传输到浏览器上的多个用户

时间:2019-11-15 01:41:43

标签: javascript html node.js socket.io webrtc

现在,我正在使用Node.js到Javascript来从1位用户获取麦克风音频数据,并让其他人通过浏览器收听该用户的麦克风音频。我正在尝试使用socket.io进行此操作。我该如何实现?现在,我可以发送消息,但不能发送音频或简单的音频文件。如果我可以实时直播用户的麦克风音频,那就太好了。任何简单的客户端/服务器代码示例将不胜感激。我已经读了几个小时了,一切似乎都很混乱。 如果问题太难解决,我也有一个wav文件,但是该如何发送呢?请帮助!

这是我当前的代码。

Server.js

var http = require("http");
var io = require('socket.io');
var server = http.createServer(function (request, response) {
    response.writeHead(200, {
        "Content-Type": "text/html"
    });
    response.write("WebSocket Start~~~~~~~~~~~~");
    response.end("");
}).listen(8080);

var socket = io.listen(server);

socket.on('connection', function (client) {

    client.on('client-stream-request', function (data) {
        var stream = ss.createStream();
        var filename = __dirname + '/downloads/' + < YOURSONG.MP3 > ;
        ss(socket).emit('audio-stream', stream, {
            name: filename
        });
        fs.createReadStream(filename).pipe(stream);
    });


    client.on('message', function (event) {
        console.log('Received message from client!', event);
        client.receive audio from user 1 browser mic ? ? ? ? ? ? ? ?
            client.emit('emitMessage', {
                hello: 'messgge received, wish you happy' + new
                Date().toString()
            });
    });
    client.on('disconnect', function () {
        // clearInterval(interval); 
        console.log('Server has disconnected');
    });
    client.send('hello, I am the server');
    client.send user 1 browser mic audio to everyone connected to the server ? ? ? ? ? ?
});

这是我的客户代码

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <style>
        div {
            border-radius: 10px;
            border: 2px solid pink;
            width: 800px;
        }
    </style>
</head>

<body>
    <h1></h1>
    <div id="result"></div>
    <script src="http://localhost:8080/socket.io/socket.io.js"></script>
    <script>
        //创建Socket.IO实例,建立连接

        var socket = io.connect('http://localhost:8080');

        var audio = document.getElementById('player');
        ss(socket).on('audio-stream', function (stream, data) {
            parts = [];
            stream.on('data', function (chunk) {
                parts.push(chunk);
            });
            stream.on('end', function () {
                audio.src = (window.URL || window.webkitURL).createObjectURL(new Blob(parts));
                audio.play();
            });
        });

        // 添加一个连接监听器
        socket.on('connect', function () {
            console.log('Client has connected to the server!');
        });

        // 添加一个连接监听器
        socket.on('message', function (data) {
            document.getElementById("result").innerHTML += data + "<br />";

        });
        socket.on('emitMessage', function (data) {
            document.getElementById("result").innerHTML += data.hello + "<br />";

        });

        // 添加一个关闭连接的监听器
        socket.on('disconnect', function () {
            console.log('The client has disconnected!');
        });

        // 通过Socket发送一条消息到服务器
        function sendMessageToServer(message) {
            socket.send(message);
        }
        var date = new Date();
        var ms = "Time: " + date.toString() + "Today is a nice day, wish you happy";
        setInterval("sendMessageToServer(ms)", 1000);
    </script>

</body>

</html>

0 个答案:

没有答案