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