@game-vir/multiplayer
Version:
Client side definitions and types for @game-vir/multiplayer-server
92 lines (91 loc) • 4.17 kB
TypeScript
import { type JsonCompatibleValue, type Uuid } from '@augment-vir/common';
import { ListenTarget } from 'typed-event-target';
import { type WebrtcAnswer, type WebrtcOffer } from './web-rtc-communication.js';
declare const WebrtcMessageEvent_base: (new (eventInitDict: {
bubbles?: boolean;
cancelable?: boolean;
composed?: boolean;
detail: any;
}) => import("typed-event-target").TypedCustomEvent<any, "webrtc-message">) & Pick<{
new (type: string, eventInitDict?: EventInit): Event;
prototype: Event;
readonly NONE: 0;
readonly CAPTURING_PHASE: 1;
readonly AT_TARGET: 2;
readonly BUBBLING_PHASE: 3;
}, "prototype" | "NONE" | "CAPTURING_PHASE" | "AT_TARGET" | "BUBBLING_PHASE"> & Pick<import("typed-event-target").TypedCustomEvent<any, "webrtc-message">, "type">;
/**
* An event that is omitted from {@link WebrtcController} when a WebRTC message is received.
*
* @category Internal
*/
export declare class WebrtcMessageEvent<MessageData extends JsonCompatibleValue> extends WebrtcMessageEvent_base {
detail: MessageData;
}
declare const WebrtcConnectEvent_base: (new (eventInitDict: {
bubbles?: boolean;
cancelable?: boolean;
composed?: boolean;
detail: boolean;
}) => import("typed-event-target").TypedCustomEvent<boolean, "webrtc-connect">) & Pick<{
new (type: string, eventInitDict?: EventInit): Event;
prototype: Event;
readonly NONE: 0;
readonly CAPTURING_PHASE: 1;
readonly AT_TARGET: 2;
readonly BUBBLING_PHASE: 3;
}, "prototype" | "NONE" | "CAPTURING_PHASE" | "AT_TARGET" | "BUBBLING_PHASE"> & Pick<import("typed-event-target").TypedCustomEvent<boolean, "webrtc-connect">, "type">;
/**
* An event that is omitted from {@link WebrtcController} when the WebRTC connection is made or lost.
* `event.detail` is `true` if the connection has been made; `false` if the connection was lost.
*
* @category Internal
*/
export declare class WebrtcConnectEvent extends WebrtcConnectEvent_base {
}
/**
* All events fro {@link WebrtcController}.
*
* @category Internal
*/
export type WebrtcEvents<MessageData extends JsonCompatibleValue> = WebrtcMessageEvent<MessageData> | WebrtcConnectEvent;
/**
* A single peer-to-peer WebRTC controller. It is to be used like this:
*
* 1. Call {@link WebrtcController.createOffer} to start off a WebRTC handshake. Send this offer to
* another WebRTC connection or {@link WebrtcController} instance.
* 2. Call {@link WebrtcController.createAnswer} to accept someone else's WebRTC handshake offer and
* create a WebRTC handshake answer. Send this answer back to whoever sent the WebRTC offer.
* 3. Call {@link WebrtcController.acceptAnswer} on the {@link WebrtcController} instance that first
* created the offer.
*
* If everything went well, after those 3 steps you'll now have a live WebRTC connection!
*
* @category Internal
*/
export declare class WebrtcController<MessageData extends JsonCompatibleValue> extends ListenTarget<WebrtcEvents<MessageData>> {
readonly clientId: Uuid;
private dataChannel;
private connection;
/** Indicates whether the WebRTC connection is live or not. */
readonly isConnected: boolean;
constructor(clientId: Uuid);
/** Create a WebRTC offer. This is the first step in the WebRTC handshake process. */
createOffer(stunServerUrls: ReadonlyArray<string>): Promise<WebrtcOffer>;
/** Accepts a WebRTC answer. This is the third (and last) step in the WebRTC handshake process. */
acceptAnswer(rawAnswer: string | Readonly<RTCSessionDescriptionInit>): Promise<void>;
/**
* Accepts a WebRTC offer and creates a WebRTC answer. This is the second step in the WebRTC
* handshake process.
*/
createAnswer(rawOffer: string | Readonly<RTCSessionDescriptionInit>, stunServerUrls: ReadonlyArray<string>): Promise<WebrtcAnswer>;
/**
* Send a message to the other peer in the WebRTC connection. This will throw an error if the
* connection has not been established yet.
*/
sendMessage(data: Readonly<MessageData>): void;
destroy(): void;
private handleDataChannel;
private createConnection;
}
export {};