| import { Socket as Engine, installTimerFunctions, nextTick, } from "engine.io-client"; |
| import { Socket } from "./socket.js"; |
| import * as parser from "socket.io-parser"; |
| import { on } from "./on.js"; |
| import { Backoff } from "./contrib/backo2.js"; |
| import { Emitter, } from "@socket.io/component-emitter"; |
| export class Manager extends Emitter { |
| constructor(uri, opts) { |
| var _a; |
| super(); |
| this.nsps = {}; |
| this.subs = []; |
| if (uri && "object" === typeof uri) { |
| opts = uri; |
| uri = undefined; |
| } |
| opts = opts || {}; |
| opts.path = opts.path || "/socket.io"; |
| this.opts = opts; |
| installTimerFunctions(this, opts); |
| this.reconnection(opts.reconnection !== false); |
| this.reconnectionAttempts(opts.reconnectionAttempts || Infinity); |
| this.reconnectionDelay(opts.reconnectionDelay || 1000); |
| this.reconnectionDelayMax(opts.reconnectionDelayMax || 5000); |
| this.randomizationFactor((_a = opts.randomizationFactor) !== null && _a !== void 0 ? _a : 0.5); |
| this.backoff = new Backoff({ |
| min: this.reconnectionDelay(), |
| max: this.reconnectionDelayMax(), |
| jitter: this.randomizationFactor(), |
| }); |
| this.timeout(null == opts.timeout ? 20000 : opts.timeout); |
| this._readyState = "closed"; |
| this.uri = uri; |
| const _parser = opts.parser || parser; |
| this.encoder = new _parser.Encoder(); |
| this.decoder = new _parser.Decoder(); |
| this._autoConnect = opts.autoConnect !== false; |
| if (this._autoConnect) |
| this.open(); |
| } |
| reconnection(v) { |
| if (!arguments.length) |
| return this._reconnection; |
| this._reconnection = !!v; |
| if (!v) { |
| this.skipReconnect = true; |
| } |
| return this; |
| } |
| reconnectionAttempts(v) { |
| if (v === undefined) |
| return this._reconnectionAttempts; |
| this._reconnectionAttempts = v; |
| return this; |
| } |
| reconnectionDelay(v) { |
| var _a; |
| if (v === undefined) |
| return this._reconnectionDelay; |
| this._reconnectionDelay = v; |
| (_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setMin(v); |
| return this; |
| } |
| randomizationFactor(v) { |
| var _a; |
| if (v === undefined) |
| return this._randomizationFactor; |
| this._randomizationFactor = v; |
| (_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setJitter(v); |
| return this; |
| } |
| reconnectionDelayMax(v) { |
| var _a; |
| if (v === undefined) |
| return this._reconnectionDelayMax; |
| this._reconnectionDelayMax = v; |
| (_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setMax(v); |
| return this; |
| } |
| timeout(v) { |
| if (!arguments.length) |
| return this._timeout; |
| this._timeout = v; |
| return this; |
| } |
| |
| |
| |
| |
| |
| |
| maybeReconnectOnOpen() { |
| |
| if (!this._reconnecting && |
| this._reconnection && |
| this.backoff.attempts === 0) { |
| |
| this.reconnect(); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| open(fn) { |
| if (~this._readyState.indexOf("open")) |
| return this; |
| this.engine = new Engine(this.uri, this.opts); |
| const socket = this.engine; |
| const self = this; |
| this._readyState = "opening"; |
| this.skipReconnect = false; |
| |
| const openSubDestroy = on(socket, "open", function () { |
| self.onopen(); |
| fn && fn(); |
| }); |
| const onError = (err) => { |
| this.cleanup(); |
| this._readyState = "closed"; |
| this.emitReserved("error", err); |
| if (fn) { |
| fn(err); |
| } |
| else { |
| |
| this.maybeReconnectOnOpen(); |
| } |
| }; |
| |
| const errorSub = on(socket, "error", onError); |
| if (false !== this._timeout) { |
| const timeout = this._timeout; |
| |
| const timer = this.setTimeoutFn(() => { |
| openSubDestroy(); |
| onError(new Error("timeout")); |
| socket.close(); |
| }, timeout); |
| if (this.opts.autoUnref) { |
| timer.unref(); |
| } |
| this.subs.push(() => { |
| this.clearTimeoutFn(timer); |
| }); |
| } |
| this.subs.push(openSubDestroy); |
| this.subs.push(errorSub); |
| return this; |
| } |
| |
| |
| |
| |
| |
| |
| connect(fn) { |
| return this.open(fn); |
| } |
| |
| |
| |
| |
| |
| onopen() { |
| |
| this.cleanup(); |
| |
| this._readyState = "open"; |
| this.emitReserved("open"); |
| |
| const socket = this.engine; |
| this.subs.push(on(socket, "ping", this.onping.bind(this)), on(socket, "data", this.ondata.bind(this)), on(socket, "error", this.onerror.bind(this)), on(socket, "close", this.onclose.bind(this)), |
| |
| on(this.decoder, "decoded", this.ondecoded.bind(this))); |
| } |
| |
| |
| |
| |
| |
| onping() { |
| this.emitReserved("ping"); |
| } |
| |
| |
| |
| |
| |
| ondata(data) { |
| try { |
| this.decoder.add(data); |
| } |
| catch (e) { |
| this.onclose("parse error", e); |
| } |
| } |
| |
| |
| |
| |
| |
| ondecoded(packet) { |
| |
| nextTick(() => { |
| this.emitReserved("packet", packet); |
| }, this.setTimeoutFn); |
| } |
| |
| |
| |
| |
| |
| onerror(err) { |
| this.emitReserved("error", err); |
| } |
| |
| |
| |
| |
| |
| |
| socket(nsp, opts) { |
| let socket = this.nsps[nsp]; |
| if (!socket) { |
| socket = new Socket(this, nsp, opts); |
| this.nsps[nsp] = socket; |
| } |
| else if (this._autoConnect && !socket.active) { |
| socket.connect(); |
| } |
| return socket; |
| } |
| |
| |
| |
| |
| |
| |
| _destroy(socket) { |
| const nsps = Object.keys(this.nsps); |
| for (const nsp of nsps) { |
| const socket = this.nsps[nsp]; |
| if (socket.active) { |
| return; |
| } |
| } |
| this._close(); |
| } |
| |
| |
| |
| |
| |
| |
| _packet(packet) { |
| const encodedPackets = this.encoder.encode(packet); |
| for (let i = 0; i < encodedPackets.length; i++) { |
| this.engine.write(encodedPackets[i], packet.options); |
| } |
| } |
| |
| |
| |
| |
| |
| cleanup() { |
| this.subs.forEach((subDestroy) => subDestroy()); |
| this.subs.length = 0; |
| this.decoder.destroy(); |
| } |
| |
| |
| |
| |
| |
| _close() { |
| this.skipReconnect = true; |
| this._reconnecting = false; |
| this.onclose("forced close"); |
| } |
| |
| |
| |
| |
| |
| disconnect() { |
| return this._close(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| onclose(reason, description) { |
| var _a; |
| this.cleanup(); |
| (_a = this.engine) === null || _a === void 0 ? void 0 : _a.close(); |
| this.backoff.reset(); |
| this._readyState = "closed"; |
| this.emitReserved("close", reason, description); |
| if (this._reconnection && !this.skipReconnect) { |
| this.reconnect(); |
| } |
| } |
| |
| |
| |
| |
| |
| reconnect() { |
| if (this._reconnecting || this.skipReconnect) |
| return this; |
| const self = this; |
| if (this.backoff.attempts >= this._reconnectionAttempts) { |
| this.backoff.reset(); |
| this.emitReserved("reconnect_failed"); |
| this._reconnecting = false; |
| } |
| else { |
| const delay = this.backoff.duration(); |
| this._reconnecting = true; |
| const timer = this.setTimeoutFn(() => { |
| if (self.skipReconnect) |
| return; |
| this.emitReserved("reconnect_attempt", self.backoff.attempts); |
| |
| if (self.skipReconnect) |
| return; |
| self.open((err) => { |
| if (err) { |
| self._reconnecting = false; |
| self.reconnect(); |
| this.emitReserved("reconnect_error", err); |
| } |
| else { |
| self.onreconnect(); |
| } |
| }); |
| }, delay); |
| if (this.opts.autoUnref) { |
| timer.unref(); |
| } |
| this.subs.push(() => { |
| this.clearTimeoutFn(timer); |
| }); |
| } |
| } |
| |
| |
| |
| |
| |
| onreconnect() { |
| const attempt = this.backoff.attempts; |
| this._reconnecting = false; |
| this.backoff.reset(); |
| this.emitReserved("reconnect", attempt); |
| } |
| } |
|
|