UNPKG

9.48 kBTypeScriptView Raw
1/// <reference types="node" />
2import { Packet } from "socket.io-parser";
3import { EventParams, EventNames, EventsMap, StrictEventEmitter, DefaultEventsMap } from "./typed-events";
4import type { Client } from "./client";
5import type { Namespace } from "./namespace";
6import type { IncomingMessage, IncomingHttpHeaders } from "http";
7import type { Room, SocketId } from "socket.io-adapter";
8import type { ParsedUrlQuery } from "querystring";
9import { BroadcastOperator } from "./broadcast-operator";
10export interface SocketReservedEventsMap {
11 disconnect: (reason: string) => void;
12 disconnecting: (reason: string) => void;
13 error: (err: Error) => void;
14}
15export interface EventEmitterReservedEventsMap {
16 newListener: (eventName: string | Symbol, listener: (...args: any[]) => void) => void;
17 removeListener: (eventName: string | Symbol, listener: (...args: any[]) => void) => void;
18}
19export declare const RESERVED_EVENTS: ReadonlySet<string | Symbol>;
20/**
21 * The handshake details
22 */
23export 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}
63export declare class Socket<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents, ServerSideEvents extends EventsMap = DefaultEventsMap> extends StrictEventEmitter<ListenEvents, EmitEvents, SocketReservedEventsMap> {
64 readonly nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents>;
65 readonly client: Client<ListenEvents, EmitEvents, ServerSideEvents>;
66 readonly id: SocketId;
67 readonly handshake: Handshake;
68 /**
69 * Additional information that can be attached to the Socket instance and which will be used in the fetchSockets method
70 */
71 data: any;
72 connected: boolean;
73 disconnected: 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 */
88 constructor(nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents>, client: Client<ListenEvents, EmitEvents, ServerSideEvents>, auth: object);
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 */
101 emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
102 /**
103 * Targets a room when broadcasting.
104 *
105 * @param room
106 * @return self
107 * @public
108 */
109 to(room: Room | Room[]): BroadcastOperator<EmitEvents>;
110 /**
111 * Targets a room when broadcasting.
112 *
113 * @param room
114 * @return self
115 * @public
116 */
117 in(room: Room | Room[]): BroadcastOperator<EmitEvents>;
118 /**
119 * Excludes a room when broadcasting.
120 *
121 * @param room
122 * @return self
123 * @public
124 */
125 except(room: Room | Room[]): BroadcastOperator<EmitEvents>;
126 /**
127 * Sends a `message` event.
128 *
129 * @return self
130 * @public
131 */
132 send(...args: EventParams<EmitEvents, "message">): this;
133 /**
134 * Sends a `message` event.
135 *
136 * @return self
137 * @public
138 */
139 write(...args: EventParams<EmitEvents, "message">): this;
140 /**
141 * Writes a packet.
142 *
143 * @param {Object} packet - packet object
144 * @param {Object} opts - options
145 * @private
146 */
147 private packet;
148 /**
149 * Joins a room.
150 *
151 * @param {String|Array} rooms - room or array of rooms
152 * @return a Promise or nothing, depending on the adapter
153 * @public
154 */
155 join(rooms: Room | Array<Room>): Promise<void> | void;
156 /**
157 * Leaves a room.
158 *
159 * @param {String} room
160 * @return a Promise or nothing, depending on the adapter
161 * @public
162 */
163 leave(room: string): Promise<void> | void;
164 /**
165 * Leave all rooms.
166 *
167 * @private
168 */
169 private leaveAll;
170 /**
171 * Called by `Namespace` upon successful
172 * middleware execution (ie: authorization).
173 * Socket is added to namespace array before
174 * call to join, so adapters can access it.
175 *
176 * @private
177 */
178 _onconnect(): void;
179 /**
180 * Called with each packet. Called by `Client`.
181 *
182 * @param {Object} packet
183 * @private
184 */
185 _onpacket(packet: Packet): void;
186 /**
187 * Called upon event packet.
188 *
189 * @param {Packet} packet - packet object
190 * @private
191 */
192 private onevent;
193 /**
194 * Produces an ack callback to emit with an event.
195 *
196 * @param {Number} id - packet id
197 * @private
198 */
199 private ack;
200 /**
201 * Called upon ack packet.
202 *
203 * @private
204 */
205 private onack;
206 /**
207 * Called upon client disconnect packet.
208 *
209 * @private
210 */
211 private ondisconnect;
212 /**
213 * Handles a client error.
214 *
215 * @private
216 */
217 _onerror(err: Error): void;
218 /**
219 * Called upon closing. Called by `Client`.
220 *
221 * @param {String} reason
222 * @throw {Error} optional error object
223 *
224 * @private
225 */
226 _onclose(reason: string): this | undefined;
227 /**
228 * Produces an `error` packet.
229 *
230 * @param {Object} err - error object
231 *
232 * @private
233 */
234 _error(err: any): void;
235 /**
236 * Disconnects this client.
237 *
238 * @param {Boolean} close - if `true`, closes the underlying connection
239 * @return {Socket} self
240 *
241 * @public
242 */
243 disconnect(close?: boolean): this;
244 /**
245 * Sets the compress flag.
246 *
247 * @param {Boolean} compress - if `true`, compresses the sending data
248 * @return {Socket} self
249 * @public
250 */
251 compress(compress: boolean): this;
252 /**
253 * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
254 * receive messages (because of network slowness or other issues, or because they’re connected through long polling
255 * and is in the middle of a request-response cycle).
256 *
257 * @return {Socket} self
258 * @public
259 */
260 get volatile(): this;
261 /**
262 * Sets a modifier for a subsequent event emission that the event data will only be broadcast to every sockets but the
263 * sender.
264 *
265 * @return {Socket} self
266 * @public
267 */
268 get broadcast(): BroadcastOperator<EmitEvents>;
269 /**
270 * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
271 *
272 * @return {Socket} self
273 * @public
274 */
275 get local(): BroadcastOperator<EmitEvents>;
276 /**
277 * Dispatch incoming event to socket listeners.
278 *
279 * @param {Array} event - event that will get emitted
280 * @private
281 */
282 private dispatch;
283 /**
284 * Sets up socket middleware.
285 *
286 * @param {Function} fn - middleware function (event, next)
287 * @return {Socket} self
288 * @public
289 */
290 use(fn: (event: Array<any>, next: (err?: Error) => void) => void): this;
291 /**
292 * Executes the middleware for an incoming event.
293 *
294 * @param {Array} event - event that will get emitted
295 * @param {Function} fn - last fn call in the middleware
296 * @private
297 */
298 private run;
299 /**
300 * A reference to the request that originated the underlying Engine.IO Socket.
301 *
302 * @public
303 */
304 get request(): IncomingMessage;
305 /**
306 * A reference to the underlying Client transport connection (Engine.IO Socket object).
307 *
308 * @public
309 */
310 get conn(): import("engine.io").Socket;
311 /**
312 * @public
313 */
314 get rooms(): Set<Room>;
315 /**
316 * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
317 * callback.
318 *
319 * @param listener
320 * @public
321 */
322 onAny(listener: (...args: any[]) => void): this;
323 /**
324 * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
325 * callback. The listener is added to the beginning of the listeners array.
326 *
327 * @param listener
328 * @public
329 */
330 prependAny(listener: (...args: any[]) => void): this;
331 /**
332 * Removes the listener that will be fired when any event is emitted.
333 *
334 * @param listener
335 * @public
336 */
337 offAny(listener?: (...args: any[]) => void): this;
338 /**
339 * Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
340 * e.g. to remove listeners.
341 *
342 * @public
343 */
344 listenersAny(): ((...args: any[]) => void)[];
345 private newBroadcastOperator;
346}
347
\No newline at end of file