import { EventEmitter } from 'node:events'; import WebSocket from 'ws'; import { ScreepsHttpClient } from './ScreepsHttpClient'; import { MapVisualEvent, RoomEvent, RoomMap2Event, SocketEvent, UserCodeEvent, UserConsoleEvent, UserCpuEvent, UserMemoryEvent, UserResourceEvent } from './socket'; /** * Provides access to the Screeps WebSocket API. * * To begin using this client, call {@link connect} to establish a connection * to {@link ScreepsHttpClient.server} and authenticate once the connection * is established. The {@link ScreepsSocketClient.AUTHED} event will be fired * when authentication is successful. {@link ScreepsSocketClient.AUTH} will be * fired when authentication success or fails. * * Interactions with this client are primarily event-driven. Use {@link subscribe} * and its derived methods to subscribe to relevant events and register listeners. * * This client's behavior can be configured via options in {@link ScreepsClientConfig}. * * To switch servers, call {@link ScreepsHttpClient.setServer}, then manually * reconnect and (re)subscribe to any relevant events. * Example: * {@includeCode ../examples/socket-demo.ts} * @see {@link ScreepsHttpClient} for the HTTP API client * @hideconstructor * @category WebSocket API */ export declare class ScreepsSocketClient extends EventEmitter { /** * Sent after a connection is established and authentication has been attempted. * * Payload: * @event {@link ServerAuthEvent} The response to the authentication request * @category Events */ static readonly AUTH = "auth"; /** * Sent after successful authentication to the server. * * Payload: * @event undefined * @category Events */ static readonly AUTHED = "authed"; /** * Sent after a connection is established, but before authentication. * * Payload: * @event undefined * @category Events */ static readonly CONNECTED = "connected"; /** * Sent after a connection is established, but before automatic reconnection * is attempted. * * Payload: * @event undefined * @category Events */ static readonly DISCONNECTED = "disconnected"; /** * Sent when an error is emitted by the WebSocket library. * * Payload: * @event unknown - An error value or object * @category Events */ static readonly ERROR = "error"; /** * Sent when any message is received from the server. * * Payload: * @event {@link SocketEvent} The received message after it has been parsed * into an event object * @category Events */ static readonly MESSAGE = "message"; /** * Sent after subscribing to an event on the server. * * Payload: * @event string - The subscribed event type * @category Events */ static readonly SUBSCRIBE = "subscribe"; /** * Sent when a new API authentication token has been obtained. * * Payload: * @event string - The API auth token * @category Events */ static readonly TOKEN = "token"; /** * Sent after unsubscribing from an event on the server. * * Payload: * @event string - The unsubscribed event type * @category Events */ static readonly UNSUBSCRIBE = "unsubscribe"; private __authed; /** * If true, the client has an open WebSocket connection to the server * and has successfully authenticated */ get authed(): boolean; private __connected; /** * If true, the client has an open WebSocket connection to the server * @see {@link authed} for authentication status */ get connected(): boolean; private __reconnecting; /** * If true, a {@link reconnect} attempt is currently in progress */ get reconnecting(): boolean; protected ws?: WebSocket; /** * Used to decode buffered message data received over the WebSocket connection * @hidden */ protected decoder: TextDecoder; /** * A reference to the associated HTTP API client * @hidden */ protected http: ScreepsHttpClient; private keepAliveInter?; /** * Pending messages to send once connected/authenticated * @hidden */ private __queue; /** * Pending subscriptions to request once connected/authenticated * @hidden */ private __subQueue; /** * The number of subscriber callbacks by event type * @hidden */ private __subs; /** * Initializes a new WebSocket API client. Do not call this directly. * Instead, use the instance from {@link ScreepsHttpClient.socket}. * @param http The HTTP client instance with which config and auth credentials * should be shared */ constructor(http: ScreepsHttpClient); /** Initialize (or re-initialize) all client state */ private reset; /** * Connect to the server and immediately attempt to authenticate. * * Any queued messages will be sent as soon as authentication succeeds. * @throws {@link node!Error | Error} if an API token is not available due to missing auth credentials */ connect(): Promise; /** * Reconnect to the server using current client settings. * * This method will immediately exit if the client is already attempting * to reconnect. * * Upon success, all previous subscriptions will be reestablished * (if {@link ScreepsSocketConfig.resubscribe} is enabled). * * Up to {@link ScreepsSocketConfig.maxRetries} connections will be attempted * with exponential backoff. * @throws {@link node!Error | Error} if the maximum number of retry attempts is exceeded. * The same error instance will also be fired as an {@link ScreepsSocketClient.ERROR} * event payload. */ reconnect(): Promise; /** Close the connection and clear all queued messages. */ disconnect(): void; /** * Subscribe to {@link MapVisualEvent}s * @param shardName The name of the shard to use (ignored by unofficial servers). * Defaults to {@link ScreepsClientConfig.defaultShard} if undefined. * @param cb The callback to trigger when a relevant message is received. * @throws {@link node!Error | Error} if shard and {@link ScreepsClientConfig.defaultShard} are undefined * while using an official server * @see {@link unSubscribeMapVisual} */ subscribeMapVisual(shardName?: string, cb?: (event: MapVisualEvent) => void): Promise; /** * Unsuubscribe from {@link MapVisualEvent}s * @param shardName The name of the shard to use (ignored by unofficial servers). * Defaults to {@link ScreepsClientConfig.defaultShard} if undefined. * @param cb The callback to unregister * @throws {@link node!Error | Error} if shard and {@link ScreepsClientConfig.defaultShard} are undefined * while using an official server * @see {@link subscribeMapVisual} */ unSubscribeMapVisual(shardName?: string, cb?: (event: MapVisualEvent) => void): Promise; protected mapVisualEventSpec(shardName?: string): Promise; /** * Subscribe to {@link RoomEvent}s * @param roomName The name of the room * @param shardName The name of the shard to use (ignored by unofficial servers). * Defaults to {@link ScreepsClientConfig.defaultShard} if undefined. * @param cb The callback to trigger when a relevant message is received. * @throws {@link node!Error | Error} if shard and {@link ScreepsClientConfig.defaultShard} are undefined * while using an official server * @see {@link unsubscribeRoom} */ subscribeRoom(roomName: string, shardName?: string, cb?: (event: RoomEvent) => void): Promise; /** * Unsubscribe from {@link RoomEvent}s * @param roomName The name of the room * @param shardName The name of the shard to use (ignored by unofficial servers). * Defaults to {@link ScreepsClientConfig.defaultShard} if undefined. * @param cb The callback to unregister * @throws {@link node!Error | Error} if shard and {@link ScreepsClientConfig.defaultShard} are undefined * while using an official server * @see {@link subscribeRoom} */ unsubscribeRoom(roomName: string, shardName?: string, cb?: (event: RoomEvent) => void): Promise; /** * Subscribe to {@link RoomMap2Event}s * @param roomName The name of the room * @param shardName The name of the shard to use (ignored by unofficial servers). * Defaults to {@link ScreepsClientConfig.defaultShard} if undefined. * @param cb The callback to trigger when a relevant message is received. * @throws {@link node!Error | Error} if shard and {@link ScreepsClientConfig.defaultShard} are undefined * while using an official server * @see {@link unsubscribeRoomMap2} */ subscribeRoomMap2(roomName: string, shardName?: string, cb?: (event: RoomMap2Event) => void): Promise; /** * Unsubscribe from {@link RoomMap2Event}s * @param roomName The name of the room * @param shardName The name of the shard to use (ignored by unofficial servers). * Defaults to {@link ScreepsClientConfig.defaultShard} if undefined. * @param cb The callback to unregister * @throws {@link node!Error | Error} if shard and {@link ScreepsClientConfig.defaultShard} are undefined * while using an official server * @see {@link subscribeRoomMap2} */ unsubscribeRoomMap2(roomName: string, shardName?: string, cb?: (event: RoomMap2Event) => void): Promise; protected roomEventSpec(eventType: string, roomName: string, shardName?: string): string; /** * Subscribe to {@link UserCodeEvent}s * @param cb The callback to trigger when a relevant message is received. * @see {@link unsubscribeUserCode} */ subscribeUserCode(cb?: (event: UserCodeEvent) => void): Promise; /** * Unsubscribe from {@link UserCodeEvent}s * @param cb The callback to unregister * @see {@link subscribeUserCode} */ unsubscribeUserCode(cb?: (event: UserCodeEvent) => void): Promise; /** * Subscribe to {@link UserConsoleEvent}s * @param cb The callback to trigger when a relevant message is received. * @see {@link unsubscribeUserConsole} */ subscribeUserConsole(cb?: (event: UserConsoleEvent) => void): Promise; /** * Unsubscribe from {@link UserConsoleEvent}s * @param cb The callback to unregister * @see {@link subscribeUserConsole} */ unsubscribeUserConsole(cb?: (event: UserConsoleEvent) => void): Promise; /** * Subscribe to {@link UserCpuEvent}s * @param cb The callback to trigger when a relevant message is received. * @see {@link unsubscribeUserCpu} */ subscribeUserCpu(cb?: (event: UserCpuEvent) => void): Promise; /** * Unsubscribe from {@link UserCpuEvent}s * @param cb The callback to unregister * @see {@link subscribeUserCpu} */ unsubscribeUserCpu(cb?: (event: UserCpuEvent) => void): Promise; /** * Subscribe to {@link UserResourceEvent}s * @param cb The callback to trigger when a relevant message is received. * @see {@link unsubscribeUserResource} */ subscribeUserResource(cb?: (event: UserResourceEvent) => void): Promise; /** * Unsubscribe from {@link UserResourceEvent}s * @param cb The callback to unregister * @see {@link subscribeUserResource} */ unsubscribeUserResource(cb?: (event: UserResourceEvent) => void): Promise; /** * Subscribe to {@link UserMemoryEvent}s for a particular memory path * @param memoryPath The Memory path, as it would be formatted for {@link ScreepsHttpClient.userMemory.get} * @param shardName The name of the shard to use (ignored by unofficial servers). * Defaults to {@link ScreepsClientConfig.defaultShard} if undefined. * @param cb The callback to trigger when a relevant message is received. * @throws {@link node!Error | Error} if shard and {@link ScreepsClientConfig.defaultShard} are undefined * while using an official server * @see {@link unsubscribeUserMemory} */ subscribeUserMemory(memoryPath: string, shardName?: string, cb?: (event: UserMemoryEvent) => void): Promise; /** * Unsubscribe from {@link UserMemoryEvent}s for a particular memory path * @param memoryPath The Memory path, as it would be formatted for {@link ScreepsHttpClient.userMemory.get} * @param shardName The name of the shard to use (ignored by unofficial servers). * Defaults to {@link ScreepsClientConfig.defaultShard} if undefined. * @param cb The callback to unregister * @throws {@link node!Error | Error} if shard and {@link ScreepsClientConfig.defaultShard} are undefined * while using an official server * @see {@link subscribeUserMemory} */ unsubscribeUserMemory(memoryPath: string, shardName?: string, cb?: (event: UserMemoryEvent) => void): Promise; protected userMemoryEventSpec(memoryPath: string, shardName?: string): string; /** * Subscribe to an event type. * * If not authenticated, the subscribe message will be queued to be sent * once authentication is completed. * * Use of one of the more specific `subscribe___()` methods is recommended * to enforce better type checking on `cb`. * @param eventSpec The name of the event (ex: `console`, `room:${roomName}`). * Non-colon-delimited strings will be prefixed with `user:${yourUserId}/`. * @param cb The callback to trigger when a relevant message is received. * * This argument is ignored if `cb` is already registered as a listener for * this event, unless `force` is set to true. For this reason, registering * a callback via `subscribe()` is recommended over registering it separately * via {@link on}. * * This can be left undefined to resubscribe to an event using a * previously-registered callback. * @param force See `cb` param documentation * @see {@link unsubscribe} to unsubscribe from events */ subscribe(eventSpec: string, cb?: (event: E) => void, force?: boolean): Promise; /** * Unsubscribe from an event type. * * If not authenticated, the unsubscribe message will be queued to be sent * once authentication is completed. * * Use of one of the more specific `unsubscribe___()` methods is recommended * to enforce better type checking on `cb`. * @param eventSpec The type of event to unsubscribe from (ex: 'console', 'room:ROOM_NAME'). * Non-colon-delimited strings will be prefixed with `user:${yourUserId}/`. * @param cb The callback to unregister. * @see {@link unsubscribe} to subscribe to events */ unsubscribe(eventSpec: string, cb?: (event: E) => void): Promise; private normalizeEvent; /** * Authenticate to the server. This is called automatically after a * connection is successfully established. * @param token The API token with which to authenticate */ private auth; /** * Enable/disable gzip compression/deflation of messages from the server. * * Regardless of whether this is enabled or not, {@link ScreepsSocketClient} will * automatically inflate any compressed messages before notifying subscribers. * @param enabled `true` to enable compression; `false` to disable it */ gzip(enabled: boolean): void; /** * Send a message to the server. * * If the client is not currently connected, the message will be queued * to be sent when the connection is established. * * This should only be called directly if the desired functionality is not * already implemented as another method on {@link ScreepsSocketClient}. * If you have a use case for calling `send` directly, please consider * submitting a PR to add the feature to {@link ScreepsSocketClient}. * @param data the message to send */ send(data: string | WebSocket.RawData): void; /** * Process an incoming message (normalize/inflate message data, etc), * then emit events to notify the relevant subscribers. * @param rawMsg The raw message content sent by the server */ protected handleMessage(rawMsg: WebSocket.Data | { data: string; }): Promise; private inflate; } //# sourceMappingURL=ScreepsSocketClient.d.ts.map