socket.d.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { EventEmitter } from "events";
  2. import type { IncomingMessage } from "http";
  3. import type { EngineRequest, Transport } from "./transport";
  4. import type { BaseServer } from "./server";
  5. import type { RawData } from "engine.io-parser";
  6. export interface SendOptions {
  7. compress?: boolean;
  8. }
  9. type ReadyState = "opening" | "open" | "closing" | "closed";
  10. type SendCallback = (transport: Transport) => void;
  11. export declare class Socket extends EventEmitter {
  12. /**
  13. * The revision of the protocol:
  14. *
  15. * - 3rd is used in Engine.IO v3 / Socket.IO v2
  16. * - 4th is used in Engine.IO v4 and above / Socket.IO v3 and above
  17. *
  18. * It is found in the `EIO` query parameters of the HTTP requests.
  19. *
  20. * @see https://github.com/socketio/engine.io-protocol
  21. */
  22. readonly protocol: number;
  23. /**
  24. * A reference to the first HTTP request of the session
  25. *
  26. * TODO for the next major release: remove it
  27. */
  28. request: IncomingMessage;
  29. /**
  30. * The IP address of the client.
  31. */
  32. readonly remoteAddress: string;
  33. /**
  34. * The current state of the socket.
  35. */
  36. _readyState: ReadyState;
  37. /**
  38. * The current low-level transport.
  39. */
  40. transport: Transport;
  41. private server;
  42. upgrading: boolean;
  43. upgraded: boolean;
  44. private writeBuffer;
  45. private packetsFn;
  46. private sentCallbackFn;
  47. private cleanupFn;
  48. private pingTimeoutTimer;
  49. private pingIntervalTimer;
  50. /**
  51. * This is the session identifier that the client will use in the subsequent HTTP requests. It must not be shared with
  52. * others parties, as it might lead to session hijacking.
  53. *
  54. * @private
  55. */
  56. private readonly id;
  57. get readyState(): ReadyState;
  58. set readyState(state: ReadyState);
  59. constructor(id: string, server: BaseServer, transport: Transport, req: EngineRequest, protocol: number);
  60. /**
  61. * Called upon transport considered open.
  62. *
  63. * @private
  64. */
  65. private onOpen;
  66. /**
  67. * Called upon transport packet.
  68. *
  69. * @param {Object} packet
  70. * @private
  71. */
  72. private onPacket;
  73. /**
  74. * Called upon transport error.
  75. *
  76. * @param {Error} err - error object
  77. * @private
  78. */
  79. private onError;
  80. /**
  81. * Pings client every `this.pingInterval` and expects response
  82. * within `this.pingTimeout` or closes connection.
  83. *
  84. * @private
  85. */
  86. private schedulePing;
  87. /**
  88. * Resets ping timeout.
  89. *
  90. * @private
  91. */
  92. private resetPingTimeout;
  93. /**
  94. * Attaches handlers for the given transport.
  95. *
  96. * @param {Transport} transport
  97. * @private
  98. */
  99. private setTransport;
  100. /**
  101. * Upon transport "drain" event
  102. *
  103. * @private
  104. */
  105. private onDrain;
  106. /**
  107. * Upgrades socket to the given transport
  108. *
  109. * @param {Transport} transport
  110. * @private
  111. */
  112. _maybeUpgrade(transport: Transport): void;
  113. /**
  114. * Clears listeners and timers associated with current transport.
  115. *
  116. * @private
  117. */
  118. private clearTransport;
  119. /**
  120. * Called upon transport considered closed.
  121. * Possible reasons: `ping timeout`, `client error`, `parse error`,
  122. * `transport error`, `server close`, `transport close`
  123. */
  124. private onClose;
  125. /**
  126. * Sends a message packet.
  127. *
  128. * @param {Object} data
  129. * @param {Object} options
  130. * @param {Function} callback
  131. * @return {Socket} for chaining
  132. */
  133. send(data: RawData, options?: SendOptions, callback?: SendCallback): this;
  134. /**
  135. * Alias of {@link send}.
  136. *
  137. * @param data
  138. * @param options
  139. * @param callback
  140. */
  141. write(data: RawData, options?: SendOptions, callback?: SendCallback): this;
  142. /**
  143. * Sends a packet.
  144. *
  145. * @param {String} type - packet type
  146. * @param {String} data
  147. * @param {Object} options
  148. * @param {Function} callback
  149. *
  150. * @private
  151. */
  152. private sendPacket;
  153. /**
  154. * Attempts to flush the packets buffer.
  155. *
  156. * @private
  157. */
  158. private flush;
  159. /**
  160. * Get available upgrades for this socket.
  161. *
  162. * @private
  163. */
  164. private getAvailableUpgrades;
  165. /**
  166. * Closes the socket and underlying transport.
  167. *
  168. * @param {Boolean} discard - optional, discard the transport
  169. * @return {Socket} for chaining
  170. */
  171. close(discard?: boolean): void;
  172. /**
  173. * Closes the underlying transport.
  174. *
  175. * @param {Boolean} discard
  176. * @private
  177. */
  178. private closeTransport;
  179. }
  180. export {};