diff --git a/e2e/peer/peer-unavailable.html b/e2e/peer/peer-unavailable.html new file mode 100644 index 000000000..cacf82296 --- /dev/null +++ b/e2e/peer/peer-unavailable.html @@ -0,0 +1,43 @@ + + + + + + + + + +

PEER-UNAVAILABLE

+
+
+ + + + diff --git a/e2e/peer/peer.spec.ts b/e2e/peer/peer.spec.ts index 7545a23ce..b593a3800 100644 --- a/e2e/peer/peer.spec.ts +++ b/e2e/peer/peer.spec.ts @@ -17,4 +17,9 @@ describe("Peer", () => { await P.waitForMessage('{"type":"disconnected"}'); expect(await P.errorMessage.getText()).toBe(""); }); + it("should emit an error, when the remote peer is unavailable", async () => { + await P.open("peer-unavailable"); + await P.waitForMessage('{"type":"peer-unavailable"}'); + expect(await P.errorMessage.getText()).toBe('{"type":"peer-unavailable"}'); + }); }); diff --git a/lib/enums.ts b/lib/enums.ts index 0394f90aa..ee19d0cec 100644 --- a/lib/enums.ts +++ b/lib/enums.ts @@ -61,6 +61,7 @@ export enum PeerErrorType { } export enum BaseConnectionErrorType { + PeerUnavailable = "peer-unavailable", NegotiationFailed = "negotiation-failed", ConnectionClosed = "connection-closed", } diff --git a/lib/peer.ts b/lib/peer.ts index 6fa306cbc..2352ff43f 100644 --- a/lib/peer.ts +++ b/lib/peer.ts @@ -4,6 +4,7 @@ import { Socket } from "./socket"; import { MediaConnection } from "./mediaconnection"; import type { DataConnection } from "./dataconnection/DataConnection"; import { + BaseConnectionErrorType, ConnectionType, PeerErrorType, ServerMessageType, @@ -379,6 +380,16 @@ export class Peer extends EventEmitterWithError { PeerErrorType.PeerUnavailable, `Could not connect to peer ${peerId}`, ); + // Emit an error on all connections with this peer. + const connections = (this._connections.get(peerId) ?? []).filter( + (c) => c.peer === peerId, + ); + for (const conn of connections) { + conn.emitError( + BaseConnectionErrorType.PeerUnavailable, + `${peerId} is unavailable`, + ); + } break; case ServerMessageType.Offer: { // we should consider switching this to CALL/CONNECT, but this is the least breaking option. @@ -484,7 +495,11 @@ export class Peer extends EventEmitterWithError { /** * Connects to the remote peer specified by id and returns a data connection. - * @param peer The brokering ID of the remote peer (their {@apilink Peer.id}). + * + * Make sure to listen to the `error` event of the resulting {@link DataConnection} + * in case the connection fails. + * + * @param peer The brokering ID of the remote peer (their {@link Peer.id}). * @param options for specifying details about Peer Connection */ connect(peer: string, options: PeerConnectOption = {}): DataConnection {