import * as wshe from 'wshe'; import { BinaryDataTypes } from 'wshe'; type DataTypes = string | BinaryDataTypes; interface MCHelperOptionsBase { /** * The ID of the peer. */ id: string; /** * Whether to print debug information. * - `'verbose'`: print all debug information * - `true`: print connection information * * @default false */ debug?: boolean | 'verbose'; /** * Room id. * * Same room id will be able to communicate with each other. */ roomId: string; } interface MCHelperOptionsWebRTC extends MCHelperOptionsBase { mode: 'webrtc'; /** * The URL of the signaling server or ws instance. * @example "wss://example.com:8080" */ signalingServerUrlOrWsInstance: string | WebSocket; /** * ICE servers. * @default * [ * { urls: 'stun:stun.l.google.com:19302', } * ] */ iceServers?: RTCIceServer[]; } interface MCHelperOptionsWebSocket extends MCHelperOptionsBase { mode: 'websocket'; /** * The URL or ws instance. * @example "wss://example.com:8080" */ urlOrWsInstance: string | WebSocket; } type MCHelperOptions = MCHelperOptionsWebRTC | MCHelperOptionsWebSocket; interface RoomId { roomId: string; } interface PayloadBase { open: { data: { id: string; userData?: Record; }[]; } & RoomId; close: { data: { id: string; }; } & RoomId; } type OnJoinCallback = (data: PayloadBase['open']['data']) => void; type OnLeaveCallback = (data: PayloadBase['close']['data']) => void; type NonRoomId = Omit; declare class WsClient

= Record> { #private; constructor(urlOrWsInstance: string | WebSocket, roomId: string); send(topic: K, data: NonRoomId): void; sendRaw(data: DataTypes): void; onRaw(callback: (data: D) => void): () => void; on(topic: K, callback: (data: P[K]) => void): void; close(): void; get ws(): { ws: { readonly ws: WebSocket | null; open(): void; close(): void; send(event: string, data: any): void; sendRaw(data: wshe.BinaryDataTypes): void; subscribeRaw(callback: (data: T) => void): () => void; subscribe(event: string, callback: (data?: any) => void, once?: boolean): () => void; }; registerCallbacks: (callbacks: Partial<{ onConnected: (ws: WebSocket, event: Event) => void; onDisconnected: (ws: WebSocket, event: Event) => void; onError: (ws: WebSocket, event: Event) => void; }>) => void; }; } declare abstract class AbstractContainer { abstract ws: InstanceType['ws']; /** * If you want to broadcast messages to some peers, you can use `send` function * @param message DataTypes, include binary | string, you can use `serializeObjectPayload` to serialize object to string. */ abstract broadcast(message: DataTypes): { sender: string; receiver: string; message: DataTypes; }; /** * If you want to broadcast messages to all peers, you can use `broadcast` function * @param targetIds The ID of the peers. * @param message DataTypes, include binary | string, you can use `serializeObjectPayload` to serialize object to string. */ abstract send(targetIds: string[], message: DataTypes): { sender: string; receiver: string; message: DataTypes; }; /** * If you want to listen to the broadcast message, you can use `onBroadcast` function * @returns subscribe function */ abstract onBroadcast(callback: (message: D) => void): () => void; abstract onMessageChannelReady(callback: () => void): void; /** * Trigger when some peer is connected to the room. * @returns unsubscribe function */ abstract onJoin(callback: OnJoinCallback): () => void; /** * Trigger when some peer is disconnected to the room. * @returns unsubscribe function */ abstract onLeave(callback: OnLeaveCallback): () => void; /** * You can use this function to remove all the effects when you don't need it. */ abstract close(): void; } declare class MCHelper implements AbstractContainer { #private; constructor(options: MCHelperOptions); get id(): string; serializeObjectPayload(payload: B): string; deserializeObjectPayload(payload: DataTypes): B; broadcast(message: DataTypes): { sender: string; receiver: string; message: DataTypes; }; send(targetIds: string[], message: DataTypes): { sender: string; receiver: string; message: DataTypes; }; onMessageChannelReady(callback: () => void): void; onJoin(callback: OnJoinCallback): () => void; onLeave(callback: OnLeaveCallback): () => void; onBroadcast(callback: (message: D) => void): () => void; close(): void; get ws(): { ws: { readonly ws: WebSocket | null; open(): void; close(): void; send(event: string, data: any): void; sendRaw(data: wshe.BinaryDataTypes): void; subscribeRaw(callback: (data: T) => void): () => void; subscribe(event: string, callback: (data?: any) => void, once?: boolean): () => void; }; registerCallbacks: (callbacks: Partial<{ onConnected: (ws: WebSocket, event: Event) => void; onDisconnected: (ws: WebSocket, event: Event) => void; onError: (ws: WebSocket, event: Event) => void; }>) => void; }; } export { type DataTypes, MCHelper, type MCHelperOptions, type MCHelperOptionsBase, type MCHelperOptionsWebRTC, type MCHelperOptionsWebSocket, type OnJoinCallback, type OnLeaveCallback, type PayloadBase, type RoomId };