uniapp,h5

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __importDefault = (this && this.__importDefault) || function (mod) {
  26. return (mod && mod.__esModule) ? mod : { "default": mod };
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. exports.Manager = void 0;
  30. const engine_io_client_1 = require("engine.io-client");
  31. const socket_js_1 = require("./socket.js");
  32. const parser = __importStar(require("socket.io-parser"));
  33. const on_js_1 = require("./on.js");
  34. const backo2_js_1 = require("./contrib/backo2.js");
  35. const component_emitter_1 = require("@socket.io/component-emitter");
  36. const debug_1 = __importDefault(require("debug")); // debug()
  37. const debug = (0, debug_1.default)("socket.io-client:manager"); // debug()
  38. class Manager extends component_emitter_1.Emitter {
  39. constructor(uri, opts) {
  40. var _a;
  41. super();
  42. this.nsps = {};
  43. this.subs = [];
  44. if (uri && "object" === typeof uri) {
  45. opts = uri;
  46. uri = undefined;
  47. }
  48. opts = opts || {};
  49. opts.path = opts.path || "/socket.io";
  50. this.opts = opts;
  51. (0, engine_io_client_1.installTimerFunctions)(this, opts);
  52. this.reconnection(opts.reconnection !== false);
  53. this.reconnectionAttempts(opts.reconnectionAttempts || Infinity);
  54. this.reconnectionDelay(opts.reconnectionDelay || 1000);
  55. this.reconnectionDelayMax(opts.reconnectionDelayMax || 5000);
  56. this.randomizationFactor((_a = opts.randomizationFactor) !== null && _a !== void 0 ? _a : 0.5);
  57. this.backoff = new backo2_js_1.Backoff({
  58. min: this.reconnectionDelay(),
  59. max: this.reconnectionDelayMax(),
  60. jitter: this.randomizationFactor(),
  61. });
  62. this.timeout(null == opts.timeout ? 20000 : opts.timeout);
  63. this._readyState = "closed";
  64. this.uri = uri;
  65. const _parser = opts.parser || parser;
  66. this.encoder = new _parser.Encoder();
  67. this.decoder = new _parser.Decoder();
  68. this._autoConnect = opts.autoConnect !== false;
  69. if (this._autoConnect)
  70. this.open();
  71. }
  72. reconnection(v) {
  73. if (!arguments.length)
  74. return this._reconnection;
  75. this._reconnection = !!v;
  76. if (!v) {
  77. this.skipReconnect = true;
  78. }
  79. return this;
  80. }
  81. reconnectionAttempts(v) {
  82. if (v === undefined)
  83. return this._reconnectionAttempts;
  84. this._reconnectionAttempts = v;
  85. return this;
  86. }
  87. reconnectionDelay(v) {
  88. var _a;
  89. if (v === undefined)
  90. return this._reconnectionDelay;
  91. this._reconnectionDelay = v;
  92. (_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setMin(v);
  93. return this;
  94. }
  95. randomizationFactor(v) {
  96. var _a;
  97. if (v === undefined)
  98. return this._randomizationFactor;
  99. this._randomizationFactor = v;
  100. (_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setJitter(v);
  101. return this;
  102. }
  103. reconnectionDelayMax(v) {
  104. var _a;
  105. if (v === undefined)
  106. return this._reconnectionDelayMax;
  107. this._reconnectionDelayMax = v;
  108. (_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setMax(v);
  109. return this;
  110. }
  111. timeout(v) {
  112. if (!arguments.length)
  113. return this._timeout;
  114. this._timeout = v;
  115. return this;
  116. }
  117. /**
  118. * Starts trying to reconnect if reconnection is enabled and we have not
  119. * started reconnecting yet
  120. *
  121. * @private
  122. */
  123. maybeReconnectOnOpen() {
  124. // Only try to reconnect if it's the first time we're connecting
  125. if (!this._reconnecting &&
  126. this._reconnection &&
  127. this.backoff.attempts === 0) {
  128. // keeps reconnection from firing twice for the same reconnection loop
  129. this.reconnect();
  130. }
  131. }
  132. /**
  133. * Sets the current transport `socket`.
  134. *
  135. * @param {Function} fn - optional, callback
  136. * @return self
  137. * @public
  138. */
  139. open(fn) {
  140. debug("readyState %s", this._readyState);
  141. if (~this._readyState.indexOf("open"))
  142. return this;
  143. debug("opening %s", this.uri);
  144. this.engine = new engine_io_client_1.Socket(this.uri, this.opts);
  145. const socket = this.engine;
  146. const self = this;
  147. this._readyState = "opening";
  148. this.skipReconnect = false;
  149. // emit `open`
  150. const openSubDestroy = (0, on_js_1.on)(socket, "open", function () {
  151. self.onopen();
  152. fn && fn();
  153. });
  154. const onError = (err) => {
  155. debug("error");
  156. this.cleanup();
  157. this._readyState = "closed";
  158. this.emitReserved("error", err);
  159. if (fn) {
  160. fn(err);
  161. }
  162. else {
  163. // Only do this if there is no fn to handle the error
  164. this.maybeReconnectOnOpen();
  165. }
  166. };
  167. // emit `error`
  168. const errorSub = (0, on_js_1.on)(socket, "error", onError);
  169. if (false !== this._timeout) {
  170. const timeout = this._timeout;
  171. debug("connect attempt will timeout after %d", timeout);
  172. // set timer
  173. const timer = this.setTimeoutFn(() => {
  174. debug("connect attempt timed out after %d", timeout);
  175. openSubDestroy();
  176. onError(new Error("timeout"));
  177. socket.close();
  178. }, timeout);
  179. if (this.opts.autoUnref) {
  180. timer.unref();
  181. }
  182. this.subs.push(() => {
  183. this.clearTimeoutFn(timer);
  184. });
  185. }
  186. this.subs.push(openSubDestroy);
  187. this.subs.push(errorSub);
  188. return this;
  189. }
  190. /**
  191. * Alias for open()
  192. *
  193. * @return self
  194. * @public
  195. */
  196. connect(fn) {
  197. return this.open(fn);
  198. }
  199. /**
  200. * Called upon transport open.
  201. *
  202. * @private
  203. */
  204. onopen() {
  205. debug("open");
  206. // clear old subs
  207. this.cleanup();
  208. // mark as open
  209. this._readyState = "open";
  210. this.emitReserved("open");
  211. // add new subs
  212. const socket = this.engine;
  213. this.subs.push((0, on_js_1.on)(socket, "ping", this.onping.bind(this)), (0, on_js_1.on)(socket, "data", this.ondata.bind(this)), (0, on_js_1.on)(socket, "error", this.onerror.bind(this)), (0, on_js_1.on)(socket, "close", this.onclose.bind(this)),
  214. // @ts-ignore
  215. (0, on_js_1.on)(this.decoder, "decoded", this.ondecoded.bind(this)));
  216. }
  217. /**
  218. * Called upon a ping.
  219. *
  220. * @private
  221. */
  222. onping() {
  223. this.emitReserved("ping");
  224. }
  225. /**
  226. * Called with data.
  227. *
  228. * @private
  229. */
  230. ondata(data) {
  231. try {
  232. this.decoder.add(data);
  233. }
  234. catch (e) {
  235. this.onclose("parse error", e);
  236. }
  237. }
  238. /**
  239. * Called when parser fully decodes a packet.
  240. *
  241. * @private
  242. */
  243. ondecoded(packet) {
  244. // the nextTick call prevents an exception in a user-provided event listener from triggering a disconnection due to a "parse error"
  245. (0, engine_io_client_1.nextTick)(() => {
  246. this.emitReserved("packet", packet);
  247. }, this.setTimeoutFn);
  248. }
  249. /**
  250. * Called upon socket error.
  251. *
  252. * @private
  253. */
  254. onerror(err) {
  255. debug("error", err);
  256. this.emitReserved("error", err);
  257. }
  258. /**
  259. * Creates a new socket for the given `nsp`.
  260. *
  261. * @return {Socket}
  262. * @public
  263. */
  264. socket(nsp, opts) {
  265. let socket = this.nsps[nsp];
  266. if (!socket) {
  267. socket = new socket_js_1.Socket(this, nsp, opts);
  268. this.nsps[nsp] = socket;
  269. }
  270. else if (this._autoConnect && !socket.active) {
  271. socket.connect();
  272. }
  273. return socket;
  274. }
  275. /**
  276. * Called upon a socket close.
  277. *
  278. * @param socket
  279. * @private
  280. */
  281. _destroy(socket) {
  282. const nsps = Object.keys(this.nsps);
  283. for (const nsp of nsps) {
  284. const socket = this.nsps[nsp];
  285. if (socket.active) {
  286. debug("socket %s is still active, skipping close", nsp);
  287. return;
  288. }
  289. }
  290. this._close();
  291. }
  292. /**
  293. * Writes a packet.
  294. *
  295. * @param packet
  296. * @private
  297. */
  298. _packet(packet) {
  299. debug("writing packet %j", packet);
  300. const encodedPackets = this.encoder.encode(packet);
  301. for (let i = 0; i < encodedPackets.length; i++) {
  302. this.engine.write(encodedPackets[i], packet.options);
  303. }
  304. }
  305. /**
  306. * Clean up transport subscriptions and packet buffer.
  307. *
  308. * @private
  309. */
  310. cleanup() {
  311. debug("cleanup");
  312. this.subs.forEach((subDestroy) => subDestroy());
  313. this.subs.length = 0;
  314. this.decoder.destroy();
  315. }
  316. /**
  317. * Close the current socket.
  318. *
  319. * @private
  320. */
  321. _close() {
  322. debug("disconnect");
  323. this.skipReconnect = true;
  324. this._reconnecting = false;
  325. this.onclose("forced close");
  326. }
  327. /**
  328. * Alias for close()
  329. *
  330. * @private
  331. */
  332. disconnect() {
  333. return this._close();
  334. }
  335. /**
  336. * Called when:
  337. *
  338. * - the low-level engine is closed
  339. * - the parser encountered a badly formatted packet
  340. * - all sockets are disconnected
  341. *
  342. * @private
  343. */
  344. onclose(reason, description) {
  345. var _a;
  346. debug("closed due to %s", reason);
  347. this.cleanup();
  348. (_a = this.engine) === null || _a === void 0 ? void 0 : _a.close();
  349. this.backoff.reset();
  350. this._readyState = "closed";
  351. this.emitReserved("close", reason, description);
  352. if (this._reconnection && !this.skipReconnect) {
  353. this.reconnect();
  354. }
  355. }
  356. /**
  357. * Attempt a reconnection.
  358. *
  359. * @private
  360. */
  361. reconnect() {
  362. if (this._reconnecting || this.skipReconnect)
  363. return this;
  364. const self = this;
  365. if (this.backoff.attempts >= this._reconnectionAttempts) {
  366. debug("reconnect failed");
  367. this.backoff.reset();
  368. this.emitReserved("reconnect_failed");
  369. this._reconnecting = false;
  370. }
  371. else {
  372. const delay = this.backoff.duration();
  373. debug("will wait %dms before reconnect attempt", delay);
  374. this._reconnecting = true;
  375. const timer = this.setTimeoutFn(() => {
  376. if (self.skipReconnect)
  377. return;
  378. debug("attempting reconnect");
  379. this.emitReserved("reconnect_attempt", self.backoff.attempts);
  380. // check again for the case socket closed in above events
  381. if (self.skipReconnect)
  382. return;
  383. self.open((err) => {
  384. if (err) {
  385. debug("reconnect attempt error");
  386. self._reconnecting = false;
  387. self.reconnect();
  388. this.emitReserved("reconnect_error", err);
  389. }
  390. else {
  391. debug("reconnect success");
  392. self.onreconnect();
  393. }
  394. });
  395. }, delay);
  396. if (this.opts.autoUnref) {
  397. timer.unref();
  398. }
  399. this.subs.push(() => {
  400. this.clearTimeoutFn(timer);
  401. });
  402. }
  403. }
  404. /**
  405. * Called upon successful reconnect.
  406. *
  407. * @private
  408. */
  409. onreconnect() {
  410. const attempt = this.backoff.attempts;
  411. this._reconnecting = false;
  412. this.backoff.reset();
  413. this.emitReserved("reconnect", attempt);
  414. }
  415. }
  416. exports.Manager = Manager;