@trigger.dev/core 3.0.0-beta.17 → 3.0.0-beta.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/catalog-A-D3UC6S.d.mts +6646 -0
- package/dist/catalog-TAZd4-TP.d.ts +6646 -0
- package/dist/manager-M9GLDnhJ.d.mts +1158 -0
- package/dist/manager-M9GLDnhJ.d.ts +1158 -0
- package/dist/messages-AriaDDm0.d.mts +8696 -0
- package/dist/messages-AriaDDm0.d.ts +8696 -0
- package/dist/v3/dev/index.d.mts +28 -0
- package/dist/v3/dev/index.d.ts +28 -0
- package/dist/v3/dev/index.js +93 -0
- package/dist/v3/dev/index.js.map +1 -0
- package/dist/v3/dev/index.mjs +91 -0
- package/dist/v3/dev/index.mjs.map +1 -0
- package/dist/v3/index.d.mts +299 -17348
- package/dist/v3/index.d.ts +299 -17348
- package/dist/v3/index.js +2091 -3860
- package/dist/v3/index.js.map +1 -1
- package/dist/v3/index.mjs +2093 -3836
- package/dist/v3/index.mjs.map +1 -1
- package/dist/v3/otel/index.js +62 -38
- package/dist/v3/otel/index.js.map +1 -1
- package/dist/v3/otel/index.mjs +62 -38
- package/dist/v3/otel/index.mjs.map +1 -1
- package/dist/v3/prod/index.d.mts +45 -0
- package/dist/v3/prod/index.d.ts +45 -0
- package/dist/v3/prod/index.js +205 -0
- package/dist/v3/prod/index.js.map +1 -0
- package/dist/v3/prod/index.mjs +203 -0
- package/dist/v3/prod/index.mjs.map +1 -0
- package/dist/v3/utils/structuredLogger.d.mts +31 -0
- package/dist/v3/utils/structuredLogger.d.ts +31 -0
- package/dist/v3/utils/structuredLogger.js +88 -0
- package/dist/v3/utils/structuredLogger.js.map +1 -0
- package/dist/v3/utils/structuredLogger.mjs +86 -0
- package/dist/v3/utils/structuredLogger.mjs.map +1 -0
- package/dist/v3/workers/index.d.mts +95 -0
- package/dist/v3/workers/index.d.ts +95 -0
- package/dist/v3/workers/index.js +2663 -0
- package/dist/v3/workers/index.js.map +1 -0
- package/dist/v3/workers/index.mjs +2647 -0
- package/dist/v3/workers/index.mjs.map +1 -0
- package/dist/v3/zodIpc.d.mts +32 -0
- package/dist/v3/zodIpc.d.ts +32 -0
- package/dist/v3/zodIpc.js +268 -0
- package/dist/v3/zodIpc.js.map +1 -0
- package/dist/v3/zodIpc.mjs +266 -0
- package/dist/v3/zodIpc.mjs.map +1 -0
- package/dist/v3/zodMessageHandler.d.mts +69 -0
- package/dist/v3/zodMessageHandler.d.ts +69 -0
- package/dist/v3/zodMessageHandler.js +168 -0
- package/dist/v3/zodMessageHandler.js.map +1 -0
- package/dist/v3/zodMessageHandler.mjs +163 -0
- package/dist/v3/zodMessageHandler.mjs.map +1 -0
- package/dist/v3/zodNamespace.d.mts +3663 -0
- package/dist/v3/zodNamespace.d.ts +3663 -0
- package/dist/v3/zodNamespace.js +356 -0
- package/dist/v3/zodNamespace.js.map +1 -0
- package/dist/v3/zodNamespace.mjs +354 -0
- package/dist/v3/zodNamespace.mjs.map +1 -0
- package/dist/v3/zodSocket.d.mts +88 -0
- package/dist/v3/zodSocket.d.ts +88 -0
- package/dist/v3/zodSocket.js +309 -0
- package/dist/v3/zodSocket.js.map +1 -0
- package/dist/v3/zodSocket.mjs +305 -0
- package/dist/v3/zodSocket.mjs.map +1 -0
- package/package.json +69 -5
|
@@ -0,0 +1,3663 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { EventEmitter } from 'events';
|
|
3
|
+
import http, { IncomingMessage, ServerResponse, IncomingHttpHeaders } from 'http';
|
|
4
|
+
import { Server as Server$2 } from 'https';
|
|
5
|
+
import { Http2SecureServer, Http2Server } from 'http2';
|
|
6
|
+
import { CookieSerializeOptions } from 'cookie';
|
|
7
|
+
import { CorsOptions, CorsOptionsDelegate } from 'cors';
|
|
8
|
+
import { ParsedUrlQuery } from 'querystring';
|
|
9
|
+
import { ZodMessageSender } from './zodMessageHandler.mjs';
|
|
10
|
+
import { ZodSocketMessageCatalogSchema, ZodMessageCatalogToSocketIoEvents, ZodSocketMessageHandlers, ZodSocketMessageHandler } from './zodSocket.mjs';
|
|
11
|
+
import { z } from 'zod';
|
|
12
|
+
import { StructuredLogger } from './utils/structuredLogger.mjs';
|
|
13
|
+
import 'socket.io-client';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* An events map is an interface that maps event names to their value, which
|
|
17
|
+
* represents the type of the `on` listener.
|
|
18
|
+
*/
|
|
19
|
+
interface EventsMap$2 {
|
|
20
|
+
[event: string]: any;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The default events map, used if no EventsMap is given. Using this EventsMap
|
|
24
|
+
* is equivalent to accepting all event names, and any data.
|
|
25
|
+
*/
|
|
26
|
+
interface DefaultEventsMap$1 {
|
|
27
|
+
[event: string]: (...args: any[]) => void;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Returns a union type containing all the keys of an event map.
|
|
31
|
+
*/
|
|
32
|
+
declare type EventNames$1<Map extends EventsMap$2> = keyof Map & (string | symbol);
|
|
33
|
+
/**
|
|
34
|
+
* Returns a union type containing all the keys of an event map that have an acknowledgement callback.
|
|
35
|
+
*
|
|
36
|
+
* That also have *some* data coming in.
|
|
37
|
+
*/
|
|
38
|
+
declare type EventNamesWithAck<Map extends EventsMap$2, K extends EventNames$1<Map> = EventNames$1<Map>> = IfAny<Last<Parameters<Map[K]>> | Map[K], K, K extends (Last<Parameters<Map[K]>> extends (...args: any[]) => any ? FirstNonErrorArg<Last<Parameters<Map[K]>>> extends void ? never : K : never) ? K : never>;
|
|
39
|
+
/**
|
|
40
|
+
* Returns a union type containing all the keys of an event map that have an acknowledgement callback.
|
|
41
|
+
*
|
|
42
|
+
* That also have *some* data coming in.
|
|
43
|
+
*/
|
|
44
|
+
declare type EventNamesWithoutAck<Map extends EventsMap$2, K extends EventNames$1<Map> = EventNames$1<Map>> = IfAny<Last<Parameters<Map[K]>> | Map[K], K, K extends (Parameters<Map[K]> extends never[] ? K : never) ? K : K extends (Last<Parameters<Map[K]>> extends (...args: any[]) => any ? never : K) ? K : never>;
|
|
45
|
+
declare type RemoveAcknowledgements<E extends EventsMap$2> = {
|
|
46
|
+
[K in EventNamesWithoutAck<E>]: E[K];
|
|
47
|
+
};
|
|
48
|
+
declare type EventNamesWithError<Map extends EventsMap$2, K extends EventNamesWithAck<Map> = EventNamesWithAck<Map>> = IfAny<Last<Parameters<Map[K]>> | Map[K], K, K extends (LooseParameters<Last<Parameters<Map[K]>>>[0] extends Error ? K : never) ? K : never>;
|
|
49
|
+
/** The tuple type representing the parameters of an event listener */
|
|
50
|
+
declare type EventParams$1<Map extends EventsMap$2, Ev extends EventNames$1<Map>> = Parameters<Map[Ev]>;
|
|
51
|
+
/**
|
|
52
|
+
* The event names that are either in ReservedEvents or in UserEvents
|
|
53
|
+
*/
|
|
54
|
+
declare type ReservedOrUserEventNames$1<ReservedEventsMap extends EventsMap$2, UserEvents extends EventsMap$2> = EventNames$1<ReservedEventsMap> | EventNames$1<UserEvents>;
|
|
55
|
+
/**
|
|
56
|
+
* Type of a listener of a user event or a reserved event. If `Ev` is in
|
|
57
|
+
* `ReservedEvents`, the reserved event listener is returned.
|
|
58
|
+
*/
|
|
59
|
+
declare type ReservedOrUserListener$1<ReservedEvents extends EventsMap$2, UserEvents extends EventsMap$2, Ev extends ReservedOrUserEventNames$1<ReservedEvents, UserEvents>> = FallbackToUntypedListener$1<Ev extends EventNames$1<ReservedEvents> ? ReservedEvents[Ev] : Ev extends EventNames$1<UserEvents> ? UserEvents[Ev] : never>;
|
|
60
|
+
/**
|
|
61
|
+
* Returns an untyped listener type if `T` is `never`; otherwise, returns `T`.
|
|
62
|
+
*
|
|
63
|
+
* This is a hack to mitigate https://github.com/socketio/socket.io/issues/3833.
|
|
64
|
+
* Needed because of https://github.com/microsoft/TypeScript/issues/41778
|
|
65
|
+
*/
|
|
66
|
+
declare type FallbackToUntypedListener$1<T> = [T] extends [never] ? (...args: any[]) => void | Promise<void> : T;
|
|
67
|
+
/**
|
|
68
|
+
* Interface for classes that aren't `EventEmitter`s, but still expose a
|
|
69
|
+
* strictly typed `emit` method.
|
|
70
|
+
*/
|
|
71
|
+
interface TypedEventBroadcaster<EmitEvents extends EventsMap$2> {
|
|
72
|
+
emit<Ev extends EventNames$1<EmitEvents>>(ev: Ev, ...args: EventParams$1<EmitEvents, Ev>): boolean;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Strictly typed version of an `EventEmitter`. A `TypedEventEmitter` takes type
|
|
76
|
+
* parameters for mappings of event names to event data types, and strictly
|
|
77
|
+
* types method calls to the `EventEmitter` according to these event maps.
|
|
78
|
+
*
|
|
79
|
+
* @typeParam ListenEvents - `EventsMap` of user-defined events that can be
|
|
80
|
+
* listened to with `on` or `once`
|
|
81
|
+
* @typeParam EmitEvents - `EventsMap` of user-defined events that can be
|
|
82
|
+
* emitted with `emit`
|
|
83
|
+
* @typeParam ReservedEvents - `EventsMap` of reserved events, that can be
|
|
84
|
+
* emitted by socket.io with `emitReserved`, and can be listened to with
|
|
85
|
+
* `listen`.
|
|
86
|
+
*/
|
|
87
|
+
declare abstract class StrictEventEmitter<ListenEvents extends EventsMap$2, EmitEvents extends EventsMap$2, ReservedEvents extends EventsMap$2 = {}> extends EventEmitter implements TypedEventBroadcaster<EmitEvents> {
|
|
88
|
+
/**
|
|
89
|
+
* Adds the `listener` function as an event listener for `ev`.
|
|
90
|
+
*
|
|
91
|
+
* @param ev Name of the event
|
|
92
|
+
* @param listener Callback function
|
|
93
|
+
*/
|
|
94
|
+
on<Ev extends ReservedOrUserEventNames$1<ReservedEvents, ListenEvents>>(ev: Ev, listener: ReservedOrUserListener$1<ReservedEvents, ListenEvents, Ev>): this;
|
|
95
|
+
/**
|
|
96
|
+
* Adds a one-time `listener` function as an event listener for `ev`.
|
|
97
|
+
*
|
|
98
|
+
* @param ev Name of the event
|
|
99
|
+
* @param listener Callback function
|
|
100
|
+
*/
|
|
101
|
+
once<Ev extends ReservedOrUserEventNames$1<ReservedEvents, ListenEvents>>(ev: Ev, listener: ReservedOrUserListener$1<ReservedEvents, ListenEvents, Ev>): this;
|
|
102
|
+
/**
|
|
103
|
+
* Emits an event.
|
|
104
|
+
*
|
|
105
|
+
* @param ev Name of the event
|
|
106
|
+
* @param args Values to send to listeners of this event
|
|
107
|
+
*/
|
|
108
|
+
emit<Ev extends EventNames$1<EmitEvents>>(ev: Ev, ...args: EventParams$1<EmitEvents, Ev>): boolean;
|
|
109
|
+
/**
|
|
110
|
+
* Emits a reserved event.
|
|
111
|
+
*
|
|
112
|
+
* This method is `protected`, so that only a class extending
|
|
113
|
+
* `StrictEventEmitter` can emit its own reserved events.
|
|
114
|
+
*
|
|
115
|
+
* @param ev Reserved event name
|
|
116
|
+
* @param args Arguments to emit along with the event
|
|
117
|
+
*/
|
|
118
|
+
protected emitReserved<Ev extends EventNames$1<ReservedEvents>>(ev: Ev, ...args: EventParams$1<ReservedEvents, Ev>): boolean;
|
|
119
|
+
/**
|
|
120
|
+
* Emits an event.
|
|
121
|
+
*
|
|
122
|
+
* This method is `protected`, so that only a class extending
|
|
123
|
+
* `StrictEventEmitter` can get around the strict typing. This is useful for
|
|
124
|
+
* calling `emit.apply`, which can be called as `emitUntyped.apply`.
|
|
125
|
+
*
|
|
126
|
+
* @param ev Event name
|
|
127
|
+
* @param args Arguments to emit along with the event
|
|
128
|
+
*/
|
|
129
|
+
protected emitUntyped(ev: string, ...args: any[]): boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Returns the listeners listening to an event.
|
|
132
|
+
*
|
|
133
|
+
* @param event Event name
|
|
134
|
+
* @returns Array of listeners subscribed to `event`
|
|
135
|
+
*/
|
|
136
|
+
listeners<Ev extends ReservedOrUserEventNames$1<ReservedEvents, ListenEvents>>(event: Ev): ReservedOrUserListener$1<ReservedEvents, ListenEvents, Ev>[];
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Returns a boolean for whether the given type is `any`.
|
|
140
|
+
*
|
|
141
|
+
* @link https://stackoverflow.com/a/49928360/1490091
|
|
142
|
+
*
|
|
143
|
+
* Useful in type utilities, such as disallowing `any`s to be passed to a function.
|
|
144
|
+
*
|
|
145
|
+
* @author sindresorhus
|
|
146
|
+
* @link https://github.com/sindresorhus/type-fest
|
|
147
|
+
*/
|
|
148
|
+
declare type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
149
|
+
/**
|
|
150
|
+
* An if-else-like type that resolves depending on whether the given type is `any`.
|
|
151
|
+
*
|
|
152
|
+
* @see {@link IsAny}
|
|
153
|
+
*
|
|
154
|
+
* @author sindresorhus
|
|
155
|
+
* @link https://github.com/sindresorhus/type-fest
|
|
156
|
+
*/
|
|
157
|
+
declare type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = IsAny<T> extends true ? TypeIfAny : TypeIfNotAny;
|
|
158
|
+
/**
|
|
159
|
+
* Extracts the type of the last element of an array.
|
|
160
|
+
*
|
|
161
|
+
* Use-case: Defining the return type of functions that extract the last element of an array, for example [`lodash.last`](https://lodash.com/docs/4.17.15#last).
|
|
162
|
+
*
|
|
163
|
+
* @author sindresorhus
|
|
164
|
+
* @link https://github.com/sindresorhus/type-fest
|
|
165
|
+
*/
|
|
166
|
+
declare type Last<ValueType extends readonly unknown[]> = ValueType extends readonly [infer ElementType] ? ElementType : ValueType extends readonly [infer _, ...infer Tail] ? Last<Tail> : ValueType extends ReadonlyArray<infer ElementType> ? ElementType : never;
|
|
167
|
+
declare type FirstNonErrorTuple<T extends unknown[]> = T[0] extends Error ? T[1] : T[0];
|
|
168
|
+
declare type AllButLast<T extends any[]> = T extends [...infer H, infer L] ? H : any[];
|
|
169
|
+
/**
|
|
170
|
+
* Like `Parameters<T>`, but doesn't require `T` to be a function ahead of time.
|
|
171
|
+
*/
|
|
172
|
+
declare type LooseParameters<T> = T extends (...args: infer P) => any ? P : never;
|
|
173
|
+
declare type FirstNonErrorArg<T> = T extends (...args: infer Params) => any ? FirstNonErrorTuple<Params> : any;
|
|
174
|
+
declare type PrependTimeoutError<T extends any[]> = {
|
|
175
|
+
[K in keyof T]: T[K] extends (...args: infer Params) => infer Result ? Params[0] extends Error ? T[K] : (err: Error, ...args: Params) => Result : T[K];
|
|
176
|
+
};
|
|
177
|
+
declare type MultiplyArray<T extends unknown[]> = {
|
|
178
|
+
[K in keyof T]: T[K][];
|
|
179
|
+
};
|
|
180
|
+
declare type InferFirstAndPreserveLabel<T extends any[]> = T extends [any, ...infer R] ? T extends [...infer H, ...R] ? H : never : never;
|
|
181
|
+
/**
|
|
182
|
+
* Utility type to decorate the acknowledgement callbacks multiple values
|
|
183
|
+
* on the first non error element while removing any elements after
|
|
184
|
+
*/
|
|
185
|
+
declare type ExpectMultipleResponses<T extends any[]> = {
|
|
186
|
+
[K in keyof T]: T[K] extends (...args: infer Params) => infer Result ? Params extends [Error] ? (err: Error) => Result : Params extends [Error, ...infer Rest] ? (err: Error, ...args: InferFirstAndPreserveLabel<MultiplyArray<Rest>>) => Result : Params extends [] ? () => Result : (...args: InferFirstAndPreserveLabel<MultiplyArray<Params>>) => Result : T[K];
|
|
187
|
+
};
|
|
188
|
+
/**
|
|
189
|
+
* Utility type to decorate the acknowledgement callbacks with a timeout error.
|
|
190
|
+
*
|
|
191
|
+
* This is needed because the timeout() flag breaks the symmetry between the sender and the receiver:
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* interface Events {
|
|
195
|
+
* "my-event": (val: string) => void;
|
|
196
|
+
* }
|
|
197
|
+
*
|
|
198
|
+
* socket.on("my-event", (cb) => {
|
|
199
|
+
* cb("123"); // one single argument here
|
|
200
|
+
* });
|
|
201
|
+
*
|
|
202
|
+
* socket.timeout(1000).emit("my-event", (err, val) => {
|
|
203
|
+
* // two arguments there (the "err" argument is not properly typed)
|
|
204
|
+
* });
|
|
205
|
+
*
|
|
206
|
+
*/
|
|
207
|
+
declare type DecorateAcknowledgements<E> = {
|
|
208
|
+
[K in keyof E]: E[K] extends (...args: infer Params) => infer Result ? (...args: PrependTimeoutError<Params>) => Result : E[K];
|
|
209
|
+
};
|
|
210
|
+
declare type DecorateAcknowledgementsWithTimeoutAndMultipleResponses<E> = {
|
|
211
|
+
[K in keyof E]: E[K] extends (...args: infer Params) => infer Result ? (...args: ExpectMultipleResponses<PrependTimeoutError<Params>>) => Result : E[K];
|
|
212
|
+
};
|
|
213
|
+
declare type DecorateAcknowledgementsWithMultipleResponses<E> = {
|
|
214
|
+
[K in keyof E]: E[K] extends (...args: infer Params) => infer Result ? (...args: ExpectMultipleResponses<Params>) => Result : E[K];
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
declare type Transport$1 = "polling" | "websocket";
|
|
218
|
+
interface AttachOptions {
|
|
219
|
+
/**
|
|
220
|
+
* name of the path to capture
|
|
221
|
+
* @default "/engine.io"
|
|
222
|
+
*/
|
|
223
|
+
path?: string;
|
|
224
|
+
/**
|
|
225
|
+
* destroy unhandled upgrade requests
|
|
226
|
+
* @default true
|
|
227
|
+
*/
|
|
228
|
+
destroyUpgrade?: boolean;
|
|
229
|
+
/**
|
|
230
|
+
* milliseconds after which unhandled requests are ended
|
|
231
|
+
* @default 1000
|
|
232
|
+
*/
|
|
233
|
+
destroyUpgradeTimeout?: number;
|
|
234
|
+
/**
|
|
235
|
+
* Whether we should add a trailing slash to the request path.
|
|
236
|
+
* @default true
|
|
237
|
+
*/
|
|
238
|
+
addTrailingSlash?: boolean;
|
|
239
|
+
}
|
|
240
|
+
interface ServerOptions$2 {
|
|
241
|
+
/**
|
|
242
|
+
* how many ms without a pong packet to consider the connection closed
|
|
243
|
+
* @default 20000
|
|
244
|
+
*/
|
|
245
|
+
pingTimeout?: number;
|
|
246
|
+
/**
|
|
247
|
+
* how many ms before sending a new ping packet
|
|
248
|
+
* @default 25000
|
|
249
|
+
*/
|
|
250
|
+
pingInterval?: number;
|
|
251
|
+
/**
|
|
252
|
+
* how many ms before an uncompleted transport upgrade is cancelled
|
|
253
|
+
* @default 10000
|
|
254
|
+
*/
|
|
255
|
+
upgradeTimeout?: number;
|
|
256
|
+
/**
|
|
257
|
+
* how many bytes or characters a message can be, before closing the session (to avoid DoS).
|
|
258
|
+
* @default 1e5 (100 KB)
|
|
259
|
+
*/
|
|
260
|
+
maxHttpBufferSize?: number;
|
|
261
|
+
/**
|
|
262
|
+
* A function that receives a given handshake or upgrade request as its first parameter,
|
|
263
|
+
* and can decide whether to continue or not. The second argument is a function that needs
|
|
264
|
+
* to be called with the decided information: fn(err, success), where success is a boolean
|
|
265
|
+
* value where false means that the request is rejected, and err is an error code.
|
|
266
|
+
*/
|
|
267
|
+
allowRequest?: (req: IncomingMessage, fn: (err: string | null | undefined, success: boolean) => void) => void;
|
|
268
|
+
/**
|
|
269
|
+
* The low-level transports that are enabled. WebTransport is disabled by default and must be manually enabled:
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* new Server({
|
|
273
|
+
* transports: ["polling", "websocket", "webtransport"]
|
|
274
|
+
* });
|
|
275
|
+
*
|
|
276
|
+
* @default ["polling", "websocket"]
|
|
277
|
+
*/
|
|
278
|
+
transports?: Transport$1[];
|
|
279
|
+
/**
|
|
280
|
+
* whether to allow transport upgrades
|
|
281
|
+
* @default true
|
|
282
|
+
*/
|
|
283
|
+
allowUpgrades?: boolean;
|
|
284
|
+
/**
|
|
285
|
+
* parameters of the WebSocket permessage-deflate extension (see ws module api docs). Set to false to disable.
|
|
286
|
+
* @default false
|
|
287
|
+
*/
|
|
288
|
+
perMessageDeflate?: boolean | object;
|
|
289
|
+
/**
|
|
290
|
+
* parameters of the http compression for the polling transports (see zlib api docs). Set to false to disable.
|
|
291
|
+
* @default true
|
|
292
|
+
*/
|
|
293
|
+
httpCompression?: boolean | object;
|
|
294
|
+
/**
|
|
295
|
+
* what WebSocket server implementation to use. Specified module must
|
|
296
|
+
* conform to the ws interface (see ws module api docs).
|
|
297
|
+
* An alternative c++ addon is also available by installing eiows module.
|
|
298
|
+
*
|
|
299
|
+
* @default `require("ws").Server`
|
|
300
|
+
*/
|
|
301
|
+
wsEngine?: any;
|
|
302
|
+
/**
|
|
303
|
+
* an optional packet which will be concatenated to the handshake packet emitted by Engine.IO.
|
|
304
|
+
*/
|
|
305
|
+
initialPacket?: any;
|
|
306
|
+
/**
|
|
307
|
+
* configuration of the cookie that contains the client sid to send as part of handshake response headers. This cookie
|
|
308
|
+
* might be used for sticky-session. Defaults to not sending any cookie.
|
|
309
|
+
* @default false
|
|
310
|
+
*/
|
|
311
|
+
cookie?: (CookieSerializeOptions & {
|
|
312
|
+
name: string;
|
|
313
|
+
}) | boolean;
|
|
314
|
+
/**
|
|
315
|
+
* the options that will be forwarded to the cors module
|
|
316
|
+
*/
|
|
317
|
+
cors?: CorsOptions | CorsOptionsDelegate;
|
|
318
|
+
/**
|
|
319
|
+
* whether to enable compatibility with Socket.IO v2 clients
|
|
320
|
+
* @default false
|
|
321
|
+
*/
|
|
322
|
+
allowEIO3?: boolean;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* An Express-compatible middleware.
|
|
326
|
+
*
|
|
327
|
+
* Middleware functions are functions that have access to the request object (req), the response object (res), and the
|
|
328
|
+
* next middleware function in the application’s request-response cycle.
|
|
329
|
+
*
|
|
330
|
+
* @see https://expressjs.com/en/guide/using-middleware.html
|
|
331
|
+
*/
|
|
332
|
+
declare type Middleware = (req: IncomingMessage, res: ServerResponse, next: (err?: any) => void) => void;
|
|
333
|
+
declare abstract class BaseServer extends EventEmitter {
|
|
334
|
+
opts: ServerOptions$2;
|
|
335
|
+
protected clients: any;
|
|
336
|
+
clientsCount: number;
|
|
337
|
+
protected middlewares: Middleware[];
|
|
338
|
+
/**
|
|
339
|
+
* Server constructor.
|
|
340
|
+
*
|
|
341
|
+
* @param {Object} opts - options
|
|
342
|
+
* @api public
|
|
343
|
+
*/
|
|
344
|
+
constructor(opts?: ServerOptions$2);
|
|
345
|
+
protected abstract init(): any;
|
|
346
|
+
/**
|
|
347
|
+
* Compute the pathname of the requests that are handled by the server
|
|
348
|
+
* @param options
|
|
349
|
+
* @protected
|
|
350
|
+
*/
|
|
351
|
+
protected _computePath(options: AttachOptions): string;
|
|
352
|
+
/**
|
|
353
|
+
* Returns a list of available transports for upgrade given a certain transport.
|
|
354
|
+
*
|
|
355
|
+
* @return {Array}
|
|
356
|
+
* @api public
|
|
357
|
+
*/
|
|
358
|
+
upgrades(transport: any): any;
|
|
359
|
+
/**
|
|
360
|
+
* Verifies a request.
|
|
361
|
+
*
|
|
362
|
+
* @param {http.IncomingMessage}
|
|
363
|
+
* @return {Boolean} whether the request is valid
|
|
364
|
+
* @api private
|
|
365
|
+
*/
|
|
366
|
+
protected verify(req: any, upgrade: any, fn: any): any;
|
|
367
|
+
/**
|
|
368
|
+
* Adds a new middleware.
|
|
369
|
+
*
|
|
370
|
+
* @example
|
|
371
|
+
* import helmet from "helmet";
|
|
372
|
+
*
|
|
373
|
+
* engine.use(helmet());
|
|
374
|
+
*
|
|
375
|
+
* @param fn
|
|
376
|
+
*/
|
|
377
|
+
use(fn: any): void;
|
|
378
|
+
/**
|
|
379
|
+
* Apply the middlewares to the request.
|
|
380
|
+
*
|
|
381
|
+
* @param req
|
|
382
|
+
* @param res
|
|
383
|
+
* @param callback
|
|
384
|
+
* @protected
|
|
385
|
+
*/
|
|
386
|
+
protected _applyMiddlewares(req: IncomingMessage, res: ServerResponse, callback: (err?: any) => void): void;
|
|
387
|
+
/**
|
|
388
|
+
* Closes all clients.
|
|
389
|
+
*
|
|
390
|
+
* @api public
|
|
391
|
+
*/
|
|
392
|
+
close(): this;
|
|
393
|
+
protected abstract cleanup(): any;
|
|
394
|
+
/**
|
|
395
|
+
* generate a socket id.
|
|
396
|
+
* Overwrite this method to generate your custom socket id
|
|
397
|
+
*
|
|
398
|
+
* @param {Object} request object
|
|
399
|
+
* @api public
|
|
400
|
+
*/
|
|
401
|
+
generateId(req: any): any;
|
|
402
|
+
/**
|
|
403
|
+
* Handshakes a new client.
|
|
404
|
+
*
|
|
405
|
+
* @param {String} transport name
|
|
406
|
+
* @param {Object} request object
|
|
407
|
+
* @param {Function} closeConnection
|
|
408
|
+
*
|
|
409
|
+
* @api protected
|
|
410
|
+
*/
|
|
411
|
+
protected handshake(transportName: any, req: any, closeConnection: any): Promise<any>;
|
|
412
|
+
onWebTransportSession(session: any): Promise<any>;
|
|
413
|
+
protected abstract createTransport(transportName: any, req: any): any;
|
|
414
|
+
/**
|
|
415
|
+
* Protocol errors mappings.
|
|
416
|
+
*/
|
|
417
|
+
static errors: {
|
|
418
|
+
UNKNOWN_TRANSPORT: number;
|
|
419
|
+
UNKNOWN_SID: number;
|
|
420
|
+
BAD_HANDSHAKE_METHOD: number;
|
|
421
|
+
BAD_REQUEST: number;
|
|
422
|
+
FORBIDDEN: number;
|
|
423
|
+
UNSUPPORTED_PROTOCOL_VERSION: number;
|
|
424
|
+
};
|
|
425
|
+
static errorMessages: {
|
|
426
|
+
0: string;
|
|
427
|
+
1: string;
|
|
428
|
+
2: string;
|
|
429
|
+
3: string;
|
|
430
|
+
4: string;
|
|
431
|
+
5: string;
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
declare type PacketType$1 = "open" | "close" | "ping" | "pong" | "message" | "upgrade" | "noop" | "error";
|
|
436
|
+
declare type RawData = any;
|
|
437
|
+
interface Packet$1 {
|
|
438
|
+
type: PacketType$1;
|
|
439
|
+
options?: {
|
|
440
|
+
compress: boolean;
|
|
441
|
+
};
|
|
442
|
+
data?: RawData;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
declare type ReadyState$1 = "open" | "closing" | "closed";
|
|
446
|
+
declare abstract class Transport extends EventEmitter {
|
|
447
|
+
sid: string;
|
|
448
|
+
writable: boolean;
|
|
449
|
+
protocol: number;
|
|
450
|
+
protected _readyState: ReadyState$1;
|
|
451
|
+
protected discarded: boolean;
|
|
452
|
+
protected parser: any;
|
|
453
|
+
protected req: IncomingMessage & {
|
|
454
|
+
cleanup: Function;
|
|
455
|
+
};
|
|
456
|
+
protected supportsBinary: boolean;
|
|
457
|
+
get readyState(): ReadyState$1;
|
|
458
|
+
set readyState(state: ReadyState$1);
|
|
459
|
+
/**
|
|
460
|
+
* Transport constructor.
|
|
461
|
+
*
|
|
462
|
+
* @param {http.IncomingMessage} req
|
|
463
|
+
* @api public
|
|
464
|
+
*/
|
|
465
|
+
constructor(req: any);
|
|
466
|
+
/**
|
|
467
|
+
* Flags the transport as discarded.
|
|
468
|
+
*
|
|
469
|
+
* @api private
|
|
470
|
+
*/
|
|
471
|
+
discard(): void;
|
|
472
|
+
/**
|
|
473
|
+
* Called with an incoming HTTP request.
|
|
474
|
+
*
|
|
475
|
+
* @param {http.IncomingMessage} req
|
|
476
|
+
* @api protected
|
|
477
|
+
*/
|
|
478
|
+
protected onRequest(req: any): void;
|
|
479
|
+
/**
|
|
480
|
+
* Closes the transport.
|
|
481
|
+
*
|
|
482
|
+
* @api private
|
|
483
|
+
*/
|
|
484
|
+
close(fn?: any): void;
|
|
485
|
+
/**
|
|
486
|
+
* Called with a transport error.
|
|
487
|
+
*
|
|
488
|
+
* @param {String} msg - message error
|
|
489
|
+
* @param {Object} desc - error description
|
|
490
|
+
* @api protected
|
|
491
|
+
*/
|
|
492
|
+
protected onError(msg: string, desc?: any): void;
|
|
493
|
+
/**
|
|
494
|
+
* Called with parsed out a packets from the data stream.
|
|
495
|
+
*
|
|
496
|
+
* @param {Object} packet
|
|
497
|
+
* @api protected
|
|
498
|
+
*/
|
|
499
|
+
protected onPacket(packet: Packet$1): void;
|
|
500
|
+
/**
|
|
501
|
+
* Called with the encoded packet data.
|
|
502
|
+
*
|
|
503
|
+
* @param {String} data
|
|
504
|
+
* @api protected
|
|
505
|
+
*/
|
|
506
|
+
protected onData(data: any): void;
|
|
507
|
+
/**
|
|
508
|
+
* Called upon transport close.
|
|
509
|
+
*
|
|
510
|
+
* @api protected
|
|
511
|
+
*/
|
|
512
|
+
protected onClose(): void;
|
|
513
|
+
/**
|
|
514
|
+
* Advertise framing support.
|
|
515
|
+
*/
|
|
516
|
+
abstract get supportsFraming(): any;
|
|
517
|
+
/**
|
|
518
|
+
* The name of the transport.
|
|
519
|
+
*/
|
|
520
|
+
abstract get name(): any;
|
|
521
|
+
/**
|
|
522
|
+
* Sends an array of packets.
|
|
523
|
+
*
|
|
524
|
+
* @param {Array} packets
|
|
525
|
+
* @package
|
|
526
|
+
*/
|
|
527
|
+
abstract send(packets: any): any;
|
|
528
|
+
/**
|
|
529
|
+
* Closes the transport.
|
|
530
|
+
*/
|
|
531
|
+
abstract doClose(fn?: any): any;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
interface SendOptions {
|
|
535
|
+
compress?: boolean;
|
|
536
|
+
}
|
|
537
|
+
declare type ReadyState = "opening" | "open" | "closing" | "closed";
|
|
538
|
+
declare class Socket$1 extends EventEmitter {
|
|
539
|
+
readonly protocol: number;
|
|
540
|
+
readonly request: IncomingMessage;
|
|
541
|
+
readonly remoteAddress: string;
|
|
542
|
+
_readyState: ReadyState;
|
|
543
|
+
transport: Transport;
|
|
544
|
+
private server;
|
|
545
|
+
private upgrading;
|
|
546
|
+
private upgraded;
|
|
547
|
+
private writeBuffer;
|
|
548
|
+
private packetsFn;
|
|
549
|
+
private sentCallbackFn;
|
|
550
|
+
private cleanupFn;
|
|
551
|
+
private pingTimeoutTimer;
|
|
552
|
+
private pingIntervalTimer;
|
|
553
|
+
/**
|
|
554
|
+
* This is the session identifier that the client will use in the subsequent HTTP requests. It must not be shared with
|
|
555
|
+
* others parties, as it might lead to session hijacking.
|
|
556
|
+
*
|
|
557
|
+
* @private
|
|
558
|
+
*/
|
|
559
|
+
private readonly id;
|
|
560
|
+
get readyState(): ReadyState;
|
|
561
|
+
set readyState(state: ReadyState);
|
|
562
|
+
/**
|
|
563
|
+
* Client class (abstract).
|
|
564
|
+
*
|
|
565
|
+
* @api private
|
|
566
|
+
*/
|
|
567
|
+
constructor(id: any, server: any, transport: any, req: any, protocol: any);
|
|
568
|
+
/**
|
|
569
|
+
* Called upon transport considered open.
|
|
570
|
+
*
|
|
571
|
+
* @api private
|
|
572
|
+
*/
|
|
573
|
+
private onOpen;
|
|
574
|
+
/**
|
|
575
|
+
* Called upon transport packet.
|
|
576
|
+
*
|
|
577
|
+
* @param {Object} packet
|
|
578
|
+
* @api private
|
|
579
|
+
*/
|
|
580
|
+
private onPacket;
|
|
581
|
+
/**
|
|
582
|
+
* Called upon transport error.
|
|
583
|
+
*
|
|
584
|
+
* @param {Error} err - error object
|
|
585
|
+
* @api private
|
|
586
|
+
*/
|
|
587
|
+
private onError;
|
|
588
|
+
/**
|
|
589
|
+
* Pings client every `this.pingInterval` and expects response
|
|
590
|
+
* within `this.pingTimeout` or closes connection.
|
|
591
|
+
*
|
|
592
|
+
* @api private
|
|
593
|
+
*/
|
|
594
|
+
private schedulePing;
|
|
595
|
+
/**
|
|
596
|
+
* Resets ping timeout.
|
|
597
|
+
*
|
|
598
|
+
* @api private
|
|
599
|
+
*/
|
|
600
|
+
private resetPingTimeout;
|
|
601
|
+
/**
|
|
602
|
+
* Attaches handlers for the given transport.
|
|
603
|
+
*
|
|
604
|
+
* @param {Transport} transport
|
|
605
|
+
* @api private
|
|
606
|
+
*/
|
|
607
|
+
private setTransport;
|
|
608
|
+
/**
|
|
609
|
+
* Upgrades socket to the given transport
|
|
610
|
+
*
|
|
611
|
+
* @param {Transport} transport
|
|
612
|
+
* @api private
|
|
613
|
+
*/
|
|
614
|
+
private maybeUpgrade;
|
|
615
|
+
/**
|
|
616
|
+
* Clears listeners and timers associated with current transport.
|
|
617
|
+
*
|
|
618
|
+
* @api private
|
|
619
|
+
*/
|
|
620
|
+
private clearTransport;
|
|
621
|
+
/**
|
|
622
|
+
* Called upon transport considered closed.
|
|
623
|
+
* Possible reasons: `ping timeout`, `client error`, `parse error`,
|
|
624
|
+
* `transport error`, `server close`, `transport close`
|
|
625
|
+
*/
|
|
626
|
+
private onClose;
|
|
627
|
+
/**
|
|
628
|
+
* Setup and manage send callback
|
|
629
|
+
*
|
|
630
|
+
* @api private
|
|
631
|
+
*/
|
|
632
|
+
private setupSendCallback;
|
|
633
|
+
/**
|
|
634
|
+
* Sends a message packet.
|
|
635
|
+
*
|
|
636
|
+
* @param {Object} data
|
|
637
|
+
* @param {Object} options
|
|
638
|
+
* @param {Function} callback
|
|
639
|
+
* @return {Socket} for chaining
|
|
640
|
+
* @api public
|
|
641
|
+
*/
|
|
642
|
+
send(data: RawData, options?: SendOptions, callback?: () => void): this;
|
|
643
|
+
/**
|
|
644
|
+
* Alias of {@link send}.
|
|
645
|
+
*
|
|
646
|
+
* @param data
|
|
647
|
+
* @param options
|
|
648
|
+
* @param callback
|
|
649
|
+
*/
|
|
650
|
+
write(data: RawData, options?: SendOptions, callback?: () => void): this;
|
|
651
|
+
/**
|
|
652
|
+
* Sends a packet.
|
|
653
|
+
*
|
|
654
|
+
* @param {String} type - packet type
|
|
655
|
+
* @param {String} data
|
|
656
|
+
* @param {Object} options
|
|
657
|
+
* @param {Function} callback
|
|
658
|
+
*
|
|
659
|
+
* @api private
|
|
660
|
+
*/
|
|
661
|
+
private sendPacket;
|
|
662
|
+
/**
|
|
663
|
+
* Attempts to flush the packets buffer.
|
|
664
|
+
*
|
|
665
|
+
* @api private
|
|
666
|
+
*/
|
|
667
|
+
private flush;
|
|
668
|
+
/**
|
|
669
|
+
* Get available upgrades for this socket.
|
|
670
|
+
*
|
|
671
|
+
* @api private
|
|
672
|
+
*/
|
|
673
|
+
private getAvailableUpgrades;
|
|
674
|
+
/**
|
|
675
|
+
* Closes the socket and underlying transport.
|
|
676
|
+
*
|
|
677
|
+
* @param {Boolean} discard - optional, discard the transport
|
|
678
|
+
* @return {Socket} for chaining
|
|
679
|
+
* @api public
|
|
680
|
+
*/
|
|
681
|
+
close(discard?: boolean): void;
|
|
682
|
+
/**
|
|
683
|
+
* Closes the underlying transport.
|
|
684
|
+
*
|
|
685
|
+
* @param {Boolean} discard
|
|
686
|
+
* @api private
|
|
687
|
+
*/
|
|
688
|
+
private closeTransport;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
/**
|
|
692
|
+
* A public ID, sent by the server at the beginning of the Socket.IO session and which can be used for private messaging
|
|
693
|
+
*/
|
|
694
|
+
type SocketId = string;
|
|
695
|
+
/**
|
|
696
|
+
* A private ID, sent by the server at the beginning of the Socket.IO session and used for connection state recovery
|
|
697
|
+
* upon reconnection
|
|
698
|
+
*/
|
|
699
|
+
type PrivateSessionId = string;
|
|
700
|
+
type Room = string;
|
|
701
|
+
interface BroadcastFlags {
|
|
702
|
+
volatile?: boolean;
|
|
703
|
+
compress?: boolean;
|
|
704
|
+
local?: boolean;
|
|
705
|
+
broadcast?: boolean;
|
|
706
|
+
binary?: boolean;
|
|
707
|
+
timeout?: number;
|
|
708
|
+
}
|
|
709
|
+
interface BroadcastOptions {
|
|
710
|
+
rooms: Set<Room>;
|
|
711
|
+
except?: Set<Room>;
|
|
712
|
+
flags?: BroadcastFlags;
|
|
713
|
+
}
|
|
714
|
+
interface SessionToPersist {
|
|
715
|
+
sid: SocketId;
|
|
716
|
+
pid: PrivateSessionId;
|
|
717
|
+
rooms: Room[];
|
|
718
|
+
data: unknown;
|
|
719
|
+
}
|
|
720
|
+
type Session = SessionToPersist & {
|
|
721
|
+
missedPackets: unknown[][];
|
|
722
|
+
};
|
|
723
|
+
declare class Adapter extends EventEmitter {
|
|
724
|
+
readonly nsp: any;
|
|
725
|
+
rooms: Map<Room, Set<SocketId>>;
|
|
726
|
+
sids: Map<SocketId, Set<Room>>;
|
|
727
|
+
private readonly encoder;
|
|
728
|
+
/**
|
|
729
|
+
* In-memory adapter constructor.
|
|
730
|
+
*
|
|
731
|
+
* @param {Namespace} nsp
|
|
732
|
+
*/
|
|
733
|
+
constructor(nsp: any);
|
|
734
|
+
/**
|
|
735
|
+
* To be overridden
|
|
736
|
+
*/
|
|
737
|
+
init(): Promise<void> | void;
|
|
738
|
+
/**
|
|
739
|
+
* To be overridden
|
|
740
|
+
*/
|
|
741
|
+
close(): Promise<void> | void;
|
|
742
|
+
/**
|
|
743
|
+
* Returns the number of Socket.IO servers in the cluster
|
|
744
|
+
*
|
|
745
|
+
* @public
|
|
746
|
+
*/
|
|
747
|
+
serverCount(): Promise<number>;
|
|
748
|
+
/**
|
|
749
|
+
* Adds a socket to a list of room.
|
|
750
|
+
*
|
|
751
|
+
* @param {SocketId} id the socket id
|
|
752
|
+
* @param {Set<Room>} rooms a set of rooms
|
|
753
|
+
* @public
|
|
754
|
+
*/
|
|
755
|
+
addAll(id: SocketId, rooms: Set<Room>): Promise<void> | void;
|
|
756
|
+
/**
|
|
757
|
+
* Removes a socket from a room.
|
|
758
|
+
*
|
|
759
|
+
* @param {SocketId} id the socket id
|
|
760
|
+
* @param {Room} room the room name
|
|
761
|
+
*/
|
|
762
|
+
del(id: SocketId, room: Room): Promise<void> | void;
|
|
763
|
+
private _del;
|
|
764
|
+
/**
|
|
765
|
+
* Removes a socket from all rooms it's joined.
|
|
766
|
+
*
|
|
767
|
+
* @param {SocketId} id the socket id
|
|
768
|
+
*/
|
|
769
|
+
delAll(id: SocketId): void;
|
|
770
|
+
/**
|
|
771
|
+
* Broadcasts a packet.
|
|
772
|
+
*
|
|
773
|
+
* Options:
|
|
774
|
+
* - `flags` {Object} flags for this packet
|
|
775
|
+
* - `except` {Array} sids that should be excluded
|
|
776
|
+
* - `rooms` {Array} list of rooms to broadcast to
|
|
777
|
+
*
|
|
778
|
+
* @param {Object} packet the packet object
|
|
779
|
+
* @param {Object} opts the options
|
|
780
|
+
* @public
|
|
781
|
+
*/
|
|
782
|
+
broadcast(packet: any, opts: BroadcastOptions): void;
|
|
783
|
+
/**
|
|
784
|
+
* Broadcasts a packet and expects multiple acknowledgements.
|
|
785
|
+
*
|
|
786
|
+
* Options:
|
|
787
|
+
* - `flags` {Object} flags for this packet
|
|
788
|
+
* - `except` {Array} sids that should be excluded
|
|
789
|
+
* - `rooms` {Array} list of rooms to broadcast to
|
|
790
|
+
*
|
|
791
|
+
* @param {Object} packet the packet object
|
|
792
|
+
* @param {Object} opts the options
|
|
793
|
+
* @param clientCountCallback - the number of clients that received the packet
|
|
794
|
+
* @param ack - the callback that will be called for each client response
|
|
795
|
+
*
|
|
796
|
+
* @public
|
|
797
|
+
*/
|
|
798
|
+
broadcastWithAck(packet: any, opts: BroadcastOptions, clientCountCallback: (clientCount: number) => void, ack: (...args: any[]) => void): void;
|
|
799
|
+
private _encode;
|
|
800
|
+
/**
|
|
801
|
+
* Gets a list of sockets by sid.
|
|
802
|
+
*
|
|
803
|
+
* @param {Set<Room>} rooms the explicit set of rooms to check.
|
|
804
|
+
*/
|
|
805
|
+
sockets(rooms: Set<Room>): Promise<Set<SocketId>>;
|
|
806
|
+
/**
|
|
807
|
+
* Gets the list of rooms a given socket has joined.
|
|
808
|
+
*
|
|
809
|
+
* @param {SocketId} id the socket id
|
|
810
|
+
*/
|
|
811
|
+
socketRooms(id: SocketId): Set<Room> | undefined;
|
|
812
|
+
/**
|
|
813
|
+
* Returns the matching socket instances
|
|
814
|
+
*
|
|
815
|
+
* @param opts - the filters to apply
|
|
816
|
+
*/
|
|
817
|
+
fetchSockets(opts: BroadcastOptions): Promise<any[]>;
|
|
818
|
+
/**
|
|
819
|
+
* Makes the matching socket instances join the specified rooms
|
|
820
|
+
*
|
|
821
|
+
* @param opts - the filters to apply
|
|
822
|
+
* @param rooms - the rooms to join
|
|
823
|
+
*/
|
|
824
|
+
addSockets(opts: BroadcastOptions, rooms: Room[]): void;
|
|
825
|
+
/**
|
|
826
|
+
* Makes the matching socket instances leave the specified rooms
|
|
827
|
+
*
|
|
828
|
+
* @param opts - the filters to apply
|
|
829
|
+
* @param rooms - the rooms to leave
|
|
830
|
+
*/
|
|
831
|
+
delSockets(opts: BroadcastOptions, rooms: Room[]): void;
|
|
832
|
+
/**
|
|
833
|
+
* Makes the matching socket instances disconnect
|
|
834
|
+
*
|
|
835
|
+
* @param opts - the filters to apply
|
|
836
|
+
* @param close - whether to close the underlying connection
|
|
837
|
+
*/
|
|
838
|
+
disconnectSockets(opts: BroadcastOptions, close: boolean): void;
|
|
839
|
+
private apply;
|
|
840
|
+
private computeExceptSids;
|
|
841
|
+
/**
|
|
842
|
+
* Send a packet to the other Socket.IO servers in the cluster
|
|
843
|
+
* @param packet - an array of arguments, which may include an acknowledgement callback at the end
|
|
844
|
+
*/
|
|
845
|
+
serverSideEmit(packet: any[]): void;
|
|
846
|
+
/**
|
|
847
|
+
* Save the client session in order to restore it upon reconnection.
|
|
848
|
+
*/
|
|
849
|
+
persistSession(session: SessionToPersist): void;
|
|
850
|
+
/**
|
|
851
|
+
* Restore the session and find the packets that were missed by the client.
|
|
852
|
+
* @param pid
|
|
853
|
+
* @param offset
|
|
854
|
+
*/
|
|
855
|
+
restoreSession(pid: PrivateSessionId, offset: string): Promise<Session>;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/**
|
|
859
|
+
* An events map is an interface that maps event names to their value, which
|
|
860
|
+
* represents the type of the `on` listener.
|
|
861
|
+
*/
|
|
862
|
+
interface EventsMap$1 {
|
|
863
|
+
[event: string]: any;
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
/**
|
|
867
|
+
* Returns a union type containing all the keys of an event map.
|
|
868
|
+
*/
|
|
869
|
+
type EventNames<Map extends EventsMap$1> = keyof Map & (string | symbol);
|
|
870
|
+
|
|
871
|
+
/** The tuple type representing the parameters of an event listener */
|
|
872
|
+
type EventParams<
|
|
873
|
+
Map extends EventsMap$1,
|
|
874
|
+
Ev extends EventNames<Map>
|
|
875
|
+
> = Parameters<Map[Ev]>;
|
|
876
|
+
|
|
877
|
+
/**
|
|
878
|
+
* The event names that are either in ReservedEvents or in UserEvents
|
|
879
|
+
*/
|
|
880
|
+
type ReservedOrUserEventNames<
|
|
881
|
+
ReservedEventsMap extends EventsMap$1,
|
|
882
|
+
UserEvents extends EventsMap$1
|
|
883
|
+
> = EventNames<ReservedEventsMap> | EventNames<UserEvents>;
|
|
884
|
+
|
|
885
|
+
/**
|
|
886
|
+
* Type of a listener of a user event or a reserved event. If `Ev` is in
|
|
887
|
+
* `ReservedEvents`, the reserved event listener is returned.
|
|
888
|
+
*/
|
|
889
|
+
type ReservedOrUserListener<
|
|
890
|
+
ReservedEvents extends EventsMap$1,
|
|
891
|
+
UserEvents extends EventsMap$1,
|
|
892
|
+
Ev extends ReservedOrUserEventNames<ReservedEvents, UserEvents>
|
|
893
|
+
> = FallbackToUntypedListener<
|
|
894
|
+
Ev extends EventNames<ReservedEvents>
|
|
895
|
+
? ReservedEvents[Ev]
|
|
896
|
+
: Ev extends EventNames<UserEvents>
|
|
897
|
+
? UserEvents[Ev]
|
|
898
|
+
: never
|
|
899
|
+
>;
|
|
900
|
+
|
|
901
|
+
/**
|
|
902
|
+
* Returns an untyped listener type if `T` is `never`; otherwise, returns `T`.
|
|
903
|
+
*
|
|
904
|
+
* This is a hack to mitigate https://github.com/socketio/socket.io/issues/3833.
|
|
905
|
+
* Needed because of https://github.com/microsoft/TypeScript/issues/41778
|
|
906
|
+
*/
|
|
907
|
+
type FallbackToUntypedListener<T> = [T] extends [never]
|
|
908
|
+
? (...args: any[]) => void | Promise<void>
|
|
909
|
+
: T;
|
|
910
|
+
|
|
911
|
+
/**
|
|
912
|
+
* Strictly typed version of an `EventEmitter`. A `TypedEventEmitter` takes type
|
|
913
|
+
* parameters for mappings of event names to event data types, and strictly
|
|
914
|
+
* types method calls to the `EventEmitter` according to these event maps.
|
|
915
|
+
*
|
|
916
|
+
* @typeParam ListenEvents - `EventsMap` of user-defined events that can be
|
|
917
|
+
* listened to with `on` or `once`
|
|
918
|
+
* @typeParam EmitEvents - `EventsMap` of user-defined events that can be
|
|
919
|
+
* emitted with `emit`
|
|
920
|
+
* @typeParam ReservedEvents - `EventsMap` of reserved events, that can be
|
|
921
|
+
* emitted by socket.io with `emitReserved`, and can be listened to with
|
|
922
|
+
* `listen`.
|
|
923
|
+
*/
|
|
924
|
+
declare class Emitter<
|
|
925
|
+
ListenEvents extends EventsMap$1,
|
|
926
|
+
EmitEvents extends EventsMap$1,
|
|
927
|
+
ReservedEvents extends EventsMap$1 = {}
|
|
928
|
+
> {
|
|
929
|
+
/**
|
|
930
|
+
* Adds the `listener` function as an event listener for `ev`.
|
|
931
|
+
*
|
|
932
|
+
* @param ev Name of the event
|
|
933
|
+
* @param listener Callback function
|
|
934
|
+
*/
|
|
935
|
+
on<Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>>(
|
|
936
|
+
ev: Ev,
|
|
937
|
+
listener: ReservedOrUserListener<ReservedEvents, ListenEvents, Ev>
|
|
938
|
+
): this;
|
|
939
|
+
|
|
940
|
+
/**
|
|
941
|
+
* Adds a one-time `listener` function as an event listener for `ev`.
|
|
942
|
+
*
|
|
943
|
+
* @param ev Name of the event
|
|
944
|
+
* @param listener Callback function
|
|
945
|
+
*/
|
|
946
|
+
once<Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>>(
|
|
947
|
+
ev: Ev,
|
|
948
|
+
listener: ReservedOrUserListener<ReservedEvents, ListenEvents, Ev>
|
|
949
|
+
): this;
|
|
950
|
+
|
|
951
|
+
/**
|
|
952
|
+
* Removes the `listener` function as an event listener for `ev`.
|
|
953
|
+
*
|
|
954
|
+
* @param ev Name of the event
|
|
955
|
+
* @param listener Callback function
|
|
956
|
+
*/
|
|
957
|
+
off<Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>>(
|
|
958
|
+
ev?: Ev,
|
|
959
|
+
listener?: ReservedOrUserListener<ReservedEvents, ListenEvents, Ev>
|
|
960
|
+
): this;
|
|
961
|
+
|
|
962
|
+
/**
|
|
963
|
+
* Emits an event.
|
|
964
|
+
*
|
|
965
|
+
* @param ev Name of the event
|
|
966
|
+
* @param args Values to send to listeners of this event
|
|
967
|
+
*/
|
|
968
|
+
emit<Ev extends EventNames<EmitEvents>>(
|
|
969
|
+
ev: Ev,
|
|
970
|
+
...args: EventParams<EmitEvents, Ev>
|
|
971
|
+
): this;
|
|
972
|
+
|
|
973
|
+
/**
|
|
974
|
+
* Emits a reserved event.
|
|
975
|
+
*
|
|
976
|
+
* This method is `protected`, so that only a class extending
|
|
977
|
+
* `StrictEventEmitter` can emit its own reserved events.
|
|
978
|
+
*
|
|
979
|
+
* @param ev Reserved event name
|
|
980
|
+
* @param args Arguments to emit along with the event
|
|
981
|
+
*/
|
|
982
|
+
protected emitReserved<Ev extends EventNames<ReservedEvents>>(
|
|
983
|
+
ev: Ev,
|
|
984
|
+
...args: EventParams<ReservedEvents, Ev>
|
|
985
|
+
): this;
|
|
986
|
+
|
|
987
|
+
/**
|
|
988
|
+
* Returns the listeners listening to an event.
|
|
989
|
+
*
|
|
990
|
+
* @param event Event name
|
|
991
|
+
* @returns Array of listeners subscribed to `event`
|
|
992
|
+
*/
|
|
993
|
+
listeners<Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>>(
|
|
994
|
+
event: Ev
|
|
995
|
+
): ReservedOrUserListener<ReservedEvents, ListenEvents, Ev>[];
|
|
996
|
+
|
|
997
|
+
/**
|
|
998
|
+
* Returns true if there is a listener for this event.
|
|
999
|
+
*
|
|
1000
|
+
* @param event Event name
|
|
1001
|
+
* @returns boolean
|
|
1002
|
+
*/
|
|
1003
|
+
hasListeners<
|
|
1004
|
+
Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>
|
|
1005
|
+
>(event: Ev): boolean;
|
|
1006
|
+
|
|
1007
|
+
/**
|
|
1008
|
+
* Removes the `listener` function as an event listener for `ev`.
|
|
1009
|
+
*
|
|
1010
|
+
* @param ev Name of the event
|
|
1011
|
+
* @param listener Callback function
|
|
1012
|
+
*/
|
|
1013
|
+
removeListener<
|
|
1014
|
+
Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>
|
|
1015
|
+
>(
|
|
1016
|
+
ev?: Ev,
|
|
1017
|
+
listener?: ReservedOrUserListener<ReservedEvents, ListenEvents, Ev>
|
|
1018
|
+
): this;
|
|
1019
|
+
|
|
1020
|
+
/**
|
|
1021
|
+
* Removes all `listener` function as an event listener for `ev`.
|
|
1022
|
+
*
|
|
1023
|
+
* @param ev Name of the event
|
|
1024
|
+
*/
|
|
1025
|
+
removeAllListeners<
|
|
1026
|
+
Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>
|
|
1027
|
+
>(ev?: Ev): this;
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
/**
|
|
1031
|
+
* Protocol version.
|
|
1032
|
+
*
|
|
1033
|
+
* @public
|
|
1034
|
+
*/
|
|
1035
|
+
declare const protocol: number;
|
|
1036
|
+
declare enum PacketType {
|
|
1037
|
+
CONNECT = 0,
|
|
1038
|
+
DISCONNECT = 1,
|
|
1039
|
+
EVENT = 2,
|
|
1040
|
+
ACK = 3,
|
|
1041
|
+
CONNECT_ERROR = 4,
|
|
1042
|
+
BINARY_EVENT = 5,
|
|
1043
|
+
BINARY_ACK = 6
|
|
1044
|
+
}
|
|
1045
|
+
interface Packet {
|
|
1046
|
+
type: PacketType;
|
|
1047
|
+
nsp: string;
|
|
1048
|
+
data?: any;
|
|
1049
|
+
id?: number;
|
|
1050
|
+
attachments?: number;
|
|
1051
|
+
}
|
|
1052
|
+
/**
|
|
1053
|
+
* A socket.io Encoder instance
|
|
1054
|
+
*/
|
|
1055
|
+
declare class Encoder {
|
|
1056
|
+
private replacer?;
|
|
1057
|
+
/**
|
|
1058
|
+
* Encoder constructor
|
|
1059
|
+
*
|
|
1060
|
+
* @param {function} replacer - custom replacer to pass down to JSON.parse
|
|
1061
|
+
*/
|
|
1062
|
+
constructor(replacer?: (this: any, key: string, value: any) => any);
|
|
1063
|
+
/**
|
|
1064
|
+
* Encode a packet as a single string if non-binary, or as a
|
|
1065
|
+
* buffer sequence, depending on packet type.
|
|
1066
|
+
*
|
|
1067
|
+
* @param {Object} obj - packet object
|
|
1068
|
+
*/
|
|
1069
|
+
encode(obj: Packet): any[];
|
|
1070
|
+
/**
|
|
1071
|
+
* Encode packet as string.
|
|
1072
|
+
*/
|
|
1073
|
+
private encodeAsString;
|
|
1074
|
+
/**
|
|
1075
|
+
* Encode packet as 'buffer sequence' by removing blobs, and
|
|
1076
|
+
* deconstructing packet into object with placeholders and
|
|
1077
|
+
* a list of buffers.
|
|
1078
|
+
*/
|
|
1079
|
+
private encodeAsBinary;
|
|
1080
|
+
}
|
|
1081
|
+
interface DecoderReservedEvents {
|
|
1082
|
+
decoded: (packet: Packet) => void;
|
|
1083
|
+
}
|
|
1084
|
+
/**
|
|
1085
|
+
* A socket.io Decoder instance
|
|
1086
|
+
*
|
|
1087
|
+
* @return {Object} decoder
|
|
1088
|
+
*/
|
|
1089
|
+
declare class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
|
|
1090
|
+
private reviver?;
|
|
1091
|
+
private reconstructor;
|
|
1092
|
+
/**
|
|
1093
|
+
* Decoder constructor
|
|
1094
|
+
*
|
|
1095
|
+
* @param {function} reviver - custom reviver to pass down to JSON.stringify
|
|
1096
|
+
*/
|
|
1097
|
+
constructor(reviver?: (this: any, key: string, value: any) => any);
|
|
1098
|
+
/**
|
|
1099
|
+
* Decodes an encoded packet string into packet JSON.
|
|
1100
|
+
*
|
|
1101
|
+
* @param {String} obj - encoded packet
|
|
1102
|
+
*/
|
|
1103
|
+
add(obj: any): void;
|
|
1104
|
+
/**
|
|
1105
|
+
* Decode a packet String (JSON data)
|
|
1106
|
+
*
|
|
1107
|
+
* @param {String} str
|
|
1108
|
+
* @return {Object} packet
|
|
1109
|
+
*/
|
|
1110
|
+
private decodeString;
|
|
1111
|
+
private tryParse;
|
|
1112
|
+
private static isPayloadValid;
|
|
1113
|
+
/**
|
|
1114
|
+
* Deallocates a parser's resources
|
|
1115
|
+
*/
|
|
1116
|
+
destroy(): void;
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
type parser_Decoder = Decoder;
|
|
1120
|
+
declare const parser_Decoder: typeof Decoder;
|
|
1121
|
+
type parser_Encoder = Encoder;
|
|
1122
|
+
declare const parser_Encoder: typeof Encoder;
|
|
1123
|
+
type parser_Packet = Packet;
|
|
1124
|
+
type parser_PacketType = PacketType;
|
|
1125
|
+
declare const parser_PacketType: typeof PacketType;
|
|
1126
|
+
declare const parser_protocol: typeof protocol;
|
|
1127
|
+
declare namespace parser {
|
|
1128
|
+
export { parser_Decoder as Decoder, parser_Encoder as Encoder, type parser_Packet as Packet, parser_PacketType as PacketType, parser_protocol as protocol };
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
declare type ParentNspNameMatchFn$1 = (name: string, auth: {
|
|
1132
|
+
[key: string]: any;
|
|
1133
|
+
}, fn: (err: Error | null, success: boolean) => void) => void;
|
|
1134
|
+
declare type AdapterConstructor$1 = typeof Adapter | ((nsp: Namespace) => Adapter);
|
|
1135
|
+
declare type TServerInstance$1 = http.Server | Server$2 | Http2SecureServer | Http2Server;
|
|
1136
|
+
interface ServerOptions$1 extends ServerOptions$2, AttachOptions {
|
|
1137
|
+
/**
|
|
1138
|
+
* name of the path to capture
|
|
1139
|
+
* @default "/socket.io"
|
|
1140
|
+
*/
|
|
1141
|
+
path: string;
|
|
1142
|
+
/**
|
|
1143
|
+
* whether to serve the client files
|
|
1144
|
+
* @default true
|
|
1145
|
+
*/
|
|
1146
|
+
serveClient: boolean;
|
|
1147
|
+
/**
|
|
1148
|
+
* the adapter to use
|
|
1149
|
+
* @default the in-memory adapter (https://github.com/socketio/socket.io-adapter)
|
|
1150
|
+
*/
|
|
1151
|
+
adapter: AdapterConstructor$1;
|
|
1152
|
+
/**
|
|
1153
|
+
* the parser to use
|
|
1154
|
+
* @default the default parser (https://github.com/socketio/socket.io-parser)
|
|
1155
|
+
*/
|
|
1156
|
+
parser: any;
|
|
1157
|
+
/**
|
|
1158
|
+
* how many ms before a client without namespace is closed
|
|
1159
|
+
* @default 45000
|
|
1160
|
+
*/
|
|
1161
|
+
connectTimeout: number;
|
|
1162
|
+
/**
|
|
1163
|
+
* Whether to enable the recovery of connection state when a client temporarily disconnects.
|
|
1164
|
+
*
|
|
1165
|
+
* The connection state includes the missed packets, the rooms the socket was in and the `data` attribute.
|
|
1166
|
+
*/
|
|
1167
|
+
connectionStateRecovery: {
|
|
1168
|
+
/**
|
|
1169
|
+
* The backup duration of the sessions and the packets.
|
|
1170
|
+
*
|
|
1171
|
+
* @default 120000 (2 minutes)
|
|
1172
|
+
*/
|
|
1173
|
+
maxDisconnectionDuration?: number;
|
|
1174
|
+
/**
|
|
1175
|
+
* Whether to skip middlewares upon successful connection state recovery.
|
|
1176
|
+
*
|
|
1177
|
+
* @default true
|
|
1178
|
+
*/
|
|
1179
|
+
skipMiddlewares?: boolean;
|
|
1180
|
+
};
|
|
1181
|
+
/**
|
|
1182
|
+
* Whether to remove child namespaces that have no sockets connected to them
|
|
1183
|
+
* @default false
|
|
1184
|
+
*/
|
|
1185
|
+
cleanupEmptyChildNamespaces: boolean;
|
|
1186
|
+
}
|
|
1187
|
+
/**
|
|
1188
|
+
* Represents a Socket.IO server.
|
|
1189
|
+
*
|
|
1190
|
+
* @example
|
|
1191
|
+
* import { Server } from "socket.io";
|
|
1192
|
+
*
|
|
1193
|
+
* const io = new Server();
|
|
1194
|
+
*
|
|
1195
|
+
* io.on("connection", (socket) => {
|
|
1196
|
+
* console.log(`socket ${socket.id} connected`);
|
|
1197
|
+
*
|
|
1198
|
+
* // send an event to the client
|
|
1199
|
+
* socket.emit("foo", "bar");
|
|
1200
|
+
*
|
|
1201
|
+
* socket.on("foobar", () => {
|
|
1202
|
+
* // an event was received from the client
|
|
1203
|
+
* });
|
|
1204
|
+
*
|
|
1205
|
+
* // upon disconnection
|
|
1206
|
+
* socket.on("disconnect", (reason) => {
|
|
1207
|
+
* console.log(`socket ${socket.id} disconnected due to ${reason}`);
|
|
1208
|
+
* });
|
|
1209
|
+
* });
|
|
1210
|
+
*
|
|
1211
|
+
* io.listen(3000);
|
|
1212
|
+
*/
|
|
1213
|
+
declare class Server$1<ListenEvents extends EventsMap$2 = DefaultEventsMap$1, EmitEvents extends EventsMap$2 = ListenEvents, ServerSideEvents extends EventsMap$2 = DefaultEventsMap$1, SocketData = any> extends StrictEventEmitter<ServerSideEvents, RemoveAcknowledgements<EmitEvents>, ServerReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents, SocketData>> {
|
|
1214
|
+
readonly sockets: Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>;
|
|
1215
|
+
/**
|
|
1216
|
+
* A reference to the underlying Engine.IO server.
|
|
1217
|
+
*
|
|
1218
|
+
* @example
|
|
1219
|
+
* const clientsCount = io.engine.clientsCount;
|
|
1220
|
+
*
|
|
1221
|
+
*/
|
|
1222
|
+
engine: BaseServer;
|
|
1223
|
+
/** @private */
|
|
1224
|
+
readonly _parser: typeof parser;
|
|
1225
|
+
/** @private */
|
|
1226
|
+
readonly encoder: Encoder;
|
|
1227
|
+
/**
|
|
1228
|
+
* @private
|
|
1229
|
+
*/
|
|
1230
|
+
_nsps: Map<string, Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>>;
|
|
1231
|
+
private parentNsps;
|
|
1232
|
+
/**
|
|
1233
|
+
* A subset of the {@link parentNsps} map, only containing {@link ParentNamespace} which are based on a regular
|
|
1234
|
+
* expression.
|
|
1235
|
+
*
|
|
1236
|
+
* @private
|
|
1237
|
+
*/
|
|
1238
|
+
private parentNamespacesFromRegExp;
|
|
1239
|
+
private _adapter?;
|
|
1240
|
+
private _serveClient;
|
|
1241
|
+
private readonly opts;
|
|
1242
|
+
private eio;
|
|
1243
|
+
private _path;
|
|
1244
|
+
private clientPathRegex;
|
|
1245
|
+
/**
|
|
1246
|
+
* @private
|
|
1247
|
+
*/
|
|
1248
|
+
_connectTimeout: number;
|
|
1249
|
+
private httpServer;
|
|
1250
|
+
private _corsMiddleware;
|
|
1251
|
+
/**
|
|
1252
|
+
* Server constructor.
|
|
1253
|
+
*
|
|
1254
|
+
* @param srv http server, port, or options
|
|
1255
|
+
* @param [opts]
|
|
1256
|
+
*/
|
|
1257
|
+
constructor(opts?: Partial<ServerOptions$1>);
|
|
1258
|
+
constructor(srv?: TServerInstance$1 | number, opts?: Partial<ServerOptions$1>);
|
|
1259
|
+
constructor(srv: undefined | Partial<ServerOptions$1> | TServerInstance$1 | number, opts?: Partial<ServerOptions$1>);
|
|
1260
|
+
get _opts(): Partial<ServerOptions$1>;
|
|
1261
|
+
/**
|
|
1262
|
+
* Sets/gets whether client code is being served.
|
|
1263
|
+
*
|
|
1264
|
+
* @param v - whether to serve client code
|
|
1265
|
+
* @return self when setting or value when getting
|
|
1266
|
+
*/
|
|
1267
|
+
serveClient(v: boolean): this;
|
|
1268
|
+
serveClient(): boolean;
|
|
1269
|
+
serveClient(v?: boolean): this | boolean;
|
|
1270
|
+
/**
|
|
1271
|
+
* Executes the middleware for an incoming namespace not already created on the server.
|
|
1272
|
+
*
|
|
1273
|
+
* @param name - name of incoming namespace
|
|
1274
|
+
* @param auth - the auth parameters
|
|
1275
|
+
* @param fn - callback
|
|
1276
|
+
*
|
|
1277
|
+
* @private
|
|
1278
|
+
*/
|
|
1279
|
+
_checkNamespace(name: string, auth: {
|
|
1280
|
+
[key: string]: any;
|
|
1281
|
+
}, fn: (nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData> | false) => void): void;
|
|
1282
|
+
/**
|
|
1283
|
+
* Sets the client serving path.
|
|
1284
|
+
*
|
|
1285
|
+
* @param {String} v pathname
|
|
1286
|
+
* @return {Server|String} self when setting or value when getting
|
|
1287
|
+
*/
|
|
1288
|
+
path(v: string): this;
|
|
1289
|
+
path(): string;
|
|
1290
|
+
path(v?: string): this | string;
|
|
1291
|
+
/**
|
|
1292
|
+
* Set the delay after which a client without namespace is closed
|
|
1293
|
+
* @param v
|
|
1294
|
+
*/
|
|
1295
|
+
connectTimeout(v: number): this;
|
|
1296
|
+
connectTimeout(): number;
|
|
1297
|
+
connectTimeout(v?: number): this | number;
|
|
1298
|
+
/**
|
|
1299
|
+
* Sets the adapter for rooms.
|
|
1300
|
+
*
|
|
1301
|
+
* @param v pathname
|
|
1302
|
+
* @return self when setting or value when getting
|
|
1303
|
+
*/
|
|
1304
|
+
adapter(): AdapterConstructor$1 | undefined;
|
|
1305
|
+
adapter(v: AdapterConstructor$1): this;
|
|
1306
|
+
/**
|
|
1307
|
+
* Attaches socket.io to a server or port.
|
|
1308
|
+
*
|
|
1309
|
+
* @param srv - server or port
|
|
1310
|
+
* @param opts - options passed to engine.io
|
|
1311
|
+
* @return self
|
|
1312
|
+
*/
|
|
1313
|
+
listen(srv: TServerInstance$1 | number, opts?: Partial<ServerOptions$1>): this;
|
|
1314
|
+
/**
|
|
1315
|
+
* Attaches socket.io to a server or port.
|
|
1316
|
+
*
|
|
1317
|
+
* @param srv - server or port
|
|
1318
|
+
* @param opts - options passed to engine.io
|
|
1319
|
+
* @return self
|
|
1320
|
+
*/
|
|
1321
|
+
attach(srv: TServerInstance$1 | number, opts?: Partial<ServerOptions$1>): this;
|
|
1322
|
+
attachApp(app: any, opts?: Partial<ServerOptions$1>): void;
|
|
1323
|
+
/**
|
|
1324
|
+
* Initialize engine
|
|
1325
|
+
*
|
|
1326
|
+
* @param srv - the server to attach to
|
|
1327
|
+
* @param opts - options passed to engine.io
|
|
1328
|
+
* @private
|
|
1329
|
+
*/
|
|
1330
|
+
private initEngine;
|
|
1331
|
+
/**
|
|
1332
|
+
* Attaches the static file serving.
|
|
1333
|
+
*
|
|
1334
|
+
* @param srv http server
|
|
1335
|
+
* @private
|
|
1336
|
+
*/
|
|
1337
|
+
private attachServe;
|
|
1338
|
+
/**
|
|
1339
|
+
* Handles a request serving of client source and map
|
|
1340
|
+
*
|
|
1341
|
+
* @param req
|
|
1342
|
+
* @param res
|
|
1343
|
+
* @private
|
|
1344
|
+
*/
|
|
1345
|
+
private serve;
|
|
1346
|
+
/**
|
|
1347
|
+
* @param filename
|
|
1348
|
+
* @param req
|
|
1349
|
+
* @param res
|
|
1350
|
+
* @private
|
|
1351
|
+
*/
|
|
1352
|
+
private static sendFile;
|
|
1353
|
+
/**
|
|
1354
|
+
* Binds socket.io to an engine.io instance.
|
|
1355
|
+
*
|
|
1356
|
+
* @param engine engine.io (or compatible) server
|
|
1357
|
+
* @return self
|
|
1358
|
+
*/
|
|
1359
|
+
bind(engine: BaseServer): this;
|
|
1360
|
+
/**
|
|
1361
|
+
* Called with each incoming transport connection.
|
|
1362
|
+
*
|
|
1363
|
+
* @param {engine.Socket} conn
|
|
1364
|
+
* @return self
|
|
1365
|
+
* @private
|
|
1366
|
+
*/
|
|
1367
|
+
private onconnection;
|
|
1368
|
+
/**
|
|
1369
|
+
* Looks up a namespace.
|
|
1370
|
+
*
|
|
1371
|
+
* @example
|
|
1372
|
+
* // with a simple string
|
|
1373
|
+
* const myNamespace = io.of("/my-namespace");
|
|
1374
|
+
*
|
|
1375
|
+
* // with a regex
|
|
1376
|
+
* const dynamicNsp = io.of(/^\/dynamic-\d+$/).on("connection", (socket) => {
|
|
1377
|
+
* const namespace = socket.nsp; // newNamespace.name === "/dynamic-101"
|
|
1378
|
+
*
|
|
1379
|
+
* // broadcast to all clients in the given sub-namespace
|
|
1380
|
+
* namespace.emit("hello");
|
|
1381
|
+
* });
|
|
1382
|
+
*
|
|
1383
|
+
* @param name - nsp name
|
|
1384
|
+
* @param fn optional, nsp `connection` ev handler
|
|
1385
|
+
*/
|
|
1386
|
+
of(name: string | RegExp | ParentNspNameMatchFn$1, fn?: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void): Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>;
|
|
1387
|
+
/**
|
|
1388
|
+
* Closes server connection
|
|
1389
|
+
*
|
|
1390
|
+
* @param [fn] optional, called as `fn([err])` on error OR all conns closed
|
|
1391
|
+
*/
|
|
1392
|
+
close(fn?: (err?: Error) => void): void;
|
|
1393
|
+
/**
|
|
1394
|
+
* Registers a middleware, which is a function that gets executed for every incoming {@link Socket}.
|
|
1395
|
+
*
|
|
1396
|
+
* @example
|
|
1397
|
+
* io.use((socket, next) => {
|
|
1398
|
+
* // ...
|
|
1399
|
+
* next();
|
|
1400
|
+
* });
|
|
1401
|
+
*
|
|
1402
|
+
* @param fn - the middleware function
|
|
1403
|
+
*/
|
|
1404
|
+
use(fn: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, next: (err?: ExtendedError$1) => void) => void): this;
|
|
1405
|
+
/**
|
|
1406
|
+
* Targets a room when emitting.
|
|
1407
|
+
*
|
|
1408
|
+
* @example
|
|
1409
|
+
* // the “foo” event will be broadcast to all connected clients in the “room-101” room
|
|
1410
|
+
* io.to("room-101").emit("foo", "bar");
|
|
1411
|
+
*
|
|
1412
|
+
* // with an array of rooms (a client will be notified at most once)
|
|
1413
|
+
* io.to(["room-101", "room-102"]).emit("foo", "bar");
|
|
1414
|
+
*
|
|
1415
|
+
* // with multiple chained calls
|
|
1416
|
+
* io.to("room-101").to("room-102").emit("foo", "bar");
|
|
1417
|
+
*
|
|
1418
|
+
* @param room - a room, or an array of rooms
|
|
1419
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
1420
|
+
*/
|
|
1421
|
+
to(room: Room | Room[]): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
1422
|
+
/**
|
|
1423
|
+
* Targets a room when emitting. Similar to `to()`, but might feel clearer in some cases:
|
|
1424
|
+
*
|
|
1425
|
+
* @example
|
|
1426
|
+
* // disconnect all clients in the "room-101" room
|
|
1427
|
+
* io.in("room-101").disconnectSockets();
|
|
1428
|
+
*
|
|
1429
|
+
* @param room - a room, or an array of rooms
|
|
1430
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
1431
|
+
*/
|
|
1432
|
+
in(room: Room | Room[]): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
1433
|
+
/**
|
|
1434
|
+
* Excludes a room when emitting.
|
|
1435
|
+
*
|
|
1436
|
+
* @example
|
|
1437
|
+
* // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
|
|
1438
|
+
* io.except("room-101").emit("foo", "bar");
|
|
1439
|
+
*
|
|
1440
|
+
* // with an array of rooms
|
|
1441
|
+
* io.except(["room-101", "room-102"]).emit("foo", "bar");
|
|
1442
|
+
*
|
|
1443
|
+
* // with multiple chained calls
|
|
1444
|
+
* io.except("room-101").except("room-102").emit("foo", "bar");
|
|
1445
|
+
*
|
|
1446
|
+
* @param room - a room, or an array of rooms
|
|
1447
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
1448
|
+
*/
|
|
1449
|
+
except(room: Room | Room[]): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
1450
|
+
/**
|
|
1451
|
+
* Sends a `message` event to all clients.
|
|
1452
|
+
*
|
|
1453
|
+
* This method mimics the WebSocket.send() method.
|
|
1454
|
+
*
|
|
1455
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
|
|
1456
|
+
*
|
|
1457
|
+
* @example
|
|
1458
|
+
* io.send("hello");
|
|
1459
|
+
*
|
|
1460
|
+
* // this is equivalent to
|
|
1461
|
+
* io.emit("message", "hello");
|
|
1462
|
+
*
|
|
1463
|
+
* @return self
|
|
1464
|
+
*/
|
|
1465
|
+
send(...args: EventParams$1<EmitEvents, "message">): this;
|
|
1466
|
+
/**
|
|
1467
|
+
* Sends a `message` event to all clients. Alias of {@link send}.
|
|
1468
|
+
*
|
|
1469
|
+
* @return self
|
|
1470
|
+
*/
|
|
1471
|
+
write(...args: EventParams$1<EmitEvents, "message">): this;
|
|
1472
|
+
/**
|
|
1473
|
+
* Sends a message to the other Socket.IO servers of the cluster.
|
|
1474
|
+
*
|
|
1475
|
+
* @example
|
|
1476
|
+
* io.serverSideEmit("hello", "world");
|
|
1477
|
+
*
|
|
1478
|
+
* io.on("hello", (arg1) => {
|
|
1479
|
+
* console.log(arg1); // prints "world"
|
|
1480
|
+
* });
|
|
1481
|
+
*
|
|
1482
|
+
* // acknowledgements (without binary content) are supported too:
|
|
1483
|
+
* io.serverSideEmit("ping", (err, responses) => {
|
|
1484
|
+
* if (err) {
|
|
1485
|
+
* // some servers did not acknowledge the event in the given delay
|
|
1486
|
+
* } else {
|
|
1487
|
+
* console.log(responses); // one response per server (except the current one)
|
|
1488
|
+
* }
|
|
1489
|
+
* });
|
|
1490
|
+
*
|
|
1491
|
+
* io.on("ping", (cb) => {
|
|
1492
|
+
* cb("pong");
|
|
1493
|
+
* });
|
|
1494
|
+
*
|
|
1495
|
+
* @param ev - the event name
|
|
1496
|
+
* @param args - an array of arguments, which may include an acknowledgement callback at the end
|
|
1497
|
+
*/
|
|
1498
|
+
serverSideEmit<Ev extends EventNames$1<ServerSideEvents>>(ev: Ev, ...args: EventParams$1<DecorateAcknowledgementsWithTimeoutAndMultipleResponses<ServerSideEvents>, Ev>): boolean;
|
|
1499
|
+
/**
|
|
1500
|
+
* Sends a message and expect an acknowledgement from the other Socket.IO servers of the cluster.
|
|
1501
|
+
*
|
|
1502
|
+
* @example
|
|
1503
|
+
* try {
|
|
1504
|
+
* const responses = await io.serverSideEmitWithAck("ping");
|
|
1505
|
+
* console.log(responses); // one response per server (except the current one)
|
|
1506
|
+
* } catch (e) {
|
|
1507
|
+
* // some servers did not acknowledge the event in the given delay
|
|
1508
|
+
* }
|
|
1509
|
+
*
|
|
1510
|
+
* @param ev - the event name
|
|
1511
|
+
* @param args - an array of arguments
|
|
1512
|
+
*
|
|
1513
|
+
* @return a Promise that will be fulfilled when all servers have acknowledged the event
|
|
1514
|
+
*/
|
|
1515
|
+
serverSideEmitWithAck<Ev extends EventNamesWithAck<ServerSideEvents>>(ev: Ev, ...args: AllButLast<EventParams$1<ServerSideEvents, Ev>>): Promise<FirstNonErrorArg<Last<EventParams$1<ServerSideEvents, Ev>>>[]>;
|
|
1516
|
+
/**
|
|
1517
|
+
* Gets a list of socket ids.
|
|
1518
|
+
*
|
|
1519
|
+
* @deprecated this method will be removed in the next major release, please use {@link Server#serverSideEmit} or
|
|
1520
|
+
* {@link Server#fetchSockets} instead.
|
|
1521
|
+
*/
|
|
1522
|
+
allSockets(): Promise<Set<SocketId>>;
|
|
1523
|
+
/**
|
|
1524
|
+
* Sets the compress flag.
|
|
1525
|
+
*
|
|
1526
|
+
* @example
|
|
1527
|
+
* io.compress(false).emit("hello");
|
|
1528
|
+
*
|
|
1529
|
+
* @param compress - if `true`, compresses the sending data
|
|
1530
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
1531
|
+
*/
|
|
1532
|
+
compress(compress: boolean): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
1533
|
+
/**
|
|
1534
|
+
* Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
|
|
1535
|
+
* receive messages (because of network slowness or other issues, or because they’re connected through long polling
|
|
1536
|
+
* and is in the middle of a request-response cycle).
|
|
1537
|
+
*
|
|
1538
|
+
* @example
|
|
1539
|
+
* io.volatile.emit("hello"); // the clients may or may not receive it
|
|
1540
|
+
*
|
|
1541
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
1542
|
+
*/
|
|
1543
|
+
get volatile(): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
1544
|
+
/**
|
|
1545
|
+
* Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
|
|
1546
|
+
*
|
|
1547
|
+
* @example
|
|
1548
|
+
* // the “foo” event will be broadcast to all connected clients on this node
|
|
1549
|
+
* io.local.emit("foo", "bar");
|
|
1550
|
+
*
|
|
1551
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
1552
|
+
*/
|
|
1553
|
+
get local(): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
1554
|
+
/**
|
|
1555
|
+
* Adds a timeout in milliseconds for the next operation.
|
|
1556
|
+
*
|
|
1557
|
+
* @example
|
|
1558
|
+
* io.timeout(1000).emit("some-event", (err, responses) => {
|
|
1559
|
+
* if (err) {
|
|
1560
|
+
* // some clients did not acknowledge the event in the given delay
|
|
1561
|
+
* } else {
|
|
1562
|
+
* console.log(responses); // one response per client
|
|
1563
|
+
* }
|
|
1564
|
+
* });
|
|
1565
|
+
*
|
|
1566
|
+
* @param timeout
|
|
1567
|
+
*/
|
|
1568
|
+
timeout(timeout: number): BroadcastOperator<DecorateAcknowledgements<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>>, SocketData>;
|
|
1569
|
+
/**
|
|
1570
|
+
* Returns the matching socket instances.
|
|
1571
|
+
*
|
|
1572
|
+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
|
1573
|
+
*
|
|
1574
|
+
* @example
|
|
1575
|
+
* // return all Socket instances
|
|
1576
|
+
* const sockets = await io.fetchSockets();
|
|
1577
|
+
*
|
|
1578
|
+
* // return all Socket instances in the "room1" room
|
|
1579
|
+
* const sockets = await io.in("room1").fetchSockets();
|
|
1580
|
+
*
|
|
1581
|
+
* for (const socket of sockets) {
|
|
1582
|
+
* console.log(socket.id);
|
|
1583
|
+
* console.log(socket.handshake);
|
|
1584
|
+
* console.log(socket.rooms);
|
|
1585
|
+
* console.log(socket.data);
|
|
1586
|
+
*
|
|
1587
|
+
* socket.emit("hello");
|
|
1588
|
+
* socket.join("room1");
|
|
1589
|
+
* socket.leave("room2");
|
|
1590
|
+
* socket.disconnect();
|
|
1591
|
+
* }
|
|
1592
|
+
*/
|
|
1593
|
+
fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]>;
|
|
1594
|
+
/**
|
|
1595
|
+
* Makes the matching socket instances join the specified rooms.
|
|
1596
|
+
*
|
|
1597
|
+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
|
1598
|
+
*
|
|
1599
|
+
* @example
|
|
1600
|
+
*
|
|
1601
|
+
* // make all socket instances join the "room1" room
|
|
1602
|
+
* io.socketsJoin("room1");
|
|
1603
|
+
*
|
|
1604
|
+
* // make all socket instances in the "room1" room join the "room2" and "room3" rooms
|
|
1605
|
+
* io.in("room1").socketsJoin(["room2", "room3"]);
|
|
1606
|
+
*
|
|
1607
|
+
* @param room - a room, or an array of rooms
|
|
1608
|
+
*/
|
|
1609
|
+
socketsJoin(room: Room | Room[]): void;
|
|
1610
|
+
/**
|
|
1611
|
+
* Makes the matching socket instances leave the specified rooms.
|
|
1612
|
+
*
|
|
1613
|
+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
|
1614
|
+
*
|
|
1615
|
+
* @example
|
|
1616
|
+
* // make all socket instances leave the "room1" room
|
|
1617
|
+
* io.socketsLeave("room1");
|
|
1618
|
+
*
|
|
1619
|
+
* // make all socket instances in the "room1" room leave the "room2" and "room3" rooms
|
|
1620
|
+
* io.in("room1").socketsLeave(["room2", "room3"]);
|
|
1621
|
+
*
|
|
1622
|
+
* @param room - a room, or an array of rooms
|
|
1623
|
+
*/
|
|
1624
|
+
socketsLeave(room: Room | Room[]): void;
|
|
1625
|
+
/**
|
|
1626
|
+
* Makes the matching socket instances disconnect.
|
|
1627
|
+
*
|
|
1628
|
+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
|
1629
|
+
*
|
|
1630
|
+
* @example
|
|
1631
|
+
* // make all socket instances disconnect (the connections might be kept alive for other namespaces)
|
|
1632
|
+
* io.disconnectSockets();
|
|
1633
|
+
*
|
|
1634
|
+
* // make all socket instances in the "room1" room disconnect and close the underlying connections
|
|
1635
|
+
* io.in("room1").disconnectSockets(true);
|
|
1636
|
+
*
|
|
1637
|
+
* @param close - whether to close the underlying connection
|
|
1638
|
+
*/
|
|
1639
|
+
disconnectSockets(close?: boolean): void;
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
interface WriteOptions {
|
|
1643
|
+
compress?: boolean;
|
|
1644
|
+
volatile?: boolean;
|
|
1645
|
+
preEncoded?: boolean;
|
|
1646
|
+
wsPreEncoded?: string;
|
|
1647
|
+
}
|
|
1648
|
+
declare class Client<ListenEvents extends EventsMap$2, EmitEvents extends EventsMap$2, ServerSideEvents extends EventsMap$2, SocketData = any> {
|
|
1649
|
+
readonly conn: Socket$1;
|
|
1650
|
+
private readonly id;
|
|
1651
|
+
private readonly server;
|
|
1652
|
+
private readonly encoder;
|
|
1653
|
+
private readonly decoder;
|
|
1654
|
+
private sockets;
|
|
1655
|
+
private nsps;
|
|
1656
|
+
private connectTimeout?;
|
|
1657
|
+
/**
|
|
1658
|
+
* Client constructor.
|
|
1659
|
+
*
|
|
1660
|
+
* @param server instance
|
|
1661
|
+
* @param conn
|
|
1662
|
+
* @package
|
|
1663
|
+
*/
|
|
1664
|
+
constructor(server: Server$1<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, conn: any);
|
|
1665
|
+
/**
|
|
1666
|
+
* @return the reference to the request that originated the Engine.IO connection
|
|
1667
|
+
*
|
|
1668
|
+
* @public
|
|
1669
|
+
*/
|
|
1670
|
+
get request(): IncomingMessage;
|
|
1671
|
+
/**
|
|
1672
|
+
* Sets up event listeners.
|
|
1673
|
+
*
|
|
1674
|
+
* @private
|
|
1675
|
+
*/
|
|
1676
|
+
private setup;
|
|
1677
|
+
/**
|
|
1678
|
+
* Connects a client to a namespace.
|
|
1679
|
+
*
|
|
1680
|
+
* @param {String} name - the namespace
|
|
1681
|
+
* @param {Object} auth - the auth parameters
|
|
1682
|
+
* @private
|
|
1683
|
+
*/
|
|
1684
|
+
private connect;
|
|
1685
|
+
/**
|
|
1686
|
+
* Connects a client to a namespace.
|
|
1687
|
+
*
|
|
1688
|
+
* @param name - the namespace
|
|
1689
|
+
* @param {Object} auth - the auth parameters
|
|
1690
|
+
*
|
|
1691
|
+
* @private
|
|
1692
|
+
*/
|
|
1693
|
+
private doConnect;
|
|
1694
|
+
/**
|
|
1695
|
+
* Disconnects from all namespaces and closes transport.
|
|
1696
|
+
*
|
|
1697
|
+
* @private
|
|
1698
|
+
*/
|
|
1699
|
+
_disconnect(): void;
|
|
1700
|
+
/**
|
|
1701
|
+
* Removes a socket. Called by each `Socket`.
|
|
1702
|
+
*
|
|
1703
|
+
* @private
|
|
1704
|
+
*/
|
|
1705
|
+
_remove(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>): void;
|
|
1706
|
+
/**
|
|
1707
|
+
* Closes the underlying connection.
|
|
1708
|
+
*
|
|
1709
|
+
* @private
|
|
1710
|
+
*/
|
|
1711
|
+
private close;
|
|
1712
|
+
/**
|
|
1713
|
+
* Writes a packet to the transport.
|
|
1714
|
+
*
|
|
1715
|
+
* @param {Object} packet object
|
|
1716
|
+
* @param {Object} opts
|
|
1717
|
+
* @private
|
|
1718
|
+
*/
|
|
1719
|
+
_packet(packet: Packet | any[], opts?: WriteOptions): void;
|
|
1720
|
+
private writeToEngine;
|
|
1721
|
+
/**
|
|
1722
|
+
* Called with incoming transport data.
|
|
1723
|
+
*
|
|
1724
|
+
* @private
|
|
1725
|
+
*/
|
|
1726
|
+
private ondata;
|
|
1727
|
+
/**
|
|
1728
|
+
* Called when parser fully decodes a packet.
|
|
1729
|
+
*
|
|
1730
|
+
* @private
|
|
1731
|
+
*/
|
|
1732
|
+
private ondecoded;
|
|
1733
|
+
/**
|
|
1734
|
+
* Handles an error.
|
|
1735
|
+
*
|
|
1736
|
+
* @param {Object} err object
|
|
1737
|
+
* @private
|
|
1738
|
+
*/
|
|
1739
|
+
private onerror;
|
|
1740
|
+
/**
|
|
1741
|
+
* Called upon transport close.
|
|
1742
|
+
*
|
|
1743
|
+
* @param reason
|
|
1744
|
+
* @param description
|
|
1745
|
+
* @private
|
|
1746
|
+
*/
|
|
1747
|
+
private onclose;
|
|
1748
|
+
/**
|
|
1749
|
+
* Cleans up event listeners.
|
|
1750
|
+
* @private
|
|
1751
|
+
*/
|
|
1752
|
+
private destroy;
|
|
1753
|
+
}
|
|
1754
|
+
|
|
1755
|
+
declare type DisconnectReason = "transport error" | "transport close" | "forced close" | "ping timeout" | "parse error" | "server shutting down" | "forced server close" | "client namespace disconnect" | "server namespace disconnect";
|
|
1756
|
+
interface SocketReservedEventsMap {
|
|
1757
|
+
disconnect: (reason: DisconnectReason, description?: any) => void;
|
|
1758
|
+
disconnecting: (reason: DisconnectReason, description?: any) => void;
|
|
1759
|
+
error: (err: Error) => void;
|
|
1760
|
+
}
|
|
1761
|
+
/**
|
|
1762
|
+
* The handshake details
|
|
1763
|
+
*/
|
|
1764
|
+
interface Handshake {
|
|
1765
|
+
/**
|
|
1766
|
+
* The headers sent as part of the handshake
|
|
1767
|
+
*/
|
|
1768
|
+
headers: IncomingHttpHeaders;
|
|
1769
|
+
/**
|
|
1770
|
+
* The date of creation (as string)
|
|
1771
|
+
*/
|
|
1772
|
+
time: string;
|
|
1773
|
+
/**
|
|
1774
|
+
* The ip of the client
|
|
1775
|
+
*/
|
|
1776
|
+
address: string;
|
|
1777
|
+
/**
|
|
1778
|
+
* Whether the connection is cross-domain
|
|
1779
|
+
*/
|
|
1780
|
+
xdomain: boolean;
|
|
1781
|
+
/**
|
|
1782
|
+
* Whether the connection is secure
|
|
1783
|
+
*/
|
|
1784
|
+
secure: boolean;
|
|
1785
|
+
/**
|
|
1786
|
+
* The date of creation (as unix timestamp)
|
|
1787
|
+
*/
|
|
1788
|
+
issued: number;
|
|
1789
|
+
/**
|
|
1790
|
+
* The request URL string
|
|
1791
|
+
*/
|
|
1792
|
+
url: string;
|
|
1793
|
+
/**
|
|
1794
|
+
* The query object
|
|
1795
|
+
*/
|
|
1796
|
+
query: ParsedUrlQuery;
|
|
1797
|
+
/**
|
|
1798
|
+
* The auth object
|
|
1799
|
+
*/
|
|
1800
|
+
auth: {
|
|
1801
|
+
[key: string]: any;
|
|
1802
|
+
};
|
|
1803
|
+
}
|
|
1804
|
+
/**
|
|
1805
|
+
* `[eventName, ...args]`
|
|
1806
|
+
*/
|
|
1807
|
+
declare type Event = [string, ...any[]];
|
|
1808
|
+
/**
|
|
1809
|
+
* This is the main object for interacting with a client.
|
|
1810
|
+
*
|
|
1811
|
+
* A Socket belongs to a given {@link Namespace} and uses an underlying {@link Client} to communicate.
|
|
1812
|
+
*
|
|
1813
|
+
* Within each {@link Namespace}, you can also define arbitrary channels (called "rooms") that the {@link Socket} can
|
|
1814
|
+
* join and leave. That provides a convenient way to broadcast to a group of socket instances.
|
|
1815
|
+
*
|
|
1816
|
+
* @example
|
|
1817
|
+
* io.on("connection", (socket) => {
|
|
1818
|
+
* console.log(`socket ${socket.id} connected`);
|
|
1819
|
+
*
|
|
1820
|
+
* // send an event to the client
|
|
1821
|
+
* socket.emit("foo", "bar");
|
|
1822
|
+
*
|
|
1823
|
+
* socket.on("foobar", () => {
|
|
1824
|
+
* // an event was received from the client
|
|
1825
|
+
* });
|
|
1826
|
+
*
|
|
1827
|
+
* // join the room named "room1"
|
|
1828
|
+
* socket.join("room1");
|
|
1829
|
+
*
|
|
1830
|
+
* // broadcast to everyone in the room named "room1"
|
|
1831
|
+
* io.to("room1").emit("hello");
|
|
1832
|
+
*
|
|
1833
|
+
* // upon disconnection
|
|
1834
|
+
* socket.on("disconnect", (reason) => {
|
|
1835
|
+
* console.log(`socket ${socket.id} disconnected due to ${reason}`);
|
|
1836
|
+
* });
|
|
1837
|
+
* });
|
|
1838
|
+
*/
|
|
1839
|
+
declare class Socket<ListenEvents extends EventsMap$2 = DefaultEventsMap$1, EmitEvents extends EventsMap$2 = ListenEvents, ServerSideEvents extends EventsMap$2 = DefaultEventsMap$1, SocketData = any> extends StrictEventEmitter<ListenEvents, EmitEvents, SocketReservedEventsMap> {
|
|
1840
|
+
readonly nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents>;
|
|
1841
|
+
readonly client: Client<ListenEvents, EmitEvents, ServerSideEvents>;
|
|
1842
|
+
/**
|
|
1843
|
+
* An unique identifier for the session.
|
|
1844
|
+
*/
|
|
1845
|
+
readonly id: SocketId;
|
|
1846
|
+
/**
|
|
1847
|
+
* Whether the connection state was recovered after a temporary disconnection. In that case, any missed packets will
|
|
1848
|
+
* be transmitted to the client, the data attribute and the rooms will be restored.
|
|
1849
|
+
*/
|
|
1850
|
+
readonly recovered: boolean;
|
|
1851
|
+
/**
|
|
1852
|
+
* The handshake details.
|
|
1853
|
+
*/
|
|
1854
|
+
readonly handshake: Handshake;
|
|
1855
|
+
/**
|
|
1856
|
+
* Additional information that can be attached to the Socket instance and which will be used in the
|
|
1857
|
+
* {@link Server.fetchSockets()} method.
|
|
1858
|
+
*/
|
|
1859
|
+
data: SocketData;
|
|
1860
|
+
/**
|
|
1861
|
+
* Whether the socket is currently connected or not.
|
|
1862
|
+
*
|
|
1863
|
+
* @example
|
|
1864
|
+
* io.use((socket, next) => {
|
|
1865
|
+
* console.log(socket.connected); // false
|
|
1866
|
+
* next();
|
|
1867
|
+
* });
|
|
1868
|
+
*
|
|
1869
|
+
* io.on("connection", (socket) => {
|
|
1870
|
+
* console.log(socket.connected); // true
|
|
1871
|
+
* });
|
|
1872
|
+
*/
|
|
1873
|
+
connected: boolean;
|
|
1874
|
+
/**
|
|
1875
|
+
* The session ID, which must not be shared (unlike {@link id}).
|
|
1876
|
+
*
|
|
1877
|
+
* @private
|
|
1878
|
+
*/
|
|
1879
|
+
private readonly pid;
|
|
1880
|
+
private readonly server;
|
|
1881
|
+
private readonly adapter;
|
|
1882
|
+
private acks;
|
|
1883
|
+
private fns;
|
|
1884
|
+
private flags;
|
|
1885
|
+
private _anyListeners?;
|
|
1886
|
+
private _anyOutgoingListeners?;
|
|
1887
|
+
/**
|
|
1888
|
+
* Interface to a `Client` for a given `Namespace`.
|
|
1889
|
+
*
|
|
1890
|
+
* @param {Namespace} nsp
|
|
1891
|
+
* @param {Client} client
|
|
1892
|
+
* @param {Object} auth
|
|
1893
|
+
* @package
|
|
1894
|
+
*/
|
|
1895
|
+
constructor(nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents>, client: Client<ListenEvents, EmitEvents, ServerSideEvents>, auth: Record<string, unknown>, previousSession?: Session);
|
|
1896
|
+
/**
|
|
1897
|
+
* Builds the `handshake` BC object
|
|
1898
|
+
*
|
|
1899
|
+
* @private
|
|
1900
|
+
*/
|
|
1901
|
+
private buildHandshake;
|
|
1902
|
+
/**
|
|
1903
|
+
* Emits to this client.
|
|
1904
|
+
*
|
|
1905
|
+
* @example
|
|
1906
|
+
* io.on("connection", (socket) => {
|
|
1907
|
+
* socket.emit("hello", "world");
|
|
1908
|
+
*
|
|
1909
|
+
* // all serializable datastructures are supported (no need to call JSON.stringify)
|
|
1910
|
+
* socket.emit("hello", 1, "2", { 3: ["4"], 5: Buffer.from([6]) });
|
|
1911
|
+
*
|
|
1912
|
+
* // with an acknowledgement from the client
|
|
1913
|
+
* socket.emit("hello", "world", (val) => {
|
|
1914
|
+
* // ...
|
|
1915
|
+
* });
|
|
1916
|
+
* });
|
|
1917
|
+
*
|
|
1918
|
+
* @return Always returns `true`.
|
|
1919
|
+
*/
|
|
1920
|
+
emit<Ev extends EventNames$1<EmitEvents>>(ev: Ev, ...args: EventParams$1<EmitEvents, Ev>): boolean;
|
|
1921
|
+
/**
|
|
1922
|
+
* Emits an event and waits for an acknowledgement
|
|
1923
|
+
*
|
|
1924
|
+
* @example
|
|
1925
|
+
* io.on("connection", async (socket) => {
|
|
1926
|
+
* // without timeout
|
|
1927
|
+
* const response = await socket.emitWithAck("hello", "world");
|
|
1928
|
+
*
|
|
1929
|
+
* // with a specific timeout
|
|
1930
|
+
* try {
|
|
1931
|
+
* const response = await socket.timeout(1000).emitWithAck("hello", "world");
|
|
1932
|
+
* } catch (err) {
|
|
1933
|
+
* // the client did not acknowledge the event in the given delay
|
|
1934
|
+
* }
|
|
1935
|
+
* });
|
|
1936
|
+
*
|
|
1937
|
+
* @return a Promise that will be fulfilled when the client acknowledges the event
|
|
1938
|
+
*/
|
|
1939
|
+
emitWithAck<Ev extends EventNamesWithAck<EmitEvents>>(ev: Ev, ...args: AllButLast<EventParams$1<EmitEvents, Ev>>): Promise<FirstNonErrorArg<Last<EventParams$1<EmitEvents, Ev>>>>;
|
|
1940
|
+
/**
|
|
1941
|
+
* @private
|
|
1942
|
+
*/
|
|
1943
|
+
private registerAckCallback;
|
|
1944
|
+
/**
|
|
1945
|
+
* Targets a room when broadcasting.
|
|
1946
|
+
*
|
|
1947
|
+
* @example
|
|
1948
|
+
* io.on("connection", (socket) => {
|
|
1949
|
+
* // the “foo” event will be broadcast to all connected clients in the “room-101” room, except this socket
|
|
1950
|
+
* socket.to("room-101").emit("foo", "bar");
|
|
1951
|
+
*
|
|
1952
|
+
* // the code above is equivalent to:
|
|
1953
|
+
* io.to("room-101").except(socket.id).emit("foo", "bar");
|
|
1954
|
+
*
|
|
1955
|
+
* // with an array of rooms (a client will be notified at most once)
|
|
1956
|
+
* socket.to(["room-101", "room-102"]).emit("foo", "bar");
|
|
1957
|
+
*
|
|
1958
|
+
* // with multiple chained calls
|
|
1959
|
+
* socket.to("room-101").to("room-102").emit("foo", "bar");
|
|
1960
|
+
* });
|
|
1961
|
+
*
|
|
1962
|
+
* @param room - a room, or an array of rooms
|
|
1963
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
1964
|
+
*/
|
|
1965
|
+
to(room: Room | Room[]): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
1966
|
+
/**
|
|
1967
|
+
* Targets a room when broadcasting. Similar to `to()`, but might feel clearer in some cases:
|
|
1968
|
+
*
|
|
1969
|
+
* @example
|
|
1970
|
+
* io.on("connection", (socket) => {
|
|
1971
|
+
* // disconnect all clients in the "room-101" room, except this socket
|
|
1972
|
+
* socket.in("room-101").disconnectSockets();
|
|
1973
|
+
* });
|
|
1974
|
+
*
|
|
1975
|
+
* @param room - a room, or an array of rooms
|
|
1976
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
1977
|
+
*/
|
|
1978
|
+
in(room: Room | Room[]): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
1979
|
+
/**
|
|
1980
|
+
* Excludes a room when broadcasting.
|
|
1981
|
+
*
|
|
1982
|
+
* @example
|
|
1983
|
+
* io.on("connection", (socket) => {
|
|
1984
|
+
* // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
|
|
1985
|
+
* // and this socket
|
|
1986
|
+
* socket.except("room-101").emit("foo", "bar");
|
|
1987
|
+
*
|
|
1988
|
+
* // with an array of rooms
|
|
1989
|
+
* socket.except(["room-101", "room-102"]).emit("foo", "bar");
|
|
1990
|
+
*
|
|
1991
|
+
* // with multiple chained calls
|
|
1992
|
+
* socket.except("room-101").except("room-102").emit("foo", "bar");
|
|
1993
|
+
* });
|
|
1994
|
+
*
|
|
1995
|
+
* @param room - a room, or an array of rooms
|
|
1996
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
1997
|
+
*/
|
|
1998
|
+
except(room: Room | Room[]): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
1999
|
+
/**
|
|
2000
|
+
* Sends a `message` event.
|
|
2001
|
+
*
|
|
2002
|
+
* This method mimics the WebSocket.send() method.
|
|
2003
|
+
*
|
|
2004
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
|
|
2005
|
+
*
|
|
2006
|
+
* @example
|
|
2007
|
+
* io.on("connection", (socket) => {
|
|
2008
|
+
* socket.send("hello");
|
|
2009
|
+
*
|
|
2010
|
+
* // this is equivalent to
|
|
2011
|
+
* socket.emit("message", "hello");
|
|
2012
|
+
* });
|
|
2013
|
+
*
|
|
2014
|
+
* @return self
|
|
2015
|
+
*/
|
|
2016
|
+
send(...args: EventParams$1<EmitEvents, "message">): this;
|
|
2017
|
+
/**
|
|
2018
|
+
* Sends a `message` event. Alias of {@link send}.
|
|
2019
|
+
*
|
|
2020
|
+
* @return self
|
|
2021
|
+
*/
|
|
2022
|
+
write(...args: EventParams$1<EmitEvents, "message">): this;
|
|
2023
|
+
/**
|
|
2024
|
+
* Writes a packet.
|
|
2025
|
+
*
|
|
2026
|
+
* @param {Object} packet - packet object
|
|
2027
|
+
* @param {Object} opts - options
|
|
2028
|
+
* @private
|
|
2029
|
+
*/
|
|
2030
|
+
private packet;
|
|
2031
|
+
/**
|
|
2032
|
+
* Joins a room.
|
|
2033
|
+
*
|
|
2034
|
+
* @example
|
|
2035
|
+
* io.on("connection", (socket) => {
|
|
2036
|
+
* // join a single room
|
|
2037
|
+
* socket.join("room1");
|
|
2038
|
+
*
|
|
2039
|
+
* // join multiple rooms
|
|
2040
|
+
* socket.join(["room1", "room2"]);
|
|
2041
|
+
* });
|
|
2042
|
+
*
|
|
2043
|
+
* @param {String|Array} rooms - room or array of rooms
|
|
2044
|
+
* @return a Promise or nothing, depending on the adapter
|
|
2045
|
+
*/
|
|
2046
|
+
join(rooms: Room | Array<Room>): Promise<void> | void;
|
|
2047
|
+
/**
|
|
2048
|
+
* Leaves a room.
|
|
2049
|
+
*
|
|
2050
|
+
* @example
|
|
2051
|
+
* io.on("connection", (socket) => {
|
|
2052
|
+
* // leave a single room
|
|
2053
|
+
* socket.leave("room1");
|
|
2054
|
+
*
|
|
2055
|
+
* // leave multiple rooms
|
|
2056
|
+
* socket.leave("room1").leave("room2");
|
|
2057
|
+
* });
|
|
2058
|
+
*
|
|
2059
|
+
* @param {String} room
|
|
2060
|
+
* @return a Promise or nothing, depending on the adapter
|
|
2061
|
+
*/
|
|
2062
|
+
leave(room: string): Promise<void> | void;
|
|
2063
|
+
/**
|
|
2064
|
+
* Leave all rooms.
|
|
2065
|
+
*
|
|
2066
|
+
* @private
|
|
2067
|
+
*/
|
|
2068
|
+
private leaveAll;
|
|
2069
|
+
/**
|
|
2070
|
+
* Called by `Namespace` upon successful
|
|
2071
|
+
* middleware execution (ie: authorization).
|
|
2072
|
+
* Socket is added to namespace array before
|
|
2073
|
+
* call to join, so adapters can access it.
|
|
2074
|
+
*
|
|
2075
|
+
* @private
|
|
2076
|
+
*/
|
|
2077
|
+
_onconnect(): void;
|
|
2078
|
+
/**
|
|
2079
|
+
* Called with each packet. Called by `Client`.
|
|
2080
|
+
*
|
|
2081
|
+
* @param {Object} packet
|
|
2082
|
+
* @private
|
|
2083
|
+
*/
|
|
2084
|
+
_onpacket(packet: Packet): void;
|
|
2085
|
+
/**
|
|
2086
|
+
* Called upon event packet.
|
|
2087
|
+
*
|
|
2088
|
+
* @param {Packet} packet - packet object
|
|
2089
|
+
* @private
|
|
2090
|
+
*/
|
|
2091
|
+
private onevent;
|
|
2092
|
+
/**
|
|
2093
|
+
* Produces an ack callback to emit with an event.
|
|
2094
|
+
*
|
|
2095
|
+
* @param {Number} id - packet id
|
|
2096
|
+
* @private
|
|
2097
|
+
*/
|
|
2098
|
+
private ack;
|
|
2099
|
+
/**
|
|
2100
|
+
* Called upon ack packet.
|
|
2101
|
+
*
|
|
2102
|
+
* @private
|
|
2103
|
+
*/
|
|
2104
|
+
private onack;
|
|
2105
|
+
/**
|
|
2106
|
+
* Called upon client disconnect packet.
|
|
2107
|
+
*
|
|
2108
|
+
* @private
|
|
2109
|
+
*/
|
|
2110
|
+
private ondisconnect;
|
|
2111
|
+
/**
|
|
2112
|
+
* Handles a client error.
|
|
2113
|
+
*
|
|
2114
|
+
* @private
|
|
2115
|
+
*/
|
|
2116
|
+
_onerror(err: Error): void;
|
|
2117
|
+
/**
|
|
2118
|
+
* Called upon closing. Called by `Client`.
|
|
2119
|
+
*
|
|
2120
|
+
* @param {String} reason
|
|
2121
|
+
* @param description
|
|
2122
|
+
* @throw {Error} optional error object
|
|
2123
|
+
*
|
|
2124
|
+
* @private
|
|
2125
|
+
*/
|
|
2126
|
+
_onclose(reason: DisconnectReason, description?: any): this | undefined;
|
|
2127
|
+
/**
|
|
2128
|
+
* Makes the socket leave all the rooms it was part of and prevents it from joining any other room
|
|
2129
|
+
*
|
|
2130
|
+
* @private
|
|
2131
|
+
*/
|
|
2132
|
+
_cleanup(): void;
|
|
2133
|
+
/**
|
|
2134
|
+
* Produces an `error` packet.
|
|
2135
|
+
*
|
|
2136
|
+
* @param {Object} err - error object
|
|
2137
|
+
*
|
|
2138
|
+
* @private
|
|
2139
|
+
*/
|
|
2140
|
+
_error(err: any): void;
|
|
2141
|
+
/**
|
|
2142
|
+
* Disconnects this client.
|
|
2143
|
+
*
|
|
2144
|
+
* @example
|
|
2145
|
+
* io.on("connection", (socket) => {
|
|
2146
|
+
* // disconnect this socket (the connection might be kept alive for other namespaces)
|
|
2147
|
+
* socket.disconnect();
|
|
2148
|
+
*
|
|
2149
|
+
* // disconnect this socket and close the underlying connection
|
|
2150
|
+
* socket.disconnect(true);
|
|
2151
|
+
* })
|
|
2152
|
+
*
|
|
2153
|
+
* @param {Boolean} close - if `true`, closes the underlying connection
|
|
2154
|
+
* @return self
|
|
2155
|
+
*/
|
|
2156
|
+
disconnect(close?: boolean): this;
|
|
2157
|
+
/**
|
|
2158
|
+
* Sets the compress flag.
|
|
2159
|
+
*
|
|
2160
|
+
* @example
|
|
2161
|
+
* io.on("connection", (socket) => {
|
|
2162
|
+
* socket.compress(false).emit("hello");
|
|
2163
|
+
* });
|
|
2164
|
+
*
|
|
2165
|
+
* @param {Boolean} compress - if `true`, compresses the sending data
|
|
2166
|
+
* @return {Socket} self
|
|
2167
|
+
*/
|
|
2168
|
+
compress(compress: boolean): this;
|
|
2169
|
+
/**
|
|
2170
|
+
* Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
|
|
2171
|
+
* receive messages (because of network slowness or other issues, or because they’re connected through long polling
|
|
2172
|
+
* and is in the middle of a request-response cycle).
|
|
2173
|
+
*
|
|
2174
|
+
* @example
|
|
2175
|
+
* io.on("connection", (socket) => {
|
|
2176
|
+
* socket.volatile.emit("hello"); // the client may or may not receive it
|
|
2177
|
+
* });
|
|
2178
|
+
*
|
|
2179
|
+
* @return {Socket} self
|
|
2180
|
+
*/
|
|
2181
|
+
get volatile(): this;
|
|
2182
|
+
/**
|
|
2183
|
+
* Sets a modifier for a subsequent event emission that the event data will only be broadcast to every sockets but the
|
|
2184
|
+
* sender.
|
|
2185
|
+
*
|
|
2186
|
+
* @example
|
|
2187
|
+
* io.on("connection", (socket) => {
|
|
2188
|
+
* // the “foo” event will be broadcast to all connected clients, except this socket
|
|
2189
|
+
* socket.broadcast.emit("foo", "bar");
|
|
2190
|
+
* });
|
|
2191
|
+
*
|
|
2192
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
2193
|
+
*/
|
|
2194
|
+
get broadcast(): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
2195
|
+
/**
|
|
2196
|
+
* Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
|
|
2197
|
+
*
|
|
2198
|
+
* @example
|
|
2199
|
+
* io.on("connection", (socket) => {
|
|
2200
|
+
* // the “foo” event will be broadcast to all connected clients on this node, except this socket
|
|
2201
|
+
* socket.local.emit("foo", "bar");
|
|
2202
|
+
* });
|
|
2203
|
+
*
|
|
2204
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
2205
|
+
*/
|
|
2206
|
+
get local(): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
2207
|
+
/**
|
|
2208
|
+
* Sets a modifier for a subsequent event emission that the callback will be called with an error when the
|
|
2209
|
+
* given number of milliseconds have elapsed without an acknowledgement from the client:
|
|
2210
|
+
*
|
|
2211
|
+
* @example
|
|
2212
|
+
* io.on("connection", (socket) => {
|
|
2213
|
+
* socket.timeout(5000).emit("my-event", (err) => {
|
|
2214
|
+
* if (err) {
|
|
2215
|
+
* // the client did not acknowledge the event in the given delay
|
|
2216
|
+
* }
|
|
2217
|
+
* });
|
|
2218
|
+
* });
|
|
2219
|
+
*
|
|
2220
|
+
* @returns self
|
|
2221
|
+
*/
|
|
2222
|
+
timeout(timeout: number): Socket<ListenEvents, DecorateAcknowledgements<EmitEvents>, ServerSideEvents, SocketData>;
|
|
2223
|
+
/**
|
|
2224
|
+
* Dispatch incoming event to socket listeners.
|
|
2225
|
+
*
|
|
2226
|
+
* @param {Array} event - event that will get emitted
|
|
2227
|
+
* @private
|
|
2228
|
+
*/
|
|
2229
|
+
private dispatch;
|
|
2230
|
+
/**
|
|
2231
|
+
* Sets up socket middleware.
|
|
2232
|
+
*
|
|
2233
|
+
* @example
|
|
2234
|
+
* io.on("connection", (socket) => {
|
|
2235
|
+
* socket.use(([event, ...args], next) => {
|
|
2236
|
+
* if (isUnauthorized(event)) {
|
|
2237
|
+
* return next(new Error("unauthorized event"));
|
|
2238
|
+
* }
|
|
2239
|
+
* // do not forget to call next
|
|
2240
|
+
* next();
|
|
2241
|
+
* });
|
|
2242
|
+
*
|
|
2243
|
+
* socket.on("error", (err) => {
|
|
2244
|
+
* if (err && err.message === "unauthorized event") {
|
|
2245
|
+
* socket.disconnect();
|
|
2246
|
+
* }
|
|
2247
|
+
* });
|
|
2248
|
+
* });
|
|
2249
|
+
*
|
|
2250
|
+
* @param {Function} fn - middleware function (event, next)
|
|
2251
|
+
* @return {Socket} self
|
|
2252
|
+
*/
|
|
2253
|
+
use(fn: (event: Event, next: (err?: Error) => void) => void): this;
|
|
2254
|
+
/**
|
|
2255
|
+
* Executes the middleware for an incoming event.
|
|
2256
|
+
*
|
|
2257
|
+
* @param {Array} event - event that will get emitted
|
|
2258
|
+
* @param {Function} fn - last fn call in the middleware
|
|
2259
|
+
* @private
|
|
2260
|
+
*/
|
|
2261
|
+
private run;
|
|
2262
|
+
/**
|
|
2263
|
+
* Whether the socket is currently disconnected
|
|
2264
|
+
*/
|
|
2265
|
+
get disconnected(): boolean;
|
|
2266
|
+
/**
|
|
2267
|
+
* A reference to the request that originated the underlying Engine.IO Socket.
|
|
2268
|
+
*/
|
|
2269
|
+
get request(): IncomingMessage;
|
|
2270
|
+
/**
|
|
2271
|
+
* A reference to the underlying Client transport connection (Engine.IO Socket object).
|
|
2272
|
+
*
|
|
2273
|
+
* @example
|
|
2274
|
+
* io.on("connection", (socket) => {
|
|
2275
|
+
* console.log(socket.conn.transport.name); // prints "polling" or "websocket"
|
|
2276
|
+
*
|
|
2277
|
+
* socket.conn.once("upgrade", () => {
|
|
2278
|
+
* console.log(socket.conn.transport.name); // prints "websocket"
|
|
2279
|
+
* });
|
|
2280
|
+
* });
|
|
2281
|
+
*/
|
|
2282
|
+
get conn(): Socket$1;
|
|
2283
|
+
/**
|
|
2284
|
+
* Returns the rooms the socket is currently in.
|
|
2285
|
+
*
|
|
2286
|
+
* @example
|
|
2287
|
+
* io.on("connection", (socket) => {
|
|
2288
|
+
* console.log(socket.rooms); // Set { <socket.id> }
|
|
2289
|
+
*
|
|
2290
|
+
* socket.join("room1");
|
|
2291
|
+
*
|
|
2292
|
+
* console.log(socket.rooms); // Set { <socket.id>, "room1" }
|
|
2293
|
+
* });
|
|
2294
|
+
*/
|
|
2295
|
+
get rooms(): Set<Room>;
|
|
2296
|
+
/**
|
|
2297
|
+
* Adds a listener that will be fired when any event is received. The event name is passed as the first argument to
|
|
2298
|
+
* the callback.
|
|
2299
|
+
*
|
|
2300
|
+
* @example
|
|
2301
|
+
* io.on("connection", (socket) => {
|
|
2302
|
+
* socket.onAny((event, ...args) => {
|
|
2303
|
+
* console.log(`got event ${event}`);
|
|
2304
|
+
* });
|
|
2305
|
+
* });
|
|
2306
|
+
*
|
|
2307
|
+
* @param listener
|
|
2308
|
+
*/
|
|
2309
|
+
onAny(listener: (...args: any[]) => void): this;
|
|
2310
|
+
/**
|
|
2311
|
+
* Adds a listener that will be fired when any event is received. The event name is passed as the first argument to
|
|
2312
|
+
* the callback. The listener is added to the beginning of the listeners array.
|
|
2313
|
+
*
|
|
2314
|
+
* @param listener
|
|
2315
|
+
*/
|
|
2316
|
+
prependAny(listener: (...args: any[]) => void): this;
|
|
2317
|
+
/**
|
|
2318
|
+
* Removes the listener that will be fired when any event is received.
|
|
2319
|
+
*
|
|
2320
|
+
* @example
|
|
2321
|
+
* io.on("connection", (socket) => {
|
|
2322
|
+
* const catchAllListener = (event, ...args) => {
|
|
2323
|
+
* console.log(`got event ${event}`);
|
|
2324
|
+
* }
|
|
2325
|
+
*
|
|
2326
|
+
* socket.onAny(catchAllListener);
|
|
2327
|
+
*
|
|
2328
|
+
* // remove a specific listener
|
|
2329
|
+
* socket.offAny(catchAllListener);
|
|
2330
|
+
*
|
|
2331
|
+
* // or remove all listeners
|
|
2332
|
+
* socket.offAny();
|
|
2333
|
+
* });
|
|
2334
|
+
*
|
|
2335
|
+
* @param listener
|
|
2336
|
+
*/
|
|
2337
|
+
offAny(listener?: (...args: any[]) => void): this;
|
|
2338
|
+
/**
|
|
2339
|
+
* Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
|
|
2340
|
+
* e.g. to remove listeners.
|
|
2341
|
+
*/
|
|
2342
|
+
listenersAny(): ((...args: any[]) => void)[];
|
|
2343
|
+
/**
|
|
2344
|
+
* Adds a listener that will be fired when any event is sent. The event name is passed as the first argument to
|
|
2345
|
+
* the callback.
|
|
2346
|
+
*
|
|
2347
|
+
* Note: acknowledgements sent to the client are not included.
|
|
2348
|
+
*
|
|
2349
|
+
* @example
|
|
2350
|
+
* io.on("connection", (socket) => {
|
|
2351
|
+
* socket.onAnyOutgoing((event, ...args) => {
|
|
2352
|
+
* console.log(`sent event ${event}`);
|
|
2353
|
+
* });
|
|
2354
|
+
* });
|
|
2355
|
+
*
|
|
2356
|
+
* @param listener
|
|
2357
|
+
*/
|
|
2358
|
+
onAnyOutgoing(listener: (...args: any[]) => void): this;
|
|
2359
|
+
/**
|
|
2360
|
+
* Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
|
|
2361
|
+
* callback. The listener is added to the beginning of the listeners array.
|
|
2362
|
+
*
|
|
2363
|
+
* @example
|
|
2364
|
+
* io.on("connection", (socket) => {
|
|
2365
|
+
* socket.prependAnyOutgoing((event, ...args) => {
|
|
2366
|
+
* console.log(`sent event ${event}`);
|
|
2367
|
+
* });
|
|
2368
|
+
* });
|
|
2369
|
+
*
|
|
2370
|
+
* @param listener
|
|
2371
|
+
*/
|
|
2372
|
+
prependAnyOutgoing(listener: (...args: any[]) => void): this;
|
|
2373
|
+
/**
|
|
2374
|
+
* Removes the listener that will be fired when any event is sent.
|
|
2375
|
+
*
|
|
2376
|
+
* @example
|
|
2377
|
+
* io.on("connection", (socket) => {
|
|
2378
|
+
* const catchAllListener = (event, ...args) => {
|
|
2379
|
+
* console.log(`sent event ${event}`);
|
|
2380
|
+
* }
|
|
2381
|
+
*
|
|
2382
|
+
* socket.onAnyOutgoing(catchAllListener);
|
|
2383
|
+
*
|
|
2384
|
+
* // remove a specific listener
|
|
2385
|
+
* socket.offAnyOutgoing(catchAllListener);
|
|
2386
|
+
*
|
|
2387
|
+
* // or remove all listeners
|
|
2388
|
+
* socket.offAnyOutgoing();
|
|
2389
|
+
* });
|
|
2390
|
+
*
|
|
2391
|
+
* @param listener - the catch-all listener
|
|
2392
|
+
*/
|
|
2393
|
+
offAnyOutgoing(listener?: (...args: any[]) => void): this;
|
|
2394
|
+
/**
|
|
2395
|
+
* Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
|
|
2396
|
+
* e.g. to remove listeners.
|
|
2397
|
+
*/
|
|
2398
|
+
listenersAnyOutgoing(): ((...args: any[]) => void)[];
|
|
2399
|
+
/**
|
|
2400
|
+
* Notify the listeners for each packet sent (emit or broadcast)
|
|
2401
|
+
*
|
|
2402
|
+
* @param packet
|
|
2403
|
+
*
|
|
2404
|
+
* @private
|
|
2405
|
+
*/
|
|
2406
|
+
private notifyOutgoingListeners;
|
|
2407
|
+
private newBroadcastOperator;
|
|
2408
|
+
}
|
|
2409
|
+
|
|
2410
|
+
declare class BroadcastOperator<EmitEvents extends EventsMap$2, SocketData> implements TypedEventBroadcaster<EmitEvents> {
|
|
2411
|
+
private readonly adapter;
|
|
2412
|
+
private readonly rooms;
|
|
2413
|
+
private readonly exceptRooms;
|
|
2414
|
+
private readonly flags;
|
|
2415
|
+
constructor(adapter: Adapter, rooms?: Set<Room>, exceptRooms?: Set<Room>, flags?: BroadcastFlags & {
|
|
2416
|
+
expectSingleResponse?: boolean;
|
|
2417
|
+
});
|
|
2418
|
+
/**
|
|
2419
|
+
* Targets a room when emitting.
|
|
2420
|
+
*
|
|
2421
|
+
* @example
|
|
2422
|
+
* // the “foo” event will be broadcast to all connected clients in the “room-101” room
|
|
2423
|
+
* io.to("room-101").emit("foo", "bar");
|
|
2424
|
+
*
|
|
2425
|
+
* // with an array of rooms (a client will be notified at most once)
|
|
2426
|
+
* io.to(["room-101", "room-102"]).emit("foo", "bar");
|
|
2427
|
+
*
|
|
2428
|
+
* // with multiple chained calls
|
|
2429
|
+
* io.to("room-101").to("room-102").emit("foo", "bar");
|
|
2430
|
+
*
|
|
2431
|
+
* @param room - a room, or an array of rooms
|
|
2432
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
2433
|
+
*/
|
|
2434
|
+
to(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
|
|
2435
|
+
/**
|
|
2436
|
+
* Targets a room when emitting. Similar to `to()`, but might feel clearer in some cases:
|
|
2437
|
+
*
|
|
2438
|
+
* @example
|
|
2439
|
+
* // disconnect all clients in the "room-101" room
|
|
2440
|
+
* io.in("room-101").disconnectSockets();
|
|
2441
|
+
*
|
|
2442
|
+
* @param room - a room, or an array of rooms
|
|
2443
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
2444
|
+
*/
|
|
2445
|
+
in(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
|
|
2446
|
+
/**
|
|
2447
|
+
* Excludes a room when emitting.
|
|
2448
|
+
*
|
|
2449
|
+
* @example
|
|
2450
|
+
* // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
|
|
2451
|
+
* io.except("room-101").emit("foo", "bar");
|
|
2452
|
+
*
|
|
2453
|
+
* // with an array of rooms
|
|
2454
|
+
* io.except(["room-101", "room-102"]).emit("foo", "bar");
|
|
2455
|
+
*
|
|
2456
|
+
* // with multiple chained calls
|
|
2457
|
+
* io.except("room-101").except("room-102").emit("foo", "bar");
|
|
2458
|
+
*
|
|
2459
|
+
* @param room - a room, or an array of rooms
|
|
2460
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
2461
|
+
*/
|
|
2462
|
+
except(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
|
|
2463
|
+
/**
|
|
2464
|
+
* Sets the compress flag.
|
|
2465
|
+
*
|
|
2466
|
+
* @example
|
|
2467
|
+
* io.compress(false).emit("hello");
|
|
2468
|
+
*
|
|
2469
|
+
* @param compress - if `true`, compresses the sending data
|
|
2470
|
+
* @return a new BroadcastOperator instance
|
|
2471
|
+
*/
|
|
2472
|
+
compress(compress: boolean): BroadcastOperator<EmitEvents, SocketData>;
|
|
2473
|
+
/**
|
|
2474
|
+
* Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
|
|
2475
|
+
* receive messages (because of network slowness or other issues, or because they’re connected through long polling
|
|
2476
|
+
* and is in the middle of a request-response cycle).
|
|
2477
|
+
*
|
|
2478
|
+
* @example
|
|
2479
|
+
* io.volatile.emit("hello"); // the clients may or may not receive it
|
|
2480
|
+
*
|
|
2481
|
+
* @return a new BroadcastOperator instance
|
|
2482
|
+
*/
|
|
2483
|
+
get volatile(): BroadcastOperator<EmitEvents, SocketData>;
|
|
2484
|
+
/**
|
|
2485
|
+
* Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
|
|
2486
|
+
*
|
|
2487
|
+
* @example
|
|
2488
|
+
* // the “foo” event will be broadcast to all connected clients on this node
|
|
2489
|
+
* io.local.emit("foo", "bar");
|
|
2490
|
+
*
|
|
2491
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
2492
|
+
*/
|
|
2493
|
+
get local(): BroadcastOperator<EmitEvents, SocketData>;
|
|
2494
|
+
/**
|
|
2495
|
+
* Adds a timeout in milliseconds for the next operation
|
|
2496
|
+
*
|
|
2497
|
+
* @example
|
|
2498
|
+
* io.timeout(1000).emit("some-event", (err, responses) => {
|
|
2499
|
+
* if (err) {
|
|
2500
|
+
* // some clients did not acknowledge the event in the given delay
|
|
2501
|
+
* } else {
|
|
2502
|
+
* console.log(responses); // one response per client
|
|
2503
|
+
* }
|
|
2504
|
+
* });
|
|
2505
|
+
*
|
|
2506
|
+
* @param timeout
|
|
2507
|
+
*/
|
|
2508
|
+
timeout(timeout: number): BroadcastOperator<DecorateAcknowledgements<EmitEvents>, SocketData>;
|
|
2509
|
+
/**
|
|
2510
|
+
* Emits to all clients.
|
|
2511
|
+
*
|
|
2512
|
+
* @example
|
|
2513
|
+
* // the “foo” event will be broadcast to all connected clients
|
|
2514
|
+
* io.emit("foo", "bar");
|
|
2515
|
+
*
|
|
2516
|
+
* // the “foo” event will be broadcast to all connected clients in the “room-101” room
|
|
2517
|
+
* io.to("room-101").emit("foo", "bar");
|
|
2518
|
+
*
|
|
2519
|
+
* // with an acknowledgement expected from all connected clients
|
|
2520
|
+
* io.timeout(1000).emit("some-event", (err, responses) => {
|
|
2521
|
+
* if (err) {
|
|
2522
|
+
* // some clients did not acknowledge the event in the given delay
|
|
2523
|
+
* } else {
|
|
2524
|
+
* console.log(responses); // one response per client
|
|
2525
|
+
* }
|
|
2526
|
+
* });
|
|
2527
|
+
*
|
|
2528
|
+
* @return Always true
|
|
2529
|
+
*/
|
|
2530
|
+
emit<Ev extends EventNames$1<EmitEvents>>(ev: Ev, ...args: EventParams$1<EmitEvents, Ev>): boolean;
|
|
2531
|
+
/**
|
|
2532
|
+
* Emits an event and waits for an acknowledgement from all clients.
|
|
2533
|
+
*
|
|
2534
|
+
* @example
|
|
2535
|
+
* try {
|
|
2536
|
+
* const responses = await io.timeout(1000).emitWithAck("some-event");
|
|
2537
|
+
* console.log(responses); // one response per client
|
|
2538
|
+
* } catch (e) {
|
|
2539
|
+
* // some clients did not acknowledge the event in the given delay
|
|
2540
|
+
* }
|
|
2541
|
+
*
|
|
2542
|
+
* @return a Promise that will be fulfilled when all clients have acknowledged the event
|
|
2543
|
+
*/
|
|
2544
|
+
emitWithAck<Ev extends EventNamesWithError<EmitEvents>>(ev: Ev, ...args: AllButLast<EventParams$1<EmitEvents, Ev>>): Promise<FirstNonErrorArg<Last<EventParams$1<EmitEvents, Ev>>>>;
|
|
2545
|
+
/**
|
|
2546
|
+
* Gets a list of clients.
|
|
2547
|
+
*
|
|
2548
|
+
* @deprecated this method will be removed in the next major release, please use {@link Server#serverSideEmit} or
|
|
2549
|
+
* {@link fetchSockets} instead.
|
|
2550
|
+
*/
|
|
2551
|
+
allSockets(): Promise<Set<SocketId>>;
|
|
2552
|
+
/**
|
|
2553
|
+
* Returns the matching socket instances. This method works across a cluster of several Socket.IO servers.
|
|
2554
|
+
*
|
|
2555
|
+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
|
2556
|
+
*
|
|
2557
|
+
* @example
|
|
2558
|
+
* // return all Socket instances
|
|
2559
|
+
* const sockets = await io.fetchSockets();
|
|
2560
|
+
*
|
|
2561
|
+
* // return all Socket instances in the "room1" room
|
|
2562
|
+
* const sockets = await io.in("room1").fetchSockets();
|
|
2563
|
+
*
|
|
2564
|
+
* for (const socket of sockets) {
|
|
2565
|
+
* console.log(socket.id);
|
|
2566
|
+
* console.log(socket.handshake);
|
|
2567
|
+
* console.log(socket.rooms);
|
|
2568
|
+
* console.log(socket.data);
|
|
2569
|
+
*
|
|
2570
|
+
* socket.emit("hello");
|
|
2571
|
+
* socket.join("room1");
|
|
2572
|
+
* socket.leave("room2");
|
|
2573
|
+
* socket.disconnect();
|
|
2574
|
+
* }
|
|
2575
|
+
*/
|
|
2576
|
+
fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]>;
|
|
2577
|
+
/**
|
|
2578
|
+
* Makes the matching socket instances join the specified rooms.
|
|
2579
|
+
*
|
|
2580
|
+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
|
2581
|
+
*
|
|
2582
|
+
* @example
|
|
2583
|
+
*
|
|
2584
|
+
* // make all socket instances join the "room1" room
|
|
2585
|
+
* io.socketsJoin("room1");
|
|
2586
|
+
*
|
|
2587
|
+
* // make all socket instances in the "room1" room join the "room2" and "room3" rooms
|
|
2588
|
+
* io.in("room1").socketsJoin(["room2", "room3"]);
|
|
2589
|
+
*
|
|
2590
|
+
* @param room - a room, or an array of rooms
|
|
2591
|
+
*/
|
|
2592
|
+
socketsJoin(room: Room | Room[]): void;
|
|
2593
|
+
/**
|
|
2594
|
+
* Makes the matching socket instances leave the specified rooms.
|
|
2595
|
+
*
|
|
2596
|
+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
|
2597
|
+
*
|
|
2598
|
+
* @example
|
|
2599
|
+
* // make all socket instances leave the "room1" room
|
|
2600
|
+
* io.socketsLeave("room1");
|
|
2601
|
+
*
|
|
2602
|
+
* // make all socket instances in the "room1" room leave the "room2" and "room3" rooms
|
|
2603
|
+
* io.in("room1").socketsLeave(["room2", "room3"]);
|
|
2604
|
+
*
|
|
2605
|
+
* @param room - a room, or an array of rooms
|
|
2606
|
+
*/
|
|
2607
|
+
socketsLeave(room: Room | Room[]): void;
|
|
2608
|
+
/**
|
|
2609
|
+
* Makes the matching socket instances disconnect.
|
|
2610
|
+
*
|
|
2611
|
+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
|
2612
|
+
*
|
|
2613
|
+
* @example
|
|
2614
|
+
* // make all socket instances disconnect (the connections might be kept alive for other namespaces)
|
|
2615
|
+
* io.disconnectSockets();
|
|
2616
|
+
*
|
|
2617
|
+
* // make all socket instances in the "room1" room disconnect and close the underlying connections
|
|
2618
|
+
* io.in("room1").disconnectSockets(true);
|
|
2619
|
+
*
|
|
2620
|
+
* @param close - whether to close the underlying connection
|
|
2621
|
+
*/
|
|
2622
|
+
disconnectSockets(close?: boolean): void;
|
|
2623
|
+
}
|
|
2624
|
+
/**
|
|
2625
|
+
* Format of the data when the Socket instance exists on another Socket.IO server
|
|
2626
|
+
*/
|
|
2627
|
+
interface SocketDetails<SocketData> {
|
|
2628
|
+
id: SocketId;
|
|
2629
|
+
handshake: Handshake;
|
|
2630
|
+
rooms: Room[];
|
|
2631
|
+
data: SocketData;
|
|
2632
|
+
}
|
|
2633
|
+
/**
|
|
2634
|
+
* Expose of subset of the attributes and methods of the Socket class
|
|
2635
|
+
*/
|
|
2636
|
+
declare class RemoteSocket<EmitEvents extends EventsMap$2, SocketData> implements TypedEventBroadcaster<EmitEvents> {
|
|
2637
|
+
readonly id: SocketId;
|
|
2638
|
+
readonly handshake: Handshake;
|
|
2639
|
+
readonly rooms: Set<Room>;
|
|
2640
|
+
readonly data: SocketData;
|
|
2641
|
+
private readonly operator;
|
|
2642
|
+
constructor(adapter: Adapter, details: SocketDetails<SocketData>);
|
|
2643
|
+
/**
|
|
2644
|
+
* Adds a timeout in milliseconds for the next operation.
|
|
2645
|
+
*
|
|
2646
|
+
* @example
|
|
2647
|
+
* const sockets = await io.fetchSockets();
|
|
2648
|
+
*
|
|
2649
|
+
* for (const socket of sockets) {
|
|
2650
|
+
* if (someCondition) {
|
|
2651
|
+
* socket.timeout(1000).emit("some-event", (err) => {
|
|
2652
|
+
* if (err) {
|
|
2653
|
+
* // the client did not acknowledge the event in the given delay
|
|
2654
|
+
* }
|
|
2655
|
+
* });
|
|
2656
|
+
* }
|
|
2657
|
+
* }
|
|
2658
|
+
*
|
|
2659
|
+
* // note: if possible, using a room instead of looping over all sockets is preferable
|
|
2660
|
+
* io.timeout(1000).to(someConditionRoom).emit("some-event", (err, responses) => {
|
|
2661
|
+
* // ...
|
|
2662
|
+
* });
|
|
2663
|
+
*
|
|
2664
|
+
* @param timeout
|
|
2665
|
+
*/
|
|
2666
|
+
timeout(timeout: number): BroadcastOperator<DecorateAcknowledgements<EmitEvents>, SocketData>;
|
|
2667
|
+
emit<Ev extends EventNames$1<EmitEvents>>(ev: Ev, ...args: EventParams$1<EmitEvents, Ev>): boolean;
|
|
2668
|
+
/**
|
|
2669
|
+
* Joins a room.
|
|
2670
|
+
*
|
|
2671
|
+
* @param {String|Array} room - room or array of rooms
|
|
2672
|
+
*/
|
|
2673
|
+
join(room: Room | Room[]): void;
|
|
2674
|
+
/**
|
|
2675
|
+
* Leaves a room.
|
|
2676
|
+
*
|
|
2677
|
+
* @param {String} room
|
|
2678
|
+
*/
|
|
2679
|
+
leave(room: Room): void;
|
|
2680
|
+
/**
|
|
2681
|
+
* Disconnects this client.
|
|
2682
|
+
*
|
|
2683
|
+
* @param {Boolean} close - if `true`, closes the underlying connection
|
|
2684
|
+
* @return {Socket} self
|
|
2685
|
+
*/
|
|
2686
|
+
disconnect(close?: boolean): this;
|
|
2687
|
+
}
|
|
2688
|
+
|
|
2689
|
+
interface ExtendedError$1 extends Error {
|
|
2690
|
+
data?: any;
|
|
2691
|
+
}
|
|
2692
|
+
interface NamespaceReservedEventsMap<ListenEvents extends EventsMap$2, EmitEvents extends EventsMap$2, ServerSideEvents extends EventsMap$2, SocketData> {
|
|
2693
|
+
connect: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void;
|
|
2694
|
+
connection: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void;
|
|
2695
|
+
}
|
|
2696
|
+
interface ServerReservedEventsMap<ListenEvents extends EventsMap$2, EmitEvents extends EventsMap$2, ServerSideEvents extends EventsMap$2, SocketData> extends NamespaceReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents, SocketData> {
|
|
2697
|
+
new_namespace: (namespace: Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void;
|
|
2698
|
+
}
|
|
2699
|
+
/**
|
|
2700
|
+
* A Namespace is a communication channel that allows you to split the logic of your application over a single shared
|
|
2701
|
+
* connection.
|
|
2702
|
+
*
|
|
2703
|
+
* Each namespace has its own:
|
|
2704
|
+
*
|
|
2705
|
+
* - event handlers
|
|
2706
|
+
*
|
|
2707
|
+
* ```
|
|
2708
|
+
* io.of("/orders").on("connection", (socket) => {
|
|
2709
|
+
* socket.on("order:list", () => {});
|
|
2710
|
+
* socket.on("order:create", () => {});
|
|
2711
|
+
* });
|
|
2712
|
+
*
|
|
2713
|
+
* io.of("/users").on("connection", (socket) => {
|
|
2714
|
+
* socket.on("user:list", () => {});
|
|
2715
|
+
* });
|
|
2716
|
+
* ```
|
|
2717
|
+
*
|
|
2718
|
+
* - rooms
|
|
2719
|
+
*
|
|
2720
|
+
* ```
|
|
2721
|
+
* const orderNamespace = io.of("/orders");
|
|
2722
|
+
*
|
|
2723
|
+
* orderNamespace.on("connection", (socket) => {
|
|
2724
|
+
* socket.join("room1");
|
|
2725
|
+
* orderNamespace.to("room1").emit("hello");
|
|
2726
|
+
* });
|
|
2727
|
+
*
|
|
2728
|
+
* const userNamespace = io.of("/users");
|
|
2729
|
+
*
|
|
2730
|
+
* userNamespace.on("connection", (socket) => {
|
|
2731
|
+
* socket.join("room1"); // distinct from the room in the "orders" namespace
|
|
2732
|
+
* userNamespace.to("room1").emit("holà");
|
|
2733
|
+
* });
|
|
2734
|
+
* ```
|
|
2735
|
+
*
|
|
2736
|
+
* - middlewares
|
|
2737
|
+
*
|
|
2738
|
+
* ```
|
|
2739
|
+
* const orderNamespace = io.of("/orders");
|
|
2740
|
+
*
|
|
2741
|
+
* orderNamespace.use((socket, next) => {
|
|
2742
|
+
* // ensure the socket has access to the "orders" namespace
|
|
2743
|
+
* });
|
|
2744
|
+
*
|
|
2745
|
+
* const userNamespace = io.of("/users");
|
|
2746
|
+
*
|
|
2747
|
+
* userNamespace.use((socket, next) => {
|
|
2748
|
+
* // ensure the socket has access to the "users" namespace
|
|
2749
|
+
* });
|
|
2750
|
+
* ```
|
|
2751
|
+
*/
|
|
2752
|
+
declare class Namespace<ListenEvents extends EventsMap$2 = DefaultEventsMap$1, EmitEvents extends EventsMap$2 = ListenEvents, ServerSideEvents extends EventsMap$2 = DefaultEventsMap$1, SocketData = any> extends StrictEventEmitter<ServerSideEvents, RemoveAcknowledgements<EmitEvents>, NamespaceReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents, SocketData>> {
|
|
2753
|
+
readonly name: string;
|
|
2754
|
+
readonly sockets: Map<SocketId, Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>>;
|
|
2755
|
+
adapter: Adapter;
|
|
2756
|
+
/** @private */
|
|
2757
|
+
readonly server: Server$1<ListenEvents, EmitEvents, ServerSideEvents, SocketData>;
|
|
2758
|
+
/** @private */
|
|
2759
|
+
_fns: Array<(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, next: (err?: ExtendedError$1) => void) => void>;
|
|
2760
|
+
/** @private */
|
|
2761
|
+
_ids: number;
|
|
2762
|
+
/**
|
|
2763
|
+
* Namespace constructor.
|
|
2764
|
+
*
|
|
2765
|
+
* @param server instance
|
|
2766
|
+
* @param name
|
|
2767
|
+
*/
|
|
2768
|
+
constructor(server: Server$1<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, name: string);
|
|
2769
|
+
/**
|
|
2770
|
+
* Initializes the `Adapter` for this nsp.
|
|
2771
|
+
* Run upon changing adapter by `Server#adapter`
|
|
2772
|
+
* in addition to the constructor.
|
|
2773
|
+
*
|
|
2774
|
+
* @private
|
|
2775
|
+
*/
|
|
2776
|
+
_initAdapter(): void;
|
|
2777
|
+
/**
|
|
2778
|
+
* Registers a middleware, which is a function that gets executed for every incoming {@link Socket}.
|
|
2779
|
+
*
|
|
2780
|
+
* @example
|
|
2781
|
+
* const myNamespace = io.of("/my-namespace");
|
|
2782
|
+
*
|
|
2783
|
+
* myNamespace.use((socket, next) => {
|
|
2784
|
+
* // ...
|
|
2785
|
+
* next();
|
|
2786
|
+
* });
|
|
2787
|
+
*
|
|
2788
|
+
* @param fn - the middleware function
|
|
2789
|
+
*/
|
|
2790
|
+
use(fn: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, next: (err?: ExtendedError$1) => void) => void): this;
|
|
2791
|
+
/**
|
|
2792
|
+
* Executes the middleware for an incoming client.
|
|
2793
|
+
*
|
|
2794
|
+
* @param socket - the socket that will get added
|
|
2795
|
+
* @param fn - last fn call in the middleware
|
|
2796
|
+
* @private
|
|
2797
|
+
*/
|
|
2798
|
+
private run;
|
|
2799
|
+
/**
|
|
2800
|
+
* Targets a room when emitting.
|
|
2801
|
+
*
|
|
2802
|
+
* @example
|
|
2803
|
+
* const myNamespace = io.of("/my-namespace");
|
|
2804
|
+
*
|
|
2805
|
+
* // the “foo” event will be broadcast to all connected clients in the “room-101” room
|
|
2806
|
+
* myNamespace.to("room-101").emit("foo", "bar");
|
|
2807
|
+
*
|
|
2808
|
+
* // with an array of rooms (a client will be notified at most once)
|
|
2809
|
+
* myNamespace.to(["room-101", "room-102"]).emit("foo", "bar");
|
|
2810
|
+
*
|
|
2811
|
+
* // with multiple chained calls
|
|
2812
|
+
* myNamespace.to("room-101").to("room-102").emit("foo", "bar");
|
|
2813
|
+
*
|
|
2814
|
+
* @param room - a room, or an array of rooms
|
|
2815
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
2816
|
+
*/
|
|
2817
|
+
to(room: Room | Room[]): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
2818
|
+
/**
|
|
2819
|
+
* Targets a room when emitting. Similar to `to()`, but might feel clearer in some cases:
|
|
2820
|
+
*
|
|
2821
|
+
* @example
|
|
2822
|
+
* const myNamespace = io.of("/my-namespace");
|
|
2823
|
+
*
|
|
2824
|
+
* // disconnect all clients in the "room-101" room
|
|
2825
|
+
* myNamespace.in("room-101").disconnectSockets();
|
|
2826
|
+
*
|
|
2827
|
+
* @param room - a room, or an array of rooms
|
|
2828
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
2829
|
+
*/
|
|
2830
|
+
in(room: Room | Room[]): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
2831
|
+
/**
|
|
2832
|
+
* Excludes a room when emitting.
|
|
2833
|
+
*
|
|
2834
|
+
* @example
|
|
2835
|
+
* const myNamespace = io.of("/my-namespace");
|
|
2836
|
+
*
|
|
2837
|
+
* // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
|
|
2838
|
+
* myNamespace.except("room-101").emit("foo", "bar");
|
|
2839
|
+
*
|
|
2840
|
+
* // with an array of rooms
|
|
2841
|
+
* myNamespace.except(["room-101", "room-102"]).emit("foo", "bar");
|
|
2842
|
+
*
|
|
2843
|
+
* // with multiple chained calls
|
|
2844
|
+
* myNamespace.except("room-101").except("room-102").emit("foo", "bar");
|
|
2845
|
+
*
|
|
2846
|
+
* @param room - a room, or an array of rooms
|
|
2847
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
2848
|
+
*/
|
|
2849
|
+
except(room: Room | Room[]): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
2850
|
+
/**
|
|
2851
|
+
* Adds a new client.
|
|
2852
|
+
*
|
|
2853
|
+
* @return {Socket}
|
|
2854
|
+
* @private
|
|
2855
|
+
*/
|
|
2856
|
+
_add(client: Client<ListenEvents, EmitEvents, ServerSideEvents>, auth: Record<string, unknown>, fn: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void): any;
|
|
2857
|
+
private _createSocket;
|
|
2858
|
+
private _doConnect;
|
|
2859
|
+
/**
|
|
2860
|
+
* Removes a client. Called by each `Socket`.
|
|
2861
|
+
*
|
|
2862
|
+
* @private
|
|
2863
|
+
*/
|
|
2864
|
+
_remove(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>): void;
|
|
2865
|
+
/**
|
|
2866
|
+
* Emits to all connected clients.
|
|
2867
|
+
*
|
|
2868
|
+
* @example
|
|
2869
|
+
* const myNamespace = io.of("/my-namespace");
|
|
2870
|
+
*
|
|
2871
|
+
* myNamespace.emit("hello", "world");
|
|
2872
|
+
*
|
|
2873
|
+
* // all serializable datastructures are supported (no need to call JSON.stringify)
|
|
2874
|
+
* myNamespace.emit("hello", 1, "2", { 3: ["4"], 5: Uint8Array.from([6]) });
|
|
2875
|
+
*
|
|
2876
|
+
* // with an acknowledgement from the clients
|
|
2877
|
+
* myNamespace.timeout(1000).emit("some-event", (err, responses) => {
|
|
2878
|
+
* if (err) {
|
|
2879
|
+
* // some clients did not acknowledge the event in the given delay
|
|
2880
|
+
* } else {
|
|
2881
|
+
* console.log(responses); // one response per client
|
|
2882
|
+
* }
|
|
2883
|
+
* });
|
|
2884
|
+
*
|
|
2885
|
+
* @return Always true
|
|
2886
|
+
*/
|
|
2887
|
+
emit<Ev extends EventNamesWithoutAck<EmitEvents>>(ev: Ev, ...args: EventParams$1<EmitEvents, Ev>): boolean;
|
|
2888
|
+
/**
|
|
2889
|
+
* Sends a `message` event to all clients.
|
|
2890
|
+
*
|
|
2891
|
+
* This method mimics the WebSocket.send() method.
|
|
2892
|
+
*
|
|
2893
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
|
|
2894
|
+
*
|
|
2895
|
+
* @example
|
|
2896
|
+
* const myNamespace = io.of("/my-namespace");
|
|
2897
|
+
*
|
|
2898
|
+
* myNamespace.send("hello");
|
|
2899
|
+
*
|
|
2900
|
+
* // this is equivalent to
|
|
2901
|
+
* myNamespace.emit("message", "hello");
|
|
2902
|
+
*
|
|
2903
|
+
* @return self
|
|
2904
|
+
*/
|
|
2905
|
+
send(...args: EventParams$1<EmitEvents, "message">): this;
|
|
2906
|
+
/**
|
|
2907
|
+
* Sends a `message` event to all clients. Sends a `message` event. Alias of {@link send}.
|
|
2908
|
+
*
|
|
2909
|
+
* @return self
|
|
2910
|
+
*/
|
|
2911
|
+
write(...args: EventParams$1<EmitEvents, "message">): this;
|
|
2912
|
+
/**
|
|
2913
|
+
* Sends a message to the other Socket.IO servers of the cluster.
|
|
2914
|
+
*
|
|
2915
|
+
* @example
|
|
2916
|
+
* const myNamespace = io.of("/my-namespace");
|
|
2917
|
+
*
|
|
2918
|
+
* myNamespace.serverSideEmit("hello", "world");
|
|
2919
|
+
*
|
|
2920
|
+
* myNamespace.on("hello", (arg1) => {
|
|
2921
|
+
* console.log(arg1); // prints "world"
|
|
2922
|
+
* });
|
|
2923
|
+
*
|
|
2924
|
+
* // acknowledgements (without binary content) are supported too:
|
|
2925
|
+
* myNamespace.serverSideEmit("ping", (err, responses) => {
|
|
2926
|
+
* if (err) {
|
|
2927
|
+
* // some servers did not acknowledge the event in the given delay
|
|
2928
|
+
* } else {
|
|
2929
|
+
* console.log(responses); // one response per server (except the current one)
|
|
2930
|
+
* }
|
|
2931
|
+
* });
|
|
2932
|
+
*
|
|
2933
|
+
* myNamespace.on("ping", (cb) => {
|
|
2934
|
+
* cb("pong");
|
|
2935
|
+
* });
|
|
2936
|
+
*
|
|
2937
|
+
* @param ev - the event name
|
|
2938
|
+
* @param args - an array of arguments, which may include an acknowledgement callback at the end
|
|
2939
|
+
*/
|
|
2940
|
+
serverSideEmit<Ev extends EventNames$1<ServerSideEvents>>(ev: Ev, ...args: EventParams$1<DecorateAcknowledgementsWithTimeoutAndMultipleResponses<ServerSideEvents>, Ev>): boolean;
|
|
2941
|
+
/**
|
|
2942
|
+
* Sends a message and expect an acknowledgement from the other Socket.IO servers of the cluster.
|
|
2943
|
+
*
|
|
2944
|
+
* @example
|
|
2945
|
+
* const myNamespace = io.of("/my-namespace");
|
|
2946
|
+
*
|
|
2947
|
+
* try {
|
|
2948
|
+
* const responses = await myNamespace.serverSideEmitWithAck("ping");
|
|
2949
|
+
* console.log(responses); // one response per server (except the current one)
|
|
2950
|
+
* } catch (e) {
|
|
2951
|
+
* // some servers did not acknowledge the event in the given delay
|
|
2952
|
+
* }
|
|
2953
|
+
*
|
|
2954
|
+
* @param ev - the event name
|
|
2955
|
+
* @param args - an array of arguments
|
|
2956
|
+
*
|
|
2957
|
+
* @return a Promise that will be fulfilled when all servers have acknowledged the event
|
|
2958
|
+
*/
|
|
2959
|
+
serverSideEmitWithAck<Ev extends EventNamesWithAck<ServerSideEvents>>(ev: Ev, ...args: AllButLast<EventParams$1<ServerSideEvents, Ev>>): Promise<FirstNonErrorArg<Last<EventParams$1<ServerSideEvents, Ev>>>[]>;
|
|
2960
|
+
/**
|
|
2961
|
+
* Called when a packet is received from another Socket.IO server
|
|
2962
|
+
*
|
|
2963
|
+
* @param args - an array of arguments, which may include an acknowledgement callback at the end
|
|
2964
|
+
*
|
|
2965
|
+
* @private
|
|
2966
|
+
*/
|
|
2967
|
+
_onServerSideEmit(args: [string, ...any[]]): void;
|
|
2968
|
+
/**
|
|
2969
|
+
* Gets a list of clients.
|
|
2970
|
+
*
|
|
2971
|
+
* @deprecated this method will be removed in the next major release, please use {@link Namespace#serverSideEmit} or
|
|
2972
|
+
* {@link Namespace#fetchSockets} instead.
|
|
2973
|
+
*/
|
|
2974
|
+
allSockets(): Promise<Set<SocketId>>;
|
|
2975
|
+
/**
|
|
2976
|
+
* Sets the compress flag.
|
|
2977
|
+
*
|
|
2978
|
+
* @example
|
|
2979
|
+
* const myNamespace = io.of("/my-namespace");
|
|
2980
|
+
*
|
|
2981
|
+
* myNamespace.compress(false).emit("hello");
|
|
2982
|
+
*
|
|
2983
|
+
* @param compress - if `true`, compresses the sending data
|
|
2984
|
+
* @return self
|
|
2985
|
+
*/
|
|
2986
|
+
compress(compress: boolean): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
2987
|
+
/**
|
|
2988
|
+
* Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
|
|
2989
|
+
* receive messages (because of network slowness or other issues, or because they’re connected through long polling
|
|
2990
|
+
* and is in the middle of a request-response cycle).
|
|
2991
|
+
*
|
|
2992
|
+
* @example
|
|
2993
|
+
* const myNamespace = io.of("/my-namespace");
|
|
2994
|
+
*
|
|
2995
|
+
* myNamespace.volatile.emit("hello"); // the clients may or may not receive it
|
|
2996
|
+
*
|
|
2997
|
+
* @return self
|
|
2998
|
+
*/
|
|
2999
|
+
get volatile(): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
3000
|
+
/**
|
|
3001
|
+
* Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
|
|
3002
|
+
*
|
|
3003
|
+
* @example
|
|
3004
|
+
* const myNamespace = io.of("/my-namespace");
|
|
3005
|
+
*
|
|
3006
|
+
* // the “foo” event will be broadcast to all connected clients on this node
|
|
3007
|
+
* myNamespace.local.emit("foo", "bar");
|
|
3008
|
+
*
|
|
3009
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
3010
|
+
*/
|
|
3011
|
+
get local(): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
3012
|
+
/**
|
|
3013
|
+
* Adds a timeout in milliseconds for the next operation.
|
|
3014
|
+
*
|
|
3015
|
+
* @example
|
|
3016
|
+
* const myNamespace = io.of("/my-namespace");
|
|
3017
|
+
*
|
|
3018
|
+
* myNamespace.timeout(1000).emit("some-event", (err, responses) => {
|
|
3019
|
+
* if (err) {
|
|
3020
|
+
* // some clients did not acknowledge the event in the given delay
|
|
3021
|
+
* } else {
|
|
3022
|
+
* console.log(responses); // one response per client
|
|
3023
|
+
* }
|
|
3024
|
+
* });
|
|
3025
|
+
*
|
|
3026
|
+
* @param timeout
|
|
3027
|
+
*/
|
|
3028
|
+
timeout(timeout: number): BroadcastOperator<DecorateAcknowledgements<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>>, SocketData>;
|
|
3029
|
+
/**
|
|
3030
|
+
* Returns the matching socket instances.
|
|
3031
|
+
*
|
|
3032
|
+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
|
3033
|
+
*
|
|
3034
|
+
* @example
|
|
3035
|
+
* const myNamespace = io.of("/my-namespace");
|
|
3036
|
+
*
|
|
3037
|
+
* // return all Socket instances
|
|
3038
|
+
* const sockets = await myNamespace.fetchSockets();
|
|
3039
|
+
*
|
|
3040
|
+
* // return all Socket instances in the "room1" room
|
|
3041
|
+
* const sockets = await myNamespace.in("room1").fetchSockets();
|
|
3042
|
+
*
|
|
3043
|
+
* for (const socket of sockets) {
|
|
3044
|
+
* console.log(socket.id);
|
|
3045
|
+
* console.log(socket.handshake);
|
|
3046
|
+
* console.log(socket.rooms);
|
|
3047
|
+
* console.log(socket.data);
|
|
3048
|
+
*
|
|
3049
|
+
* socket.emit("hello");
|
|
3050
|
+
* socket.join("room1");
|
|
3051
|
+
* socket.leave("room2");
|
|
3052
|
+
* socket.disconnect();
|
|
3053
|
+
* }
|
|
3054
|
+
*/
|
|
3055
|
+
fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]>;
|
|
3056
|
+
/**
|
|
3057
|
+
* Makes the matching socket instances join the specified rooms.
|
|
3058
|
+
*
|
|
3059
|
+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
|
3060
|
+
*
|
|
3061
|
+
* @example
|
|
3062
|
+
* const myNamespace = io.of("/my-namespace");
|
|
3063
|
+
*
|
|
3064
|
+
* // make all socket instances join the "room1" room
|
|
3065
|
+
* myNamespace.socketsJoin("room1");
|
|
3066
|
+
*
|
|
3067
|
+
* // make all socket instances in the "room1" room join the "room2" and "room3" rooms
|
|
3068
|
+
* myNamespace.in("room1").socketsJoin(["room2", "room3"]);
|
|
3069
|
+
*
|
|
3070
|
+
* @param room - a room, or an array of rooms
|
|
3071
|
+
*/
|
|
3072
|
+
socketsJoin(room: Room | Room[]): void;
|
|
3073
|
+
/**
|
|
3074
|
+
* Makes the matching socket instances leave the specified rooms.
|
|
3075
|
+
*
|
|
3076
|
+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
|
3077
|
+
*
|
|
3078
|
+
* @example
|
|
3079
|
+
* const myNamespace = io.of("/my-namespace");
|
|
3080
|
+
*
|
|
3081
|
+
* // make all socket instances leave the "room1" room
|
|
3082
|
+
* myNamespace.socketsLeave("room1");
|
|
3083
|
+
*
|
|
3084
|
+
* // make all socket instances in the "room1" room leave the "room2" and "room3" rooms
|
|
3085
|
+
* myNamespace.in("room1").socketsLeave(["room2", "room3"]);
|
|
3086
|
+
*
|
|
3087
|
+
* @param room - a room, or an array of rooms
|
|
3088
|
+
*/
|
|
3089
|
+
socketsLeave(room: Room | Room[]): void;
|
|
3090
|
+
/**
|
|
3091
|
+
* Makes the matching socket instances disconnect.
|
|
3092
|
+
*
|
|
3093
|
+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
|
3094
|
+
*
|
|
3095
|
+
* @example
|
|
3096
|
+
* const myNamespace = io.of("/my-namespace");
|
|
3097
|
+
*
|
|
3098
|
+
* // make all socket instances disconnect (the connections might be kept alive for other namespaces)
|
|
3099
|
+
* myNamespace.disconnectSockets();
|
|
3100
|
+
*
|
|
3101
|
+
* // make all socket instances in the "room1" room disconnect and close the underlying connections
|
|
3102
|
+
* myNamespace.in("room1").disconnectSockets(true);
|
|
3103
|
+
*
|
|
3104
|
+
* @param close - whether to close the underlying connection
|
|
3105
|
+
*/
|
|
3106
|
+
disconnectSockets(close?: boolean): void;
|
|
3107
|
+
}
|
|
3108
|
+
|
|
3109
|
+
declare type ParentNspNameMatchFn = (name: string, auth: {
|
|
3110
|
+
[key: string]: any;
|
|
3111
|
+
}, fn: (err: Error | null, success: boolean) => void) => void;
|
|
3112
|
+
declare type AdapterConstructor = typeof Adapter | ((nsp: Namespace) => Adapter);
|
|
3113
|
+
declare type TServerInstance = http.Server | Server$2 | Http2SecureServer | Http2Server;
|
|
3114
|
+
interface ServerOptions extends ServerOptions$2, AttachOptions {
|
|
3115
|
+
/**
|
|
3116
|
+
* name of the path to capture
|
|
3117
|
+
* @default "/socket.io"
|
|
3118
|
+
*/
|
|
3119
|
+
path: string;
|
|
3120
|
+
/**
|
|
3121
|
+
* whether to serve the client files
|
|
3122
|
+
* @default true
|
|
3123
|
+
*/
|
|
3124
|
+
serveClient: boolean;
|
|
3125
|
+
/**
|
|
3126
|
+
* the adapter to use
|
|
3127
|
+
* @default the in-memory adapter (https://github.com/socketio/socket.io-adapter)
|
|
3128
|
+
*/
|
|
3129
|
+
adapter: AdapterConstructor;
|
|
3130
|
+
/**
|
|
3131
|
+
* the parser to use
|
|
3132
|
+
* @default the default parser (https://github.com/socketio/socket.io-parser)
|
|
3133
|
+
*/
|
|
3134
|
+
parser: any;
|
|
3135
|
+
/**
|
|
3136
|
+
* how many ms before a client without namespace is closed
|
|
3137
|
+
* @default 45000
|
|
3138
|
+
*/
|
|
3139
|
+
connectTimeout: number;
|
|
3140
|
+
/**
|
|
3141
|
+
* Whether to enable the recovery of connection state when a client temporarily disconnects.
|
|
3142
|
+
*
|
|
3143
|
+
* The connection state includes the missed packets, the rooms the socket was in and the `data` attribute.
|
|
3144
|
+
*/
|
|
3145
|
+
connectionStateRecovery: {
|
|
3146
|
+
/**
|
|
3147
|
+
* The backup duration of the sessions and the packets.
|
|
3148
|
+
*
|
|
3149
|
+
* @default 120000 (2 minutes)
|
|
3150
|
+
*/
|
|
3151
|
+
maxDisconnectionDuration?: number;
|
|
3152
|
+
/**
|
|
3153
|
+
* Whether to skip middlewares upon successful connection state recovery.
|
|
3154
|
+
*
|
|
3155
|
+
* @default true
|
|
3156
|
+
*/
|
|
3157
|
+
skipMiddlewares?: boolean;
|
|
3158
|
+
};
|
|
3159
|
+
/**
|
|
3160
|
+
* Whether to remove child namespaces that have no sockets connected to them
|
|
3161
|
+
* @default false
|
|
3162
|
+
*/
|
|
3163
|
+
cleanupEmptyChildNamespaces: boolean;
|
|
3164
|
+
}
|
|
3165
|
+
/**
|
|
3166
|
+
* Represents a Socket.IO server.
|
|
3167
|
+
*
|
|
3168
|
+
* @example
|
|
3169
|
+
* import { Server } from "socket.io";
|
|
3170
|
+
*
|
|
3171
|
+
* const io = new Server();
|
|
3172
|
+
*
|
|
3173
|
+
* io.on("connection", (socket) => {
|
|
3174
|
+
* console.log(`socket ${socket.id} connected`);
|
|
3175
|
+
*
|
|
3176
|
+
* // send an event to the client
|
|
3177
|
+
* socket.emit("foo", "bar");
|
|
3178
|
+
*
|
|
3179
|
+
* socket.on("foobar", () => {
|
|
3180
|
+
* // an event was received from the client
|
|
3181
|
+
* });
|
|
3182
|
+
*
|
|
3183
|
+
* // upon disconnection
|
|
3184
|
+
* socket.on("disconnect", (reason) => {
|
|
3185
|
+
* console.log(`socket ${socket.id} disconnected due to ${reason}`);
|
|
3186
|
+
* });
|
|
3187
|
+
* });
|
|
3188
|
+
*
|
|
3189
|
+
* io.listen(3000);
|
|
3190
|
+
*/
|
|
3191
|
+
declare class Server<ListenEvents extends EventsMap$2 = DefaultEventsMap$1, EmitEvents extends EventsMap$2 = ListenEvents, ServerSideEvents extends EventsMap$2 = DefaultEventsMap$1, SocketData = any> extends StrictEventEmitter<ServerSideEvents, RemoveAcknowledgements<EmitEvents>, ServerReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents, SocketData>> {
|
|
3192
|
+
readonly sockets: Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>;
|
|
3193
|
+
/**
|
|
3194
|
+
* A reference to the underlying Engine.IO server.
|
|
3195
|
+
*
|
|
3196
|
+
* @example
|
|
3197
|
+
* const clientsCount = io.engine.clientsCount;
|
|
3198
|
+
*
|
|
3199
|
+
*/
|
|
3200
|
+
engine: BaseServer;
|
|
3201
|
+
/** @private */
|
|
3202
|
+
readonly _parser: typeof parser;
|
|
3203
|
+
/** @private */
|
|
3204
|
+
readonly encoder: Encoder;
|
|
3205
|
+
/**
|
|
3206
|
+
* @private
|
|
3207
|
+
*/
|
|
3208
|
+
_nsps: Map<string, Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>>;
|
|
3209
|
+
private parentNsps;
|
|
3210
|
+
/**
|
|
3211
|
+
* A subset of the {@link parentNsps} map, only containing {@link ParentNamespace} which are based on a regular
|
|
3212
|
+
* expression.
|
|
3213
|
+
*
|
|
3214
|
+
* @private
|
|
3215
|
+
*/
|
|
3216
|
+
private parentNamespacesFromRegExp;
|
|
3217
|
+
private _adapter?;
|
|
3218
|
+
private _serveClient;
|
|
3219
|
+
private readonly opts;
|
|
3220
|
+
private eio;
|
|
3221
|
+
private _path;
|
|
3222
|
+
private clientPathRegex;
|
|
3223
|
+
/**
|
|
3224
|
+
* @private
|
|
3225
|
+
*/
|
|
3226
|
+
_connectTimeout: number;
|
|
3227
|
+
private httpServer;
|
|
3228
|
+
private _corsMiddleware;
|
|
3229
|
+
/**
|
|
3230
|
+
* Server constructor.
|
|
3231
|
+
*
|
|
3232
|
+
* @param srv http server, port, or options
|
|
3233
|
+
* @param [opts]
|
|
3234
|
+
*/
|
|
3235
|
+
constructor(opts?: Partial<ServerOptions>);
|
|
3236
|
+
constructor(srv?: TServerInstance | number, opts?: Partial<ServerOptions>);
|
|
3237
|
+
constructor(srv: undefined | Partial<ServerOptions> | TServerInstance | number, opts?: Partial<ServerOptions>);
|
|
3238
|
+
get _opts(): Partial<ServerOptions>;
|
|
3239
|
+
/**
|
|
3240
|
+
* Sets/gets whether client code is being served.
|
|
3241
|
+
*
|
|
3242
|
+
* @param v - whether to serve client code
|
|
3243
|
+
* @return self when setting or value when getting
|
|
3244
|
+
*/
|
|
3245
|
+
serveClient(v: boolean): this;
|
|
3246
|
+
serveClient(): boolean;
|
|
3247
|
+
serveClient(v?: boolean): this | boolean;
|
|
3248
|
+
/**
|
|
3249
|
+
* Executes the middleware for an incoming namespace not already created on the server.
|
|
3250
|
+
*
|
|
3251
|
+
* @param name - name of incoming namespace
|
|
3252
|
+
* @param auth - the auth parameters
|
|
3253
|
+
* @param fn - callback
|
|
3254
|
+
*
|
|
3255
|
+
* @private
|
|
3256
|
+
*/
|
|
3257
|
+
_checkNamespace(name: string, auth: {
|
|
3258
|
+
[key: string]: any;
|
|
3259
|
+
}, fn: (nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData> | false) => void): void;
|
|
3260
|
+
/**
|
|
3261
|
+
* Sets the client serving path.
|
|
3262
|
+
*
|
|
3263
|
+
* @param {String} v pathname
|
|
3264
|
+
* @return {Server|String} self when setting or value when getting
|
|
3265
|
+
*/
|
|
3266
|
+
path(v: string): this;
|
|
3267
|
+
path(): string;
|
|
3268
|
+
path(v?: string): this | string;
|
|
3269
|
+
/**
|
|
3270
|
+
* Set the delay after which a client without namespace is closed
|
|
3271
|
+
* @param v
|
|
3272
|
+
*/
|
|
3273
|
+
connectTimeout(v: number): this;
|
|
3274
|
+
connectTimeout(): number;
|
|
3275
|
+
connectTimeout(v?: number): this | number;
|
|
3276
|
+
/**
|
|
3277
|
+
* Sets the adapter for rooms.
|
|
3278
|
+
*
|
|
3279
|
+
* @param v pathname
|
|
3280
|
+
* @return self when setting or value when getting
|
|
3281
|
+
*/
|
|
3282
|
+
adapter(): AdapterConstructor | undefined;
|
|
3283
|
+
adapter(v: AdapterConstructor): this;
|
|
3284
|
+
/**
|
|
3285
|
+
* Attaches socket.io to a server or port.
|
|
3286
|
+
*
|
|
3287
|
+
* @param srv - server or port
|
|
3288
|
+
* @param opts - options passed to engine.io
|
|
3289
|
+
* @return self
|
|
3290
|
+
*/
|
|
3291
|
+
listen(srv: TServerInstance | number, opts?: Partial<ServerOptions>): this;
|
|
3292
|
+
/**
|
|
3293
|
+
* Attaches socket.io to a server or port.
|
|
3294
|
+
*
|
|
3295
|
+
* @param srv - server or port
|
|
3296
|
+
* @param opts - options passed to engine.io
|
|
3297
|
+
* @return self
|
|
3298
|
+
*/
|
|
3299
|
+
attach(srv: TServerInstance | number, opts?: Partial<ServerOptions>): this;
|
|
3300
|
+
attachApp(app: any, opts?: Partial<ServerOptions>): void;
|
|
3301
|
+
/**
|
|
3302
|
+
* Initialize engine
|
|
3303
|
+
*
|
|
3304
|
+
* @param srv - the server to attach to
|
|
3305
|
+
* @param opts - options passed to engine.io
|
|
3306
|
+
* @private
|
|
3307
|
+
*/
|
|
3308
|
+
private initEngine;
|
|
3309
|
+
/**
|
|
3310
|
+
* Attaches the static file serving.
|
|
3311
|
+
*
|
|
3312
|
+
* @param srv http server
|
|
3313
|
+
* @private
|
|
3314
|
+
*/
|
|
3315
|
+
private attachServe;
|
|
3316
|
+
/**
|
|
3317
|
+
* Handles a request serving of client source and map
|
|
3318
|
+
*
|
|
3319
|
+
* @param req
|
|
3320
|
+
* @param res
|
|
3321
|
+
* @private
|
|
3322
|
+
*/
|
|
3323
|
+
private serve;
|
|
3324
|
+
/**
|
|
3325
|
+
* @param filename
|
|
3326
|
+
* @param req
|
|
3327
|
+
* @param res
|
|
3328
|
+
* @private
|
|
3329
|
+
*/
|
|
3330
|
+
private static sendFile;
|
|
3331
|
+
/**
|
|
3332
|
+
* Binds socket.io to an engine.io instance.
|
|
3333
|
+
*
|
|
3334
|
+
* @param engine engine.io (or compatible) server
|
|
3335
|
+
* @return self
|
|
3336
|
+
*/
|
|
3337
|
+
bind(engine: BaseServer): this;
|
|
3338
|
+
/**
|
|
3339
|
+
* Called with each incoming transport connection.
|
|
3340
|
+
*
|
|
3341
|
+
* @param {engine.Socket} conn
|
|
3342
|
+
* @return self
|
|
3343
|
+
* @private
|
|
3344
|
+
*/
|
|
3345
|
+
private onconnection;
|
|
3346
|
+
/**
|
|
3347
|
+
* Looks up a namespace.
|
|
3348
|
+
*
|
|
3349
|
+
* @example
|
|
3350
|
+
* // with a simple string
|
|
3351
|
+
* const myNamespace = io.of("/my-namespace");
|
|
3352
|
+
*
|
|
3353
|
+
* // with a regex
|
|
3354
|
+
* const dynamicNsp = io.of(/^\/dynamic-\d+$/).on("connection", (socket) => {
|
|
3355
|
+
* const namespace = socket.nsp; // newNamespace.name === "/dynamic-101"
|
|
3356
|
+
*
|
|
3357
|
+
* // broadcast to all clients in the given sub-namespace
|
|
3358
|
+
* namespace.emit("hello");
|
|
3359
|
+
* });
|
|
3360
|
+
*
|
|
3361
|
+
* @param name - nsp name
|
|
3362
|
+
* @param fn optional, nsp `connection` ev handler
|
|
3363
|
+
*/
|
|
3364
|
+
of(name: string | RegExp | ParentNspNameMatchFn, fn?: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void): Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>;
|
|
3365
|
+
/**
|
|
3366
|
+
* Closes server connection
|
|
3367
|
+
*
|
|
3368
|
+
* @param [fn] optional, called as `fn([err])` on error OR all conns closed
|
|
3369
|
+
*/
|
|
3370
|
+
close(fn?: (err?: Error) => void): void;
|
|
3371
|
+
/**
|
|
3372
|
+
* Registers a middleware, which is a function that gets executed for every incoming {@link Socket}.
|
|
3373
|
+
*
|
|
3374
|
+
* @example
|
|
3375
|
+
* io.use((socket, next) => {
|
|
3376
|
+
* // ...
|
|
3377
|
+
* next();
|
|
3378
|
+
* });
|
|
3379
|
+
*
|
|
3380
|
+
* @param fn - the middleware function
|
|
3381
|
+
*/
|
|
3382
|
+
use(fn: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, next: (err?: ExtendedError$1) => void) => void): this;
|
|
3383
|
+
/**
|
|
3384
|
+
* Targets a room when emitting.
|
|
3385
|
+
*
|
|
3386
|
+
* @example
|
|
3387
|
+
* // the “foo” event will be broadcast to all connected clients in the “room-101” room
|
|
3388
|
+
* io.to("room-101").emit("foo", "bar");
|
|
3389
|
+
*
|
|
3390
|
+
* // with an array of rooms (a client will be notified at most once)
|
|
3391
|
+
* io.to(["room-101", "room-102"]).emit("foo", "bar");
|
|
3392
|
+
*
|
|
3393
|
+
* // with multiple chained calls
|
|
3394
|
+
* io.to("room-101").to("room-102").emit("foo", "bar");
|
|
3395
|
+
*
|
|
3396
|
+
* @param room - a room, or an array of rooms
|
|
3397
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
3398
|
+
*/
|
|
3399
|
+
to(room: Room | Room[]): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
3400
|
+
/**
|
|
3401
|
+
* Targets a room when emitting. Similar to `to()`, but might feel clearer in some cases:
|
|
3402
|
+
*
|
|
3403
|
+
* @example
|
|
3404
|
+
* // disconnect all clients in the "room-101" room
|
|
3405
|
+
* io.in("room-101").disconnectSockets();
|
|
3406
|
+
*
|
|
3407
|
+
* @param room - a room, or an array of rooms
|
|
3408
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
3409
|
+
*/
|
|
3410
|
+
in(room: Room | Room[]): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
3411
|
+
/**
|
|
3412
|
+
* Excludes a room when emitting.
|
|
3413
|
+
*
|
|
3414
|
+
* @example
|
|
3415
|
+
* // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
|
|
3416
|
+
* io.except("room-101").emit("foo", "bar");
|
|
3417
|
+
*
|
|
3418
|
+
* // with an array of rooms
|
|
3419
|
+
* io.except(["room-101", "room-102"]).emit("foo", "bar");
|
|
3420
|
+
*
|
|
3421
|
+
* // with multiple chained calls
|
|
3422
|
+
* io.except("room-101").except("room-102").emit("foo", "bar");
|
|
3423
|
+
*
|
|
3424
|
+
* @param room - a room, or an array of rooms
|
|
3425
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
3426
|
+
*/
|
|
3427
|
+
except(room: Room | Room[]): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
3428
|
+
/**
|
|
3429
|
+
* Sends a `message` event to all clients.
|
|
3430
|
+
*
|
|
3431
|
+
* This method mimics the WebSocket.send() method.
|
|
3432
|
+
*
|
|
3433
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
|
|
3434
|
+
*
|
|
3435
|
+
* @example
|
|
3436
|
+
* io.send("hello");
|
|
3437
|
+
*
|
|
3438
|
+
* // this is equivalent to
|
|
3439
|
+
* io.emit("message", "hello");
|
|
3440
|
+
*
|
|
3441
|
+
* @return self
|
|
3442
|
+
*/
|
|
3443
|
+
send(...args: EventParams$1<EmitEvents, "message">): this;
|
|
3444
|
+
/**
|
|
3445
|
+
* Sends a `message` event to all clients. Alias of {@link send}.
|
|
3446
|
+
*
|
|
3447
|
+
* @return self
|
|
3448
|
+
*/
|
|
3449
|
+
write(...args: EventParams$1<EmitEvents, "message">): this;
|
|
3450
|
+
/**
|
|
3451
|
+
* Sends a message to the other Socket.IO servers of the cluster.
|
|
3452
|
+
*
|
|
3453
|
+
* @example
|
|
3454
|
+
* io.serverSideEmit("hello", "world");
|
|
3455
|
+
*
|
|
3456
|
+
* io.on("hello", (arg1) => {
|
|
3457
|
+
* console.log(arg1); // prints "world"
|
|
3458
|
+
* });
|
|
3459
|
+
*
|
|
3460
|
+
* // acknowledgements (without binary content) are supported too:
|
|
3461
|
+
* io.serverSideEmit("ping", (err, responses) => {
|
|
3462
|
+
* if (err) {
|
|
3463
|
+
* // some servers did not acknowledge the event in the given delay
|
|
3464
|
+
* } else {
|
|
3465
|
+
* console.log(responses); // one response per server (except the current one)
|
|
3466
|
+
* }
|
|
3467
|
+
* });
|
|
3468
|
+
*
|
|
3469
|
+
* io.on("ping", (cb) => {
|
|
3470
|
+
* cb("pong");
|
|
3471
|
+
* });
|
|
3472
|
+
*
|
|
3473
|
+
* @param ev - the event name
|
|
3474
|
+
* @param args - an array of arguments, which may include an acknowledgement callback at the end
|
|
3475
|
+
*/
|
|
3476
|
+
serverSideEmit<Ev extends EventNames$1<ServerSideEvents>>(ev: Ev, ...args: EventParams$1<DecorateAcknowledgementsWithTimeoutAndMultipleResponses<ServerSideEvents>, Ev>): boolean;
|
|
3477
|
+
/**
|
|
3478
|
+
* Sends a message and expect an acknowledgement from the other Socket.IO servers of the cluster.
|
|
3479
|
+
*
|
|
3480
|
+
* @example
|
|
3481
|
+
* try {
|
|
3482
|
+
* const responses = await io.serverSideEmitWithAck("ping");
|
|
3483
|
+
* console.log(responses); // one response per server (except the current one)
|
|
3484
|
+
* } catch (e) {
|
|
3485
|
+
* // some servers did not acknowledge the event in the given delay
|
|
3486
|
+
* }
|
|
3487
|
+
*
|
|
3488
|
+
* @param ev - the event name
|
|
3489
|
+
* @param args - an array of arguments
|
|
3490
|
+
*
|
|
3491
|
+
* @return a Promise that will be fulfilled when all servers have acknowledged the event
|
|
3492
|
+
*/
|
|
3493
|
+
serverSideEmitWithAck<Ev extends EventNamesWithAck<ServerSideEvents>>(ev: Ev, ...args: AllButLast<EventParams$1<ServerSideEvents, Ev>>): Promise<FirstNonErrorArg<Last<EventParams$1<ServerSideEvents, Ev>>>[]>;
|
|
3494
|
+
/**
|
|
3495
|
+
* Gets a list of socket ids.
|
|
3496
|
+
*
|
|
3497
|
+
* @deprecated this method will be removed in the next major release, please use {@link Server#serverSideEmit} or
|
|
3498
|
+
* {@link Server#fetchSockets} instead.
|
|
3499
|
+
*/
|
|
3500
|
+
allSockets(): Promise<Set<SocketId>>;
|
|
3501
|
+
/**
|
|
3502
|
+
* Sets the compress flag.
|
|
3503
|
+
*
|
|
3504
|
+
* @example
|
|
3505
|
+
* io.compress(false).emit("hello");
|
|
3506
|
+
*
|
|
3507
|
+
* @param compress - if `true`, compresses the sending data
|
|
3508
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
3509
|
+
*/
|
|
3510
|
+
compress(compress: boolean): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
3511
|
+
/**
|
|
3512
|
+
* Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
|
|
3513
|
+
* receive messages (because of network slowness or other issues, or because they’re connected through long polling
|
|
3514
|
+
* and is in the middle of a request-response cycle).
|
|
3515
|
+
*
|
|
3516
|
+
* @example
|
|
3517
|
+
* io.volatile.emit("hello"); // the clients may or may not receive it
|
|
3518
|
+
*
|
|
3519
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
3520
|
+
*/
|
|
3521
|
+
get volatile(): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
3522
|
+
/**
|
|
3523
|
+
* Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
|
|
3524
|
+
*
|
|
3525
|
+
* @example
|
|
3526
|
+
* // the “foo” event will be broadcast to all connected clients on this node
|
|
3527
|
+
* io.local.emit("foo", "bar");
|
|
3528
|
+
*
|
|
3529
|
+
* @return a new {@link BroadcastOperator} instance for chaining
|
|
3530
|
+
*/
|
|
3531
|
+
get local(): BroadcastOperator<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>, SocketData>;
|
|
3532
|
+
/**
|
|
3533
|
+
* Adds a timeout in milliseconds for the next operation.
|
|
3534
|
+
*
|
|
3535
|
+
* @example
|
|
3536
|
+
* io.timeout(1000).emit("some-event", (err, responses) => {
|
|
3537
|
+
* if (err) {
|
|
3538
|
+
* // some clients did not acknowledge the event in the given delay
|
|
3539
|
+
* } else {
|
|
3540
|
+
* console.log(responses); // one response per client
|
|
3541
|
+
* }
|
|
3542
|
+
* });
|
|
3543
|
+
*
|
|
3544
|
+
* @param timeout
|
|
3545
|
+
*/
|
|
3546
|
+
timeout(timeout: number): BroadcastOperator<DecorateAcknowledgements<DecorateAcknowledgementsWithMultipleResponses<EmitEvents>>, SocketData>;
|
|
3547
|
+
/**
|
|
3548
|
+
* Returns the matching socket instances.
|
|
3549
|
+
*
|
|
3550
|
+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
|
3551
|
+
*
|
|
3552
|
+
* @example
|
|
3553
|
+
* // return all Socket instances
|
|
3554
|
+
* const sockets = await io.fetchSockets();
|
|
3555
|
+
*
|
|
3556
|
+
* // return all Socket instances in the "room1" room
|
|
3557
|
+
* const sockets = await io.in("room1").fetchSockets();
|
|
3558
|
+
*
|
|
3559
|
+
* for (const socket of sockets) {
|
|
3560
|
+
* console.log(socket.id);
|
|
3561
|
+
* console.log(socket.handshake);
|
|
3562
|
+
* console.log(socket.rooms);
|
|
3563
|
+
* console.log(socket.data);
|
|
3564
|
+
*
|
|
3565
|
+
* socket.emit("hello");
|
|
3566
|
+
* socket.join("room1");
|
|
3567
|
+
* socket.leave("room2");
|
|
3568
|
+
* socket.disconnect();
|
|
3569
|
+
* }
|
|
3570
|
+
*/
|
|
3571
|
+
fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]>;
|
|
3572
|
+
/**
|
|
3573
|
+
* Makes the matching socket instances join the specified rooms.
|
|
3574
|
+
*
|
|
3575
|
+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
|
3576
|
+
*
|
|
3577
|
+
* @example
|
|
3578
|
+
*
|
|
3579
|
+
* // make all socket instances join the "room1" room
|
|
3580
|
+
* io.socketsJoin("room1");
|
|
3581
|
+
*
|
|
3582
|
+
* // make all socket instances in the "room1" room join the "room2" and "room3" rooms
|
|
3583
|
+
* io.in("room1").socketsJoin(["room2", "room3"]);
|
|
3584
|
+
*
|
|
3585
|
+
* @param room - a room, or an array of rooms
|
|
3586
|
+
*/
|
|
3587
|
+
socketsJoin(room: Room | Room[]): void;
|
|
3588
|
+
/**
|
|
3589
|
+
* Makes the matching socket instances leave the specified rooms.
|
|
3590
|
+
*
|
|
3591
|
+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
|
3592
|
+
*
|
|
3593
|
+
* @example
|
|
3594
|
+
* // make all socket instances leave the "room1" room
|
|
3595
|
+
* io.socketsLeave("room1");
|
|
3596
|
+
*
|
|
3597
|
+
* // make all socket instances in the "room1" room leave the "room2" and "room3" rooms
|
|
3598
|
+
* io.in("room1").socketsLeave(["room2", "room3"]);
|
|
3599
|
+
*
|
|
3600
|
+
* @param room - a room, or an array of rooms
|
|
3601
|
+
*/
|
|
3602
|
+
socketsLeave(room: Room | Room[]): void;
|
|
3603
|
+
/**
|
|
3604
|
+
* Makes the matching socket instances disconnect.
|
|
3605
|
+
*
|
|
3606
|
+
* Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
|
|
3607
|
+
*
|
|
3608
|
+
* @example
|
|
3609
|
+
* // make all socket instances disconnect (the connections might be kept alive for other namespaces)
|
|
3610
|
+
* io.disconnectSockets();
|
|
3611
|
+
*
|
|
3612
|
+
* // make all socket instances in the "room1" room disconnect and close the underlying connections
|
|
3613
|
+
* io.in("room1").disconnectSockets(true);
|
|
3614
|
+
*
|
|
3615
|
+
* @param close - whether to close the underlying connection
|
|
3616
|
+
*/
|
|
3617
|
+
disconnectSockets(close?: boolean): void;
|
|
3618
|
+
}
|
|
3619
|
+
|
|
3620
|
+
/**
|
|
3621
|
+
* An events map is an interface that maps event names to their value, which
|
|
3622
|
+
* represents the type of the `on` listener.
|
|
3623
|
+
*/
|
|
3624
|
+
interface EventsMap {
|
|
3625
|
+
[event: string]: any;
|
|
3626
|
+
}
|
|
3627
|
+
/**
|
|
3628
|
+
* The default events map, used if no EventsMap is given. Using this EventsMap
|
|
3629
|
+
* is equivalent to accepting all event names, and any data.
|
|
3630
|
+
*/
|
|
3631
|
+
interface DefaultEventsMap {
|
|
3632
|
+
[event: string]: (...args: any[]) => void;
|
|
3633
|
+
}
|
|
3634
|
+
|
|
3635
|
+
interface ExtendedError extends Error {
|
|
3636
|
+
data?: any;
|
|
3637
|
+
}
|
|
3638
|
+
type ZodNamespaceSocket<TClientMessages extends ZodSocketMessageCatalogSchema, TServerMessages extends ZodSocketMessageCatalogSchema, TServerSideEvents extends EventsMap = DefaultEventsMap, TSocketData extends z.ZodObject<any, any, any> = any> = Socket<ZodMessageCatalogToSocketIoEvents<TClientMessages>, ZodMessageCatalogToSocketIoEvents<TServerMessages>, TServerSideEvents, z.infer<TSocketData>>;
|
|
3639
|
+
interface ZodNamespaceOptions<TClientMessages extends ZodSocketMessageCatalogSchema, TServerMessages extends ZodSocketMessageCatalogSchema, TServerSideEvents extends EventsMap = DefaultEventsMap, TSocketData extends z.ZodObject<any, any, any> = any> {
|
|
3640
|
+
io: Server;
|
|
3641
|
+
name: string;
|
|
3642
|
+
clientMessages: TClientMessages;
|
|
3643
|
+
serverMessages: TServerMessages;
|
|
3644
|
+
socketData?: TSocketData;
|
|
3645
|
+
handlers?: ZodSocketMessageHandlers<TClientMessages>;
|
|
3646
|
+
authToken?: string;
|
|
3647
|
+
logger?: StructuredLogger;
|
|
3648
|
+
preAuth?: (socket: ZodNamespaceSocket<TClientMessages, TServerMessages, TServerSideEvents, TSocketData>, next: (err?: ExtendedError) => void, logger: StructuredLogger) => Promise<void>;
|
|
3649
|
+
postAuth?: (socket: ZodNamespaceSocket<TClientMessages, TServerMessages, TServerSideEvents, TSocketData>, next: (err?: ExtendedError) => void, logger: StructuredLogger) => Promise<void>;
|
|
3650
|
+
onConnection?: (socket: ZodNamespaceSocket<TClientMessages, TServerMessages, TServerSideEvents, TSocketData>, handler: ZodSocketMessageHandler<TClientMessages>, sender: ZodMessageSender<TServerMessages>, logger: StructuredLogger) => Promise<void>;
|
|
3651
|
+
onDisconnect?: (socket: ZodNamespaceSocket<TClientMessages, TServerMessages, TServerSideEvents, TSocketData>, reason: DisconnectReason, description: any, logger: StructuredLogger) => Promise<void>;
|
|
3652
|
+
onError?: (socket: ZodNamespaceSocket<TClientMessages, TServerMessages, TServerSideEvents, TSocketData>, err: Error, logger: StructuredLogger) => Promise<void>;
|
|
3653
|
+
}
|
|
3654
|
+
declare class ZodNamespace<TClientMessages extends ZodSocketMessageCatalogSchema, TServerMessages extends ZodSocketMessageCatalogSchema, TSocketData extends z.ZodObject<any, any, any> = any, TServerSideEvents extends EventsMap = DefaultEventsMap> {
|
|
3655
|
+
#private;
|
|
3656
|
+
sender: ZodMessageSender<TServerMessages>;
|
|
3657
|
+
io: Server;
|
|
3658
|
+
namespace: Namespace<ZodMessageCatalogToSocketIoEvents<TClientMessages>, ZodMessageCatalogToSocketIoEvents<TServerMessages>, TServerSideEvents, z.infer<TSocketData>>;
|
|
3659
|
+
constructor(opts: ZodNamespaceOptions<TClientMessages, TServerMessages, TServerSideEvents, TSocketData>);
|
|
3660
|
+
fetchSockets(): Promise<RemoteSocket<ZodMessageCatalogToSocketIoEvents<TServerMessages>, z.TypeOf<TSocketData>>[]>;
|
|
3661
|
+
}
|
|
3662
|
+
|
|
3663
|
+
export { ZodNamespace, type ZodNamespaceSocket };
|