[6a3a178] | 1 | /// <reference types="node" />
|
---|
| 2 | import { Packet } from "socket.io-parser";
|
---|
[e29cc2e] | 3 | import { EventParams, EventNames, EventsMap, StrictEventEmitter, DefaultEventsMap } from "./typed-events";
|
---|
[6a3a178] | 4 | import type { Client } from "./client";
|
---|
| 5 | import type { Namespace } from "./namespace";
|
---|
| 6 | import type { IncomingMessage, IncomingHttpHeaders } from "http";
|
---|
| 7 | import type { Room, SocketId } from "socket.io-adapter";
|
---|
| 8 | import type { ParsedUrlQuery } from "querystring";
|
---|
[e29cc2e] | 9 | import { BroadcastOperator } from "./broadcast-operator";
|
---|
| 10 | export interface SocketReservedEventsMap {
|
---|
| 11 | disconnect: (reason: string) => void;
|
---|
| 12 | disconnecting: (reason: string) => void;
|
---|
| 13 | error: (err: Error) => void;
|
---|
| 14 | }
|
---|
| 15 | export interface EventEmitterReservedEventsMap {
|
---|
| 16 | newListener: (eventName: string | Symbol, listener: (...args: any[]) => void) => void;
|
---|
| 17 | removeListener: (eventName: string | Symbol, listener: (...args: any[]) => void) => void;
|
---|
| 18 | }
|
---|
[6a3a178] | 19 | export declare const RESERVED_EVENTS: ReadonlySet<string | Symbol>;
|
---|
| 20 | /**
|
---|
| 21 | * The handshake details
|
---|
| 22 | */
|
---|
| 23 | export interface Handshake {
|
---|
| 24 | /**
|
---|
| 25 | * The headers sent as part of the handshake
|
---|
| 26 | */
|
---|
| 27 | headers: IncomingHttpHeaders;
|
---|
| 28 | /**
|
---|
| 29 | * The date of creation (as string)
|
---|
| 30 | */
|
---|
| 31 | time: string;
|
---|
| 32 | /**
|
---|
| 33 | * The ip of the client
|
---|
| 34 | */
|
---|
| 35 | address: string;
|
---|
| 36 | /**
|
---|
| 37 | * Whether the connection is cross-domain
|
---|
| 38 | */
|
---|
| 39 | xdomain: boolean;
|
---|
| 40 | /**
|
---|
| 41 | * Whether the connection is secure
|
---|
| 42 | */
|
---|
| 43 | secure: boolean;
|
---|
| 44 | /**
|
---|
| 45 | * The date of creation (as unix timestamp)
|
---|
| 46 | */
|
---|
| 47 | issued: number;
|
---|
| 48 | /**
|
---|
| 49 | * The request URL string
|
---|
| 50 | */
|
---|
| 51 | url: string;
|
---|
| 52 | /**
|
---|
| 53 | * The query object
|
---|
| 54 | */
|
---|
| 55 | query: ParsedUrlQuery;
|
---|
| 56 | /**
|
---|
| 57 | * The auth object
|
---|
| 58 | */
|
---|
| 59 | auth: {
|
---|
| 60 | [key: string]: any;
|
---|
| 61 | };
|
---|
| 62 | }
|
---|
[e29cc2e] | 63 | declare type Event = [eventName: string, ...args: any[]];
|
---|
| 64 | export declare class Socket<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents, ServerSideEvents extends EventsMap = DefaultEventsMap, SocketData = any> extends StrictEventEmitter<ListenEvents, EmitEvents, SocketReservedEventsMap> {
|
---|
| 65 | readonly nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents>;
|
---|
| 66 | readonly client: Client<ListenEvents, EmitEvents, ServerSideEvents>;
|
---|
[6a3a178] | 67 | readonly id: SocketId;
|
---|
| 68 | readonly handshake: Handshake;
|
---|
[e29cc2e] | 69 | /**
|
---|
| 70 | * Additional information that can be attached to the Socket instance and which will be used in the fetchSockets method
|
---|
| 71 | */
|
---|
| 72 | data: Partial<SocketData>;
|
---|
[6a3a178] | 73 | connected: boolean;
|
---|
| 74 | private readonly server;
|
---|
| 75 | private readonly adapter;
|
---|
| 76 | private acks;
|
---|
| 77 | private fns;
|
---|
| 78 | private flags;
|
---|
| 79 | private _anyListeners?;
|
---|
| 80 | /**
|
---|
| 81 | * Interface to a `Client` for a given `Namespace`.
|
---|
| 82 | *
|
---|
| 83 | * @param {Namespace} nsp
|
---|
| 84 | * @param {Client} client
|
---|
| 85 | * @param {Object} auth
|
---|
| 86 | * @package
|
---|
| 87 | */
|
---|
[e29cc2e] | 88 | constructor(nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents>, client: Client<ListenEvents, EmitEvents, ServerSideEvents>, auth: object);
|
---|
[6a3a178] | 89 | /**
|
---|
| 90 | * Builds the `handshake` BC object
|
---|
| 91 | *
|
---|
| 92 | * @private
|
---|
| 93 | */
|
---|
| 94 | private buildHandshake;
|
---|
| 95 | /**
|
---|
| 96 | * Emits to this client.
|
---|
| 97 | *
|
---|
| 98 | * @return Always returns `true`.
|
---|
| 99 | * @public
|
---|
| 100 | */
|
---|
[e29cc2e] | 101 | emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
|
---|
| 102 | /**
|
---|
| 103 | * @private
|
---|
| 104 | */
|
---|
| 105 | private registerAckCallback;
|
---|
[6a3a178] | 106 | /**
|
---|
| 107 | * Targets a room when broadcasting.
|
---|
| 108 | *
|
---|
[e29cc2e] | 109 | * @param room
|
---|
[6a3a178] | 110 | * @return self
|
---|
| 111 | * @public
|
---|
| 112 | */
|
---|
[e29cc2e] | 113 | to(room: Room | Room[]): BroadcastOperator<EmitEvents>;
|
---|
[6a3a178] | 114 | /**
|
---|
| 115 | * Targets a room when broadcasting.
|
---|
| 116 | *
|
---|
[e29cc2e] | 117 | * @param room
|
---|
[6a3a178] | 118 | * @return self
|
---|
| 119 | * @public
|
---|
| 120 | */
|
---|
[e29cc2e] | 121 | in(room: Room | Room[]): BroadcastOperator<EmitEvents>;
|
---|
| 122 | /**
|
---|
| 123 | * Excludes a room when broadcasting.
|
---|
| 124 | *
|
---|
| 125 | * @param room
|
---|
| 126 | * @return self
|
---|
| 127 | * @public
|
---|
| 128 | */
|
---|
| 129 | except(room: Room | Room[]): BroadcastOperator<EmitEvents>;
|
---|
[6a3a178] | 130 | /**
|
---|
| 131 | * Sends a `message` event.
|
---|
| 132 | *
|
---|
| 133 | * @return self
|
---|
| 134 | * @public
|
---|
| 135 | */
|
---|
[e29cc2e] | 136 | send(...args: EventParams<EmitEvents, "message">): this;
|
---|
[6a3a178] | 137 | /**
|
---|
| 138 | * Sends a `message` event.
|
---|
| 139 | *
|
---|
| 140 | * @return self
|
---|
| 141 | * @public
|
---|
| 142 | */
|
---|
[e29cc2e] | 143 | write(...args: EventParams<EmitEvents, "message">): this;
|
---|
[6a3a178] | 144 | /**
|
---|
| 145 | * Writes a packet.
|
---|
| 146 | *
|
---|
| 147 | * @param {Object} packet - packet object
|
---|
| 148 | * @param {Object} opts - options
|
---|
| 149 | * @private
|
---|
| 150 | */
|
---|
| 151 | private packet;
|
---|
| 152 | /**
|
---|
| 153 | * Joins a room.
|
---|
| 154 | *
|
---|
| 155 | * @param {String|Array} rooms - room or array of rooms
|
---|
| 156 | * @return a Promise or nothing, depending on the adapter
|
---|
| 157 | * @public
|
---|
| 158 | */
|
---|
| 159 | join(rooms: Room | Array<Room>): Promise<void> | void;
|
---|
| 160 | /**
|
---|
| 161 | * Leaves a room.
|
---|
| 162 | *
|
---|
| 163 | * @param {String} room
|
---|
| 164 | * @return a Promise or nothing, depending on the adapter
|
---|
| 165 | * @public
|
---|
| 166 | */
|
---|
| 167 | leave(room: string): Promise<void> | void;
|
---|
| 168 | /**
|
---|
| 169 | * Leave all rooms.
|
---|
| 170 | *
|
---|
| 171 | * @private
|
---|
| 172 | */
|
---|
| 173 | private leaveAll;
|
---|
| 174 | /**
|
---|
| 175 | * Called by `Namespace` upon successful
|
---|
| 176 | * middleware execution (ie: authorization).
|
---|
| 177 | * Socket is added to namespace array before
|
---|
| 178 | * call to join, so adapters can access it.
|
---|
| 179 | *
|
---|
| 180 | * @private
|
---|
| 181 | */
|
---|
| 182 | _onconnect(): void;
|
---|
| 183 | /**
|
---|
| 184 | * Called with each packet. Called by `Client`.
|
---|
| 185 | *
|
---|
| 186 | * @param {Object} packet
|
---|
| 187 | * @private
|
---|
| 188 | */
|
---|
| 189 | _onpacket(packet: Packet): void;
|
---|
| 190 | /**
|
---|
| 191 | * Called upon event packet.
|
---|
| 192 | *
|
---|
| 193 | * @param {Packet} packet - packet object
|
---|
| 194 | * @private
|
---|
| 195 | */
|
---|
| 196 | private onevent;
|
---|
| 197 | /**
|
---|
| 198 | * Produces an ack callback to emit with an event.
|
---|
| 199 | *
|
---|
| 200 | * @param {Number} id - packet id
|
---|
| 201 | * @private
|
---|
| 202 | */
|
---|
| 203 | private ack;
|
---|
| 204 | /**
|
---|
| 205 | * Called upon ack packet.
|
---|
| 206 | *
|
---|
| 207 | * @private
|
---|
| 208 | */
|
---|
| 209 | private onack;
|
---|
| 210 | /**
|
---|
| 211 | * Called upon client disconnect packet.
|
---|
| 212 | *
|
---|
| 213 | * @private
|
---|
| 214 | */
|
---|
| 215 | private ondisconnect;
|
---|
| 216 | /**
|
---|
| 217 | * Handles a client error.
|
---|
| 218 | *
|
---|
| 219 | * @private
|
---|
| 220 | */
|
---|
[e29cc2e] | 221 | _onerror(err: Error): void;
|
---|
[6a3a178] | 222 | /**
|
---|
| 223 | * Called upon closing. Called by `Client`.
|
---|
| 224 | *
|
---|
| 225 | * @param {String} reason
|
---|
| 226 | * @throw {Error} optional error object
|
---|
| 227 | *
|
---|
| 228 | * @private
|
---|
| 229 | */
|
---|
| 230 | _onclose(reason: string): this | undefined;
|
---|
| 231 | /**
|
---|
| 232 | * Produces an `error` packet.
|
---|
| 233 | *
|
---|
| 234 | * @param {Object} err - error object
|
---|
| 235 | *
|
---|
| 236 | * @private
|
---|
| 237 | */
|
---|
| 238 | _error(err: any): void;
|
---|
| 239 | /**
|
---|
| 240 | * Disconnects this client.
|
---|
| 241 | *
|
---|
| 242 | * @param {Boolean} close - if `true`, closes the underlying connection
|
---|
| 243 | * @return {Socket} self
|
---|
| 244 | *
|
---|
| 245 | * @public
|
---|
| 246 | */
|
---|
| 247 | disconnect(close?: boolean): this;
|
---|
| 248 | /**
|
---|
| 249 | * Sets the compress flag.
|
---|
| 250 | *
|
---|
| 251 | * @param {Boolean} compress - if `true`, compresses the sending data
|
---|
| 252 | * @return {Socket} self
|
---|
| 253 | * @public
|
---|
| 254 | */
|
---|
| 255 | compress(compress: boolean): this;
|
---|
| 256 | /**
|
---|
| 257 | * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
|
---|
| 258 | * receive messages (because of network slowness or other issues, or because they’re connected through long polling
|
---|
| 259 | * and is in the middle of a request-response cycle).
|
---|
| 260 | *
|
---|
| 261 | * @return {Socket} self
|
---|
| 262 | * @public
|
---|
| 263 | */
|
---|
| 264 | get volatile(): this;
|
---|
| 265 | /**
|
---|
| 266 | * Sets a modifier for a subsequent event emission that the event data will only be broadcast to every sockets but the
|
---|
| 267 | * sender.
|
---|
| 268 | *
|
---|
| 269 | * @return {Socket} self
|
---|
| 270 | * @public
|
---|
| 271 | */
|
---|
[e29cc2e] | 272 | get broadcast(): BroadcastOperator<EmitEvents>;
|
---|
[6a3a178] | 273 | /**
|
---|
| 274 | * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
|
---|
| 275 | *
|
---|
| 276 | * @return {Socket} self
|
---|
| 277 | * @public
|
---|
| 278 | */
|
---|
[e29cc2e] | 279 | get local(): BroadcastOperator<EmitEvents>;
|
---|
| 280 | /**
|
---|
| 281 | * Sets a modifier for a subsequent event emission that the callback will be called with an error when the
|
---|
| 282 | * given number of milliseconds have elapsed without an acknowledgement from the client:
|
---|
| 283 | *
|
---|
| 284 | * ```
|
---|
| 285 | * socket.timeout(5000).emit("my-event", (err) => {
|
---|
| 286 | * if (err) {
|
---|
| 287 | * // the client did not acknowledge the event in the given delay
|
---|
| 288 | * }
|
---|
| 289 | * });
|
---|
| 290 | * ```
|
---|
| 291 | *
|
---|
| 292 | * @returns self
|
---|
| 293 | * @public
|
---|
| 294 | */
|
---|
| 295 | timeout(timeout: number): this;
|
---|
[6a3a178] | 296 | /**
|
---|
| 297 | * Dispatch incoming event to socket listeners.
|
---|
| 298 | *
|
---|
| 299 | * @param {Array} event - event that will get emitted
|
---|
| 300 | * @private
|
---|
| 301 | */
|
---|
| 302 | private dispatch;
|
---|
| 303 | /**
|
---|
| 304 | * Sets up socket middleware.
|
---|
| 305 | *
|
---|
| 306 | * @param {Function} fn - middleware function (event, next)
|
---|
| 307 | * @return {Socket} self
|
---|
| 308 | * @public
|
---|
| 309 | */
|
---|
[e29cc2e] | 310 | use(fn: (event: Event, next: (err?: Error) => void) => void): this;
|
---|
[6a3a178] | 311 | /**
|
---|
| 312 | * Executes the middleware for an incoming event.
|
---|
| 313 | *
|
---|
| 314 | * @param {Array} event - event that will get emitted
|
---|
| 315 | * @param {Function} fn - last fn call in the middleware
|
---|
| 316 | * @private
|
---|
| 317 | */
|
---|
| 318 | private run;
|
---|
[e29cc2e] | 319 | /**
|
---|
| 320 | * Whether the socket is currently disconnected
|
---|
| 321 | */
|
---|
| 322 | get disconnected(): boolean;
|
---|
[6a3a178] | 323 | /**
|
---|
| 324 | * A reference to the request that originated the underlying Engine.IO Socket.
|
---|
| 325 | *
|
---|
| 326 | * @public
|
---|
| 327 | */
|
---|
| 328 | get request(): IncomingMessage;
|
---|
| 329 | /**
|
---|
| 330 | * A reference to the underlying Client transport connection (Engine.IO Socket object).
|
---|
| 331 | *
|
---|
| 332 | * @public
|
---|
| 333 | */
|
---|
[e29cc2e] | 334 | get conn(): import("engine.io").Socket;
|
---|
[6a3a178] | 335 | /**
|
---|
| 336 | * @public
|
---|
| 337 | */
|
---|
| 338 | get rooms(): Set<Room>;
|
---|
| 339 | /**
|
---|
| 340 | * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
|
---|
| 341 | * callback.
|
---|
| 342 | *
|
---|
| 343 | * @param listener
|
---|
| 344 | * @public
|
---|
| 345 | */
|
---|
| 346 | onAny(listener: (...args: any[]) => void): this;
|
---|
| 347 | /**
|
---|
| 348 | * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
|
---|
| 349 | * callback. The listener is added to the beginning of the listeners array.
|
---|
| 350 | *
|
---|
| 351 | * @param listener
|
---|
| 352 | * @public
|
---|
| 353 | */
|
---|
| 354 | prependAny(listener: (...args: any[]) => void): this;
|
---|
| 355 | /**
|
---|
| 356 | * Removes the listener that will be fired when any event is emitted.
|
---|
| 357 | *
|
---|
| 358 | * @param listener
|
---|
| 359 | * @public
|
---|
| 360 | */
|
---|
| 361 | offAny(listener?: (...args: any[]) => void): this;
|
---|
| 362 | /**
|
---|
| 363 | * Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
|
---|
| 364 | * e.g. to remove listeners.
|
---|
| 365 | *
|
---|
| 366 | * @public
|
---|
| 367 | */
|
---|
| 368 | listenersAny(): ((...args: any[]) => void)[];
|
---|
[e29cc2e] | 369 | private newBroadcastOperator;
|
---|
[6a3a178] | 370 | }
|
---|
[e29cc2e] | 371 | export {};
|
---|