在寻求使用客户端到服务器模型(如 Discord)实现 WebRTC 后,我得出的结论是,实现此目的的方法是拥有 2 个客户端 - 服务器和客户端。音频流可以叠加并在 1 个单一流中发送回用户。
后端/server.js
const clientPeer = new Peer({ initiator: true, wrtc });
clientPeer.on('connect', () => console.log('hi client, this is server'));
clientPeer.on('data', (data) => console.log('got a message from client peer: ', data));
前端/index.js
serverPeer.on('connect', () => console.log('Connected to server'));
serverPeer.on('stream', async (stream) => {
const video = document.createElement('audio');
('srcObject' in video)
? video.srcObject = stream
: video.src = window.URL.createObjectURL(stream);
await video.play();
});
我将如何实现在客户端和服务器之间发送媒体流?