UNPKG

ps2census

Version:

Client to connect to the PS2 Event Stream websocket.

150 lines (149 loc) 4.06 kB
import { EventEmitter } from 'eventemitter3'; import { ClientOptions } from 'ws'; import { PS2Environment } from '../types/ps2.options'; import { CensusMessageWithoutEcho } from './types/messages.types'; import { CensusCommand } from './types/command.types'; export interface StreamClientOptions { connectionTimeout?: number; heartbeatInterval?: number; endpoint?: string; wsOptions?: ClientOptions; } type StreamClientEvents = { ready: () => void; destroyed: () => void; close: (code: number, reason?: string) => void; error: (err: Error) => void; warn: (err: Error) => void; debug: (info: string) => void; message: (message: CensusMessageWithoutEcho) => void; }; export declare class StreamClient extends EventEmitter<StreamClientEvents> { private readonly serviceId; private readonly environment; /** * @type {string} The endpoint url for the Census stream */ private readonly gatewayUri; /** * @type {number} Period of the heartbeat in milliseconds */ private readonly heartbeatInterval; /** * @type {Timeout?} Interval at which the heartbeat is reset */ private heartbeatTimer?; /** * @type {boolean} Whether a heartbeat was send by the websocket */ private heartbeatAcknowledged; /** * @type {number?} Unix time when last heartbeat was send */ private lastHeartbeat?; /** * @type {State} client state */ private state; /** * @type {WebSocket?} Websocket client to PS2 event stream */ private connection?; /** * @type {ClientOptions?} Websocket options used to establish stream */ private readonly wsOptions?; /** * @type {number} Unix time when client got connected */ private connectedAt?; /** * @type {Timeout?} Timeout to make sure the connection is established within a reasonable time */ private connectionTimeout?; /** * @type {number} Timeout time for when we waited long enough */ private readonly connectionTimeoutTime; /** * @param {string} serviceId * @param {PS2Environment} environment * @param {StreamClientOptions} options */ constructor(serviceId: string, environment: PS2Environment, { connectionTimeout, heartbeatInterval, endpoint, wsOptions, }?: StreamClientOptions); /** * Maybe, is ready, maybe is not, who knows */ get isReady(): boolean; /** * Connect to the PS2 event stream * * @return {Promise<void>} */ connect(): Promise<void>; /** * client successfully connected */ private onOpen; /** * Handles messages received from the gateway * * @param {WebSocket.MessageEvent} event */ private onMessage; /** * Handles the data received * * @param {CensusMessageWithoutEcho} data */ private onPackage; /** * Connection closed by server */ private onClose; /** * Relays error from the websocket connection to the client * * @param {ErrorEvent} event */ private onError; /** * If a connection exists cleanup listeners */ private cleanupConnection; /** * Destroys a connection forcefully * * @param {number} code * @param {boolean} emit */ destroy({ code, emit }?: { code?: number | undefined; emit?: boolean | undefined; }): void; /** * Toggle connection timeout * * @param {boolean} toggle */ private setConnectionTimeout; /** * Manages the heartbeat timer * * @param {number} interval Negative value will forget the timer */ private setHeartbeatTimer; /** * Checks if the heartbeat has been acknowledged and resets the heartbeat */ private resetHeartbeat; /** * Acknowledges a heartbeat send from the gateway */ private acknowledgeHeartbeat; /** * @param data */ send(data: CensusCommand): void; } export {};