client.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Client = void 0;
  4. const socket_io_parser_1 = require("socket.io-parser");
  5. const debugModule = require("debug");
  6. const debug = debugModule("socket.io:client");
  7. class Client {
  8. /**
  9. * Client constructor.
  10. *
  11. * @param {Server} server instance
  12. * @param {Socket} conn
  13. * @package
  14. */
  15. constructor(server, conn) {
  16. this.sockets = new Map();
  17. this.nsps = new Map();
  18. this.server = server;
  19. this.conn = conn;
  20. this.encoder = server.encoder;
  21. this.decoder = new server._parser.Decoder();
  22. this.id = conn.id;
  23. this.setup();
  24. }
  25. /**
  26. * @return the reference to the request that originated the Engine.IO connection
  27. *
  28. * @public
  29. */
  30. get request() {
  31. return this.conn.request;
  32. }
  33. /**
  34. * Sets up event listeners.
  35. *
  36. * @private
  37. */
  38. setup() {
  39. this.onclose = this.onclose.bind(this);
  40. this.ondata = this.ondata.bind(this);
  41. this.onerror = this.onerror.bind(this);
  42. this.ondecoded = this.ondecoded.bind(this);
  43. // @ts-ignore
  44. this.decoder.on("decoded", this.ondecoded);
  45. this.conn.on("data", this.ondata);
  46. this.conn.on("error", this.onerror);
  47. this.conn.on("close", this.onclose);
  48. this.connectTimeout = setTimeout(() => {
  49. if (this.nsps.size === 0) {
  50. debug("no namespace joined yet, close the client");
  51. this.close();
  52. }
  53. else {
  54. debug("the client has already joined a namespace, nothing to do");
  55. }
  56. }, this.server._connectTimeout);
  57. }
  58. /**
  59. * Connects a client to a namespace.
  60. *
  61. * @param {String} name - the namespace
  62. * @param {Object} auth - the auth parameters
  63. * @private
  64. */
  65. connect(name, auth = {}) {
  66. if (this.server._nsps.has(name)) {
  67. debug("connecting to namespace %s", name);
  68. return this.doConnect(name, auth);
  69. }
  70. this.server._checkNamespace(name, auth, (dynamicNsp) => {
  71. if (dynamicNsp) {
  72. debug("dynamic namespace %s was created", dynamicNsp.name);
  73. this.doConnect(name, auth);
  74. }
  75. else {
  76. debug("creation of namespace %s was denied", name);
  77. this._packet({
  78. type: socket_io_parser_1.PacketType.CONNECT_ERROR,
  79. nsp: name,
  80. data: {
  81. message: "Invalid namespace"
  82. }
  83. });
  84. }
  85. });
  86. }
  87. /**
  88. * Connects a client to a namespace.
  89. *
  90. * @param {String} name - the namespace
  91. * @param {Object} auth - the auth parameters
  92. *
  93. * @private
  94. */
  95. doConnect(name, auth) {
  96. if (this.connectTimeout) {
  97. clearTimeout(this.connectTimeout);
  98. this.connectTimeout = null;
  99. }
  100. const nsp = this.server.of(name);
  101. const socket = nsp._add(this, auth, () => {
  102. this.sockets.set(socket.id, socket);
  103. this.nsps.set(nsp.name, socket);
  104. });
  105. }
  106. /**
  107. * Disconnects from all namespaces and closes transport.
  108. *
  109. * @private
  110. */
  111. _disconnect() {
  112. for (const socket of this.sockets.values()) {
  113. socket.disconnect();
  114. }
  115. this.sockets.clear();
  116. this.close();
  117. }
  118. /**
  119. * Removes a socket. Called by each `Socket`.
  120. *
  121. * @private
  122. */
  123. _remove(socket) {
  124. if (this.sockets.has(socket.id)) {
  125. const nsp = this.sockets.get(socket.id).nsp.name;
  126. this.sockets.delete(socket.id);
  127. this.nsps.delete(nsp);
  128. }
  129. else {
  130. debug("ignoring remove for %s", socket.id);
  131. }
  132. }
  133. /**
  134. * Closes the underlying connection.
  135. *
  136. * @private
  137. */
  138. close() {
  139. if ("open" == this.conn.readyState) {
  140. debug("forcing transport close");
  141. this.conn.close();
  142. this.onclose("forced server close");
  143. }
  144. }
  145. /**
  146. * Writes a packet to the transport.
  147. *
  148. * @param {Object} packet object
  149. * @param {Object} opts
  150. * @private
  151. */
  152. _packet(packet, opts) {
  153. opts = opts || {};
  154. const self = this;
  155. // this writes to the actual connection
  156. function writeToEngine(encodedPackets) {
  157. if (opts.volatile && !self.conn.transport.writable)
  158. return;
  159. for (let i = 0; i < encodedPackets.length; i++) {
  160. self.conn.write(encodedPackets[i], { compress: opts.compress });
  161. }
  162. }
  163. if ("open" == this.conn.readyState) {
  164. debug("writing packet %j", packet);
  165. if (!opts.preEncoded) {
  166. // not broadcasting, need to encode
  167. writeToEngine(this.encoder.encode(packet)); // encode, then write results to engine
  168. }
  169. else {
  170. // a broadcast pre-encodes a packet
  171. writeToEngine(packet);
  172. }
  173. }
  174. else {
  175. debug("ignoring packet write %j", packet);
  176. }
  177. }
  178. /**
  179. * Called with incoming transport data.
  180. *
  181. * @private
  182. */
  183. ondata(data) {
  184. // try/catch is needed for protocol violations (GH-1880)
  185. try {
  186. this.decoder.add(data);
  187. }
  188. catch (e) {
  189. this.onerror(e);
  190. }
  191. }
  192. /**
  193. * Called when parser fully decodes a packet.
  194. *
  195. * @private
  196. */
  197. ondecoded(packet) {
  198. if (socket_io_parser_1.PacketType.CONNECT == packet.type) {
  199. this.connect(packet.nsp, packet.data);
  200. }
  201. else {
  202. const socket = this.nsps.get(packet.nsp);
  203. if (socket) {
  204. process.nextTick(function () {
  205. socket._onpacket(packet);
  206. });
  207. }
  208. else {
  209. debug("no socket for namespace %s", packet.nsp);
  210. }
  211. }
  212. }
  213. /**
  214. * Handles an error.
  215. *
  216. * @param {Object} err object
  217. * @private
  218. */
  219. onerror(err) {
  220. for (const socket of this.sockets.values()) {
  221. socket._onerror(err);
  222. }
  223. this.conn.close();
  224. }
  225. /**
  226. * Called upon transport close.
  227. *
  228. * @param reason
  229. * @private
  230. */
  231. onclose(reason) {
  232. debug("client close with reason %s", reason);
  233. // ignore a potential subsequent `close` event
  234. this.destroy();
  235. // `nsps` and `sockets` are cleaned up seamlessly
  236. for (const socket of this.sockets.values()) {
  237. socket._onclose(reason);
  238. }
  239. this.sockets.clear();
  240. this.decoder.destroy(); // clean up decoder
  241. }
  242. /**
  243. * Cleans up event listeners.
  244. * @private
  245. */
  246. destroy() {
  247. this.conn.removeListener("data", this.ondata);
  248. this.conn.removeListener("error", this.onerror);
  249. this.conn.removeListener("close", this.onclose);
  250. // @ts-ignore
  251. this.decoder.removeListener("decoded", this.ondecoded);
  252. }
  253. }
  254. exports.Client = Client;