123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import type { Peer } from "./peer";
- import type { ServerMessage } from "./servermessage";
- import type { ConnectionType } from "./enums";
- import { BaseConnectionErrorType } from "./enums";
- import { PeerError, type PromiseEvents } from "./peerError";
- import EventEmitter from "eventemitter3";
- import { EventEmitterWithPromise } from "./eventEmitterWithPromise";
- export interface BaseConnectionEvents<
- ErrorType extends string = BaseConnectionErrorType,
- > extends PromiseEvents<never, ErrorType> {
- /**
- * Emitted when either you or the remote peer closes the connection.
- *
- * ```ts
- * connection.on('close', () => { ... });
- * ```
- */
- close: () => void;
- /**
- * ```ts
- * connection.on('error', (error) => { ... });
- * ```
- */
- error: (error: PeerError<`${ErrorType}`>) => void;
- iceStateChanged: (state: RTCIceConnectionState) => void;
- }
- export interface IBaseConnection<
- SubClassEvents extends BaseConnectionEvents<
- BaseConnectionErrorType | ErrorType
- >,
- ErrorType extends string = never,
- > extends EventEmitter<SubClassEvents> {
- readonly metadata: any;
- readonly connectionId: string;
- get type(): ConnectionType;
- /**
- * The optional label passed in or assigned by PeerJS when the connection was initiated.
- */
- label: string;
- /**
- * Whether the media connection is active (e.g. your call has been answered).
- * You can check this if you want to set a maximum wait time for a one-sided call.
- */
- get open(): boolean;
- close(): void;
- }
- export abstract class BaseConnection<
- AwaitType extends EventEmitter<SubClassEvents>,
- SubClassEvents extends BaseConnectionEvents<
- BaseConnectionErrorType | ErrorType
- >,
- ErrorType extends string = never,
- >
- extends EventEmitterWithPromise<
- AwaitType,
- never,
- ErrorType | BaseConnectionErrorType,
- SubClassEvents
- >
- implements IBaseConnection<SubClassEvents, ErrorType>
- {
- protected _open = false;
- /**
- * Any type of metadata associated with the connection,
- * passed in by whoever initiated the connection.
- */
- readonly metadata: any;
- connectionId: string;
- peerConnection: RTCPeerConnection;
- dataChannel: RTCDataChannel;
- abstract get type(): ConnectionType;
- label: string;
- get open() {
- return this._open;
- }
- protected constructor(
- /**
- * The ID of the peer on the other end of this connection.
- */
- readonly peer: string,
- public provider: Peer,
- readonly options: any,
- ) {
- super();
- this.metadata = options.metadata;
- }
- abstract close(): void;
- /**
- * @internal
- */
- abstract handleMessage(message: ServerMessage): void;
- /**
- * Called by the Negotiator when the DataChannel is ready.
- * @internal
- * */
- abstract _initializeDataChannel(dc: RTCDataChannel): void;
- }
|