diff --git a/lib/peer.ts b/lib/peer.ts index b782f8f08..82bbe94f3 100644 --- a/lib/peer.ts +++ b/lib/peer.ts @@ -392,6 +392,22 @@ export class Peer extends EventEmitter { PeerErrorType.PeerUnavailable, `Could not connect to peer ${peerId}`, ); + + // This is so that an error is emited on a connection that has just been created, + // but could not be established because the server responded with + // `ServerMessageType.Expire` (but yes, we emit the error on all connections). + // See https://github.com/peers/peerjs/issues/924 + // + // TODO how about `this._cleanupPeer(peerId);` instead? + // Or how about make the server send back `connectionId`, and only emit the error + // on that connection? + const connections = this._connections.get(peerId); + if (connections) { + for (const c of connections) { + c.emit("error", new Error(`Server says ${peerId} is unavailable`); + } + } + break; case ServerMessageType.Offer: { // we should consider switching this to CALL/CONNECT, but this is the least breaking option.