UNPKG

@mothepro/fancy-p2p

Version:

A quick and efficient way to form p2p groups in the browser

49 lines (48 loc) 2.1 kB
import { SafeEmitter, Emitter, Listener } from 'fancy-emitter'; import type { ClientID, Name } from '@mothepro/signaling-lobby'; /** Represents another client in the same lobby and signaling server as we are. */ export interface SimpleClient { /** Name of this client. */ readonly name: Name; /** Activated when this client proposes a new group. Closed when client leaves. */ readonly proposals: Listener<{ /** The other members in this group, including me. */ members: SimpleClient[]; /** Function to accept or reject the group, not present if you created the group */ action?(accept: boolean): void; /** Activated with the Client who just accepted the group proposal. Deactivates when someone rejects. */ ack: Listener<SimpleClient>; }>; /** * Whether this client represents you in the lobby. * When false this is another client and proposals are initiated by them. */ readonly isYou: boolean; } export declare class MockClient implements SimpleClient { readonly name: Name; readonly isYou = true; readonly proposals: Emitter<{ members: SimpleClient[]; ack: Emitter<SimpleClient>; }>; constructor(name: Name); } /** Represents another client in the same lobby and signaling server as we are that can preform SDP exchange. */ export default class implements SimpleClient { readonly id: ClientID; readonly name: Name; readonly isYou = false; readonly proposals: Emitter<{ members: SimpleClient[]; action(accept: boolean): void; ack: Emitter<SimpleClient>; }>; /** Whether this connection should be an opener or closer. Set before the server finalizes the group. */ isOpener?: boolean; /** Activate with the creation of an SDP to send it to the corresponding client. */ readonly creator: SafeEmitter<RTCSessionDescriptionInit>; /** Activates when an SDP is received for this corresponding client. */ readonly acceptor: SafeEmitter<RTCSessionDescriptionInit>; constructor(id: ClientID, name: Name); }