@mothepro/fancy-p2p
Version:
A quick and efficient way to form p2p groups in the browser
47 lines (46 loc) • 2.01 kB
TypeScript
import { Emitter, Listener } from 'fancy-emitter';
import type { ClientID, Name } from '@mothepro/signaling-lobby';
import Client from './Client.js';
import Signaling from './Signaling.js';
declare type Receiveable = Blob | ArrayBuffer;
export declare type Sendable = Receiveable | ArrayBufferView;
/** Represents a direct connection to a peer found in the signalling lobby. */
export interface MySimplePeer<T = Sendable> {
/** Name of the new peer. */
readonly name: Name;
/** Send data to activate the `message` listener for the peer. */
send(data: T): void;
/** Activates when a message is received for this peer. Cancels once the connection is closed. */
readonly message: Listener<Exclude<T, ArrayBufferView>>;
/**
* Whether this peer represents a "connection" to you.
* When false this is another peer and data is sent through the wire.
*/
readonly isYou: boolean;
/** Close the connection with this peer. */
close(): void;
}
/** Simple class that can be used as a local feedback peer. */
export declare class MockPeer<T extends Sendable = Sendable> implements MySimplePeer<T> {
readonly name: Name;
readonly isYou = true;
readonly message: Emitter<Exclude<T, ArrayBufferView>>;
readonly send: (data: T) => Emitter<Exclude<T, ArrayBufferView>>;
readonly close: () => Emitter<Exclude<T, ArrayBufferView>>;
readonly ready: Promise<boolean>;
constructor(name: Name);
}
export default class<T extends Sendable = Sendable> implements MySimplePeer<T> {
readonly fallback?: Signaling | undefined;
private rtc?;
readonly isYou = false;
readonly name: Name;
readonly message: Emitter<Exclude<T, ArrayBufferView>>;
readonly ready: Promise<boolean>;
readonly fallbackId: ClientID;
readonly send: (data: T) => void;
readonly close: () => void;
constructor(stuns: string[], client: Client, retries?: number, timeout?: number, fallback?: Signaling | undefined);
private makeRtc;
}
export {};