socket.d.ts 3.9 KB

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