I'm trying to implement an audio chat with peer js (without node js). It works with 2 people, but with 3 people the sound between A and B stops to work when C joins as you can see on this picture :
3 windows example showing that A stops its audio with B
It seems that the audio.srcObject of A from B is set to null after C calls A ...
I don't understand why. Here is my complete index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Multiple audio chat</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/peerjs@1.2.0/dist/peerjs.min.js"></script>
<script>
initialize()
function initialize() {
peer = new Peer(null, {
debug: 2
});
peer.on('open', function (id) {
console.log('ID: ' + peer.id);
document.getElementById("my_id").innerHTML = "My ID : " + peer.id;
});
peer.on('connection', function (c) {
console.log("connection event")
c.on('open', function () {
console.log("connection open")
})
});
peer.on('call', (call) => {
console.log("I get a call from " + call.peer)
navigator.getUserMedia({ video: false, audio: true }, (stream) => {
call.answer(stream);
call.on('stream', (remoteStream) => {
console.log("I get the stream from " + call.peer)
add_audio(call.peer)
var remote_audio = document.getElementById("audio_" + call.peer)
remote_audio.srcObject = remoteStream
});
}, (err) => {
console.error('Failed to get local stream', err);
});
});
};
function join(id) {
console.log("join( " + id + " )")
var peer_to_join = peer.connect(id, {
reliable: true
})
peer_to_join.on('open', function () {
console.log("Joining: " + peer_to_join.peer);
navigator.getUserMedia({ video: false, audio: true }, (stream) => {
console.log('I call ' + peer_to_join.peer)
const call = peer.call(peer_to_join.peer, stream);
call.on('stream', (remoteStream) => {
console.log("I get the stream from " + peer_to_join.peer)
add_audio(peer_to_join.peer)
var remote_audio = document.getElementById("audio_" + call.peer)
remote_audio.srcObject = remoteStream
});
}, (err) => {
console.error('Failed to get local stream', err);
});
}, (err) => {
console.error('Failed to get local stream', err);
});
};
function add_audio(peer_id) {
var audio_div = document.getElementById("audio")
audio_div.innerHTML += peer_id + '<audio controls autoplay id="audio_' + peer_id + '"></audio><br>'
}
</script>
<div id="my_id"></div>
<span>ID to join: </span>
<input type="text" id="join_id">
<button onclick='join(document.getElementById("join_id").value)'>Join</button>
<div id="audio"></div>
</body>
</html>
question from:
https://stackoverflow.com/questions/65602412/audio-chat-with-peer-js-not-working-with-3-people 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…