socket.d.ts 7.0 KB

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