UNPKG

ps2census

Version:

Client to connect to the PS2 Event Stream websocket.

102 lines 4.19 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.StreamManager = void 0; const stream_client_1 = require("../stream/stream.client"); const stream_handler_1 = require("./stream.handler"); const duplicate_filter_1 = require("./utils/duplicate-filter"); const subscription_manager_1 = require("./subscription.manager"); const command_handler_1 = require("./command.handler"); const service_id_reject_exception_1 = require("./exceptions/service-id-reject.exception"); const next_tick_1 = __importDefault(require("../utils/next-tick")); class StreamManager { constructor(client, options = {}) { this.client = client; this.isStarted = false; this.stream = new stream_client_1.StreamClient(this.client.serviceId, this.client.environment, options); this.streamHandler = new stream_handler_1.StreamHandler(this.client, this.stream, new duplicate_filter_1.DuplicateFilter()); this.commandHandler = new command_handler_1.CommandHandler(this.stream); this.subscriptionManager = new subscription_manager_1.SubscriptionManager(this.client, this.stream, this.commandHandler, options.subscription); this.reconnectDelay = options.reconnectDelay ?? 2000; this.prepareEventStream(); } prepareEventStream() { this.stream.on('close', (code, reason) => { if (!this.isStarted) { (0, next_tick_1.default)(() => this.client.emit('disconnected', code, reason)); return; } (0, next_tick_1.default)(() => this.client.emit('reconnecting')); void this.reconnect(); }); this.stream.on('destroyed', () => { if (!this.isStarted) return; (0, next_tick_1.default)(() => this.client.emit('reconnecting')); void this.reconnect(); }); this.stream.on('error', error => { this.client.emit('error', error); }); this.stream.on('warn', error => { this.client.emit('warn', error); }); this.stream.on('debug', info => { this.client.emit('debug', info); }); } async connect() { if (this.isStarted) return; this.isStarted = true; const ready = () => { (0, next_tick_1.default)(() => this.client.emit('ready')); }; this.stream.once('ready', ready); try { await this.stream.connect(); } catch (e) { this.stream.off('ready', ready); this.isStarted = false; if ([403].includes(e.httpState)) { throw new service_id_reject_exception_1.ServiceIdRejectException(`Service ID rejected.`); } else { throw e; } } } disconnect() { if (!this.isStarted) return; this.isStarted = false; (0, next_tick_1.default)(() => this.client.emit('debug', `Manager disconnected.`)); if (this.reconnectTimeout) { clearTimeout(this.reconnectTimeout); delete this.reconnectTimeout; } this.stream.destroy({ code: 1000, emit: false }); } async reconnect() { if (this.reconnectTimeout) clearTimeout(this.reconnectTimeout); this.reconnectTimeout = setTimeout(async () => { try { await this.stream.connect(); } catch (e) { if ([403].includes(e.httpState)) { (0, next_tick_1.default)(() => this.client.emit('error', new Error(`Service ID rejected while trying to reconnect.`))); this.disconnect(); return; } (0, next_tick_1.default)(() => this.client.emit('debug', `Reconnect failed, trying again in ${this.reconnectDelay}ms.`)); } }, this.reconnectDelay); } } exports.StreamManager = StreamManager; //# sourceMappingURL=stream.manager.js.map