This error happens to me when I try to connect for the second time between two peers.
P1 - Person 1, P2 - Person 2
It goes like this:
P1 emits "Looking for partner" ->
P2 emits "Looking for partner" ->
Server emits to P2 "Partner found" ->
P2 creating new Peer with initiator: true
P2 emits to Server "I am ready" ->
Server emits to P1 "P2 wants to connect" ->
P1 creating new Peer with initiator: false
P1 emits to Server "P2 is accepted"
Server emits to P2 "You are accepted" ->
And they can talk at this point
Then When P2 is closing conversation
P2 is doing peer.destroy();
P2 emits to Server "Let P1 know that I disconnected"
Server emits to P1 "P2 is off"
P1 is doing peer.destroy();
And now when I want to do the exact same thing(P2 is again initiator) I receive this error.
It happens exactly at Server emits to P2 "You are accepted" ->
function when I do: peer.signal(data.signal);
This is how it looks like with peer._debug = console.log;
starting batched negotiation
start negotiation
signalingStateChange have-local-offer
createOffer success
iceStateChange (connection: new) (gathering: gathering)
started iceComplete timeout
iceStateChange (connection: new) (gathering: complete)
signal
iceStateChange (connection: checking) (gathering: complete)
signal()
on track
iceStateChange (connection: connected) (gathering: complete)
maybeReady pc true channel false
flushing sender queue []
negotiate
signalingStateChange stable
on channel open
maybeReady pc true channel true
connect local: 192.168.0.21:64575 remote: 192.168.0.21:64579
connect
destroy (error: undefined)
starting batched negotiation
start negotiation
signalingStateChange have-local-offer
createOffer success
iceStateChange (connection: new) (gathering: gathering)
started iceComplete timeout
iceStateChange (connection: new) (gathering: complete)
signal
iceStateChange (connection: checking) (gathering: complete)
Uncaught Error: cannot signal after peer is destroyed
Code is available here: https://codeshare.io/5OjVNx for Front https://codeshare.io/2KlpWK for Back
OK, turns out my code was a complete garbage and it shouldn't be done like this! I was following some bad tutorial. The problem here was with signals that were going in a constant loop ending with some error, somehow it worked like this but you couldn't destroy peers without getting application crashed.