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.
66 lines (65 loc) • 2.65 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;
}
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 {};