UNPKG

goban

Version:

[![License: Apache-2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/online-go/goban)

66 lines (65 loc) 2.65 kB
import { EventEmitter } from "eventemitter3"; import { ClientToServer, ClientToServerBase, ServerToClient } from "./protocol"; export interface GobanSocketEvents extends ServerToClient { connect: () => void; disconnect: (code: number) => void; reconnect: () => void; unrecoverable_error: (code: number, tag: string, message: string) => void; latency: (latency: number, clock_drift: number) => void; timeout: () => void; } interface GobanSocketOptions { /** Don't automatically send pings */ dont_ping?: boolean; ping_interval?: number; timeout_delay?: number; /** Don't log connection/disconnect things*/ quiet?: boolean; } export type DataArgument<Entry> = Entry extends (...args: infer A) => void ? A[0] : never; export type ProtocolResponseType<Entry> = Entry extends (...args: any[]) => infer R ? R : never; /** * This is a simple wrapper around the WebSocket API that provides a * simple interface to connect to the Online-Go.com servers. It provides: * * - Reconnection * - Deals with authentication * - Event based API * - Type safe sends and receives * - Optional promise support for sends * - Latency tracking (doubling as keep alive) * */ export declare class GobanSocket<SendProtocol extends ClientToServerBase = ClientToServer, RecvProtocol = ServerToClient> extends EventEmitter<GobanSocketEvents> { readonly url: string; clock_drift: number; latency: number; options: GobanSocketOptions; private socket; private last_request_id; private promises_in_flight; private reconnecting; private reconnect_tries; private send_queue; private ping_timer?; private timeout_timer?; private callbacks; private authentication?; private manually_disconnected; private current_ping_interval; constructor(url: string, options?: GobanSocketOptions); get connected(): boolean; authenticate(authentication: DataArgument<SendProtocol["authenticate"]>): void; private sendAuthentication; signalTimeout: () => void; ping: () => void; private startPing; private connect; private reconnect; private rejectPromisesInFlight; send<Command extends keyof SendProtocol>(command: Command, data: DataArgument<SendProtocol[Command]>, cb?: (data: ProtocolResponseType<SendProtocol[Command]>, error?: any) => void): void; sendPromise<Command extends keyof SendProtocol>(command: Command, data: DataArgument<SendProtocol[Command]>): Promise<ProtocolResponseType<SendProtocol[Command]>>; disconnect(): void; } export declare function closeErrorCodeToString(code: number): string; export {};