181 lines
4.5 KiB
TypeScript
Raw Normal View History

2023-10-10 22:00:26 +02:00
import { EventEmitter } from "events";
2025-01-25 10:46:42 +01:00
import type { IncomingMessage } from "http";
import type { EngineRequest, Transport } from "./transport";
import type { BaseServer } from "./server";
import type { RawData } from "engine.io-parser";
export interface SendOptions {
compress?: boolean;
}
type ReadyState = "opening" | "open" | "closing" | "closed";
type SendCallback = (transport: Transport) => void;
2023-10-10 22:00:26 +02:00
export declare class Socket extends EventEmitter {
2025-01-25 10:46:42 +01:00
/**
* The revision of the protocol:
*
* - 3rd is used in Engine.IO v3 / Socket.IO v2
* - 4th is used in Engine.IO v4 and above / Socket.IO v3 and above
*
* It is found in the `EIO` query parameters of the HTTP requests.
*
* @see https://github.com/socketio/engine.io-protocol
*/
2023-10-10 22:00:26 +02:00
readonly protocol: number;
2025-01-25 10:46:42 +01:00
/**
* A reference to the first HTTP request of the session
*
* TODO for the next major release: remove it
*/
request: IncomingMessage;
/**
* The IP address of the client.
*/
2023-10-10 22:00:26 +02:00
readonly remoteAddress: string;
2025-01-25 10:46:42 +01:00
/**
* The current state of the socket.
*/
_readyState: ReadyState;
/**
* The current low-level transport.
*/
2023-10-10 22:00:26 +02:00
transport: Transport;
private server;
2025-01-25 10:46:42 +01:00
upgrading: boolean;
upgraded: boolean;
2023-10-10 22:00:26 +02:00
private writeBuffer;
private packetsFn;
private sentCallbackFn;
private cleanupFn;
private pingTimeoutTimer;
private pingIntervalTimer;
/**
2025-01-25 10:46:42 +01:00
* This is the session identifier that the client will use in the subsequent HTTP requests. It must not be shared with
* others parties, as it might lead to session hijacking.
2023-10-10 22:00:26 +02:00
*
2025-01-25 10:46:42 +01:00
* @private
2023-10-10 22:00:26 +02:00
*/
2025-01-25 10:46:42 +01:00
private readonly id;
get readyState(): ReadyState;
set readyState(state: ReadyState);
constructor(id: string, server: BaseServer, transport: Transport, req: EngineRequest, protocol: number);
2023-10-10 22:00:26 +02:00
/**
* Called upon transport considered open.
*
2025-01-25 10:46:42 +01:00
* @private
2023-10-10 22:00:26 +02:00
*/
private onOpen;
/**
* Called upon transport packet.
*
* @param {Object} packet
2025-01-25 10:46:42 +01:00
* @private
2023-10-10 22:00:26 +02:00
*/
private onPacket;
/**
* Called upon transport error.
*
2025-01-25 10:46:42 +01:00
* @param {Error} err - error object
* @private
2023-10-10 22:00:26 +02:00
*/
private onError;
/**
* Pings client every `this.pingInterval` and expects response
* within `this.pingTimeout` or closes connection.
*
2025-01-25 10:46:42 +01:00
* @private
2023-10-10 22:00:26 +02:00
*/
private schedulePing;
/**
* Resets ping timeout.
*
2025-01-25 10:46:42 +01:00
* @private
2023-10-10 22:00:26 +02:00
*/
private resetPingTimeout;
/**
* Attaches handlers for the given transport.
*
* @param {Transport} transport
2025-01-25 10:46:42 +01:00
* @private
2023-10-10 22:00:26 +02:00
*/
private setTransport;
2025-01-25 10:46:42 +01:00
/**
* Upon transport "drain" event
*
* @private
*/
private onDrain;
2023-10-10 22:00:26 +02:00
/**
* Upgrades socket to the given transport
*
* @param {Transport} transport
2025-01-25 10:46:42 +01:00
* @private
2023-10-10 22:00:26 +02:00
*/
2025-01-25 10:46:42 +01:00
_maybeUpgrade(transport: Transport): void;
2023-10-10 22:00:26 +02:00
/**
* Clears listeners and timers associated with current transport.
*
2025-01-25 10:46:42 +01:00
* @private
2023-10-10 22:00:26 +02:00
*/
private clearTransport;
/**
* Called upon transport considered closed.
* Possible reasons: `ping timeout`, `client error`, `parse error`,
* `transport error`, `server close`, `transport close`
*/
private onClose;
/**
* Sends a message packet.
*
* @param {Object} data
* @param {Object} options
* @param {Function} callback
* @return {Socket} for chaining
*/
2025-01-25 10:46:42 +01:00
send(data: RawData, options?: SendOptions, callback?: SendCallback): this;
/**
* Alias of {@link send}.
*
* @param data
* @param options
* @param callback
*/
write(data: RawData, options?: SendOptions, callback?: SendCallback): this;
2023-10-10 22:00:26 +02:00
/**
* Sends a packet.
*
* @param {String} type - packet type
* @param {String} data
* @param {Object} options
* @param {Function} callback
*
2025-01-25 10:46:42 +01:00
* @private
2023-10-10 22:00:26 +02:00
*/
private sendPacket;
/**
* Attempts to flush the packets buffer.
*
2025-01-25 10:46:42 +01:00
* @private
2023-10-10 22:00:26 +02:00
*/
private flush;
/**
* Get available upgrades for this socket.
*
2025-01-25 10:46:42 +01:00
* @private
2023-10-10 22:00:26 +02:00
*/
private getAvailableUpgrades;
/**
* Closes the socket and underlying transport.
*
* @param {Boolean} discard - optional, discard the transport
* @return {Socket} for chaining
*/
close(discard?: boolean): void;
/**
* Closes the underlying transport.
*
* @param {Boolean} discard
2025-01-25 10:46:42 +01:00
* @private
2023-10-10 22:00:26 +02:00
*/
private closeTransport;
}
2025-01-25 10:46:42 +01:00
export {};