goban-engine
Version:
This contains the built Go engine that is used by the Goban package. There are no display components in this package, only the logic for playing the game of Go, making it suitable for usage in node.js or other server-side environments.
99 lines (98 loc) • 4.45 kB
TypeScript
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;
}
export interface GobanSocketOptions {
/** Don't automatically send pings */
dont_ping?: boolean;
/**
* When true, pings are still sent (letting the browser throttle
* naturally) but no timeout timers are armed and pong responses
* are silently ignored (no latency/drift updates, no timeout
* signals). Used when the tab is in the background so we keep the
* connection alive through intermediaries without reacting to
* unreliable timing measurements.
*/
background_pinging?: boolean;
/**
* Timestamp (ms since epoch). Pong responses whose originating
* ping was sent before this time are silently discarded. Set to
* Date.now() when transitioning from background to foreground so
* that stale in-flight pongs from the background period don't
* pollute latency measurements.
*/
ignore_pongs_before?: number;
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;
/**
* Interface describing the public API of a GobanSocket (or compatible proxy).
* Both GobanSocket and GobanSocketProxy satisfy this interface.
* Extends EventEmitter<GobanSocketEvents> so on/off/emit types match exactly.
*/
export interface IGobanSocket<SendProtocol extends ClientToServerBase = ClientToServer, RecvProtocol = ServerToClient> extends EventEmitter<GobanSocketEvents> {
readonly url: string;
clock_drift: number;
latency: number;
options: GobanSocketOptions;
readonly connected: boolean;
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]>>;
authenticate(authentication: DataArgument<SendProtocol["authenticate"]>): void;
disconnect(): void;
ping(): void;
}
/**
* 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;