UNPKG

5.95 kBTypeScriptView Raw
1import { Packet } from "socket.io-parser";
2import { Manager } from "./manager.js";
3import { DefaultEventsMap, EventNames, EventParams, EventsMap, Emitter } from "@socket.io/component-emitter";
4export interface SocketOptions {
5 /**
6 * the authentication payload sent when connecting to the Namespace
7 */
8 auth: {
9 [key: string]: any;
10 } | ((cb: (data: object) => void) => void);
11}
12interface SocketReservedEvents {
13 connect: () => void;
14 connect_error: (err: Error) => void;
15 disconnect: (reason: Socket.DisconnectReason) => void;
16}
17export declare class Socket<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents> extends Emitter<ListenEvents, EmitEvents, SocketReservedEvents> {
18 readonly io: Manager<ListenEvents, EmitEvents>;
19 id: string;
20 connected: boolean;
21 disconnected: boolean;
22 auth: {
23 [key: string]: any;
24 } | ((cb: (data: object) => void) => void);
25 receiveBuffer: Array<ReadonlyArray<any>>;
26 sendBuffer: Array<Packet>;
27 private readonly nsp;
28 private ids;
29 private acks;
30 private flags;
31 private subs?;
32 private _anyListeners;
33 /**
34 * `Socket` constructor.
35 *
36 * @public
37 */
38 constructor(io: Manager, nsp: string, opts?: Partial<SocketOptions>);
39 /**
40 * Subscribe to open, close and packet events
41 *
42 * @private
43 */
44 private subEvents;
45 /**
46 * Whether the Socket will try to reconnect when its Manager connects or reconnects
47 */
48 get active(): boolean;
49 /**
50 * "Opens" the socket.
51 *
52 * @public
53 */
54 connect(): this;
55 /**
56 * Alias for connect()
57 */
58 open(): this;
59 /**
60 * Sends a `message` event.
61 *
62 * @return self
63 * @public
64 */
65 send(...args: any[]): this;
66 /**
67 * Override `emit`.
68 * If the event is in `events`, it's emitted normally.
69 *
70 * @return self
71 * @public
72 */
73 emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): this;
74 /**
75 * @private
76 */
77 private _registerAckCallback;
78 /**
79 * Sends a packet.
80 *
81 * @param packet
82 * @private
83 */
84 private packet;
85 /**
86 * Called upon engine `open`.
87 *
88 * @private
89 */
90 private onopen;
91 /**
92 * Called upon engine or manager `error`.
93 *
94 * @param err
95 * @private
96 */
97 private onerror;
98 /**
99 * Called upon engine `close`.
100 *
101 * @param reason
102 * @private
103 */
104 private onclose;
105 /**
106 * Called with socket packet.
107 *
108 * @param packet
109 * @private
110 */
111 private onpacket;
112 /**
113 * Called upon a server event.
114 *
115 * @param packet
116 * @private
117 */
118 private onevent;
119 private emitEvent;
120 /**
121 * Produces an ack callback to emit with an event.
122 *
123 * @private
124 */
125 private ack;
126 /**
127 * Called upon a server acknowlegement.
128 *
129 * @param packet
130 * @private
131 */
132 private onack;
133 /**
134 * Called upon server connect.
135 *
136 * @private
137 */
138 private onconnect;
139 /**
140 * Emit buffered events (received and emitted).
141 *
142 * @private
143 */
144 private emitBuffered;
145 /**
146 * Called upon server disconnect.
147 *
148 * @private
149 */
150 private ondisconnect;
151 /**
152 * Called upon forced client/server side disconnections,
153 * this method ensures the manager stops tracking us and
154 * that reconnections don't get triggered for this.
155 *
156 * @private
157 */
158 private destroy;
159 /**
160 * Disconnects the socket manually.
161 *
162 * @return self
163 * @public
164 */
165 disconnect(): this;
166 /**
167 * Alias for disconnect()
168 *
169 * @return self
170 * @public
171 */
172 close(): this;
173 /**
174 * Sets the compress flag.
175 *
176 * @param compress - if `true`, compresses the sending data
177 * @return self
178 * @public
179 */
180 compress(compress: boolean): this;
181 /**
182 * Sets a modifier for a subsequent event emission that the event message will be dropped when this socket is not
183 * ready to send messages.
184 *
185 * @returns self
186 * @public
187 */
188 get volatile(): this;
189 /**
190 * Sets a modifier for a subsequent event emission that the callback will be called with an error when the
191 * given number of milliseconds have elapsed without an acknowledgement from the server:
192 *
193 * ```
194 * socket.timeout(5000).emit("my-event", (err) => {
195 * if (err) {
196 * // the server did not acknowledge the event in the given delay
197 * }
198 * });
199 * ```
200 *
201 * @returns self
202 * @public
203 */
204 timeout(timeout: number): this;
205 /**
206 * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
207 * callback.
208 *
209 * @param listener
210 * @public
211 */
212 onAny(listener: (...args: any[]) => void): this;
213 /**
214 * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
215 * callback. The listener is added to the beginning of the listeners array.
216 *
217 * @param listener
218 * @public
219 */
220 prependAny(listener: (...args: any[]) => void): this;
221 /**
222 * Removes the listener that will be fired when any event is emitted.
223 *
224 * @param listener
225 * @public
226 */
227 offAny(listener?: (...args: any[]) => void): this;
228 /**
229 * Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
230 * e.g. to remove listeners.
231 *
232 * @public
233 */
234 listenersAny(): ((...args: any[]) => void)[];
235}
236export declare namespace Socket {
237 type DisconnectReason = "io server disconnect" | "io client disconnect" | "ping timeout" | "transport close" | "transport error";
238}
239export {};
240
\No newline at end of file