| 447 | } else if ( |
| 448 | typeof this.options.subscribe === 'string' || |
| 449 | Array.isArray(this.options.subscribe) |
| 450 | ) { |
| 451 | this.logger?.debug('Subscribing to initial events from options:', this.options.subscribe); |
| 452 | const subscriptions = await this.getSubscriptionsFromEventStrings(this.options.subscribe); |
| 453 | this.logger?.debug('Parsed subscriptions from options:', subscriptions); |
| 454 | if (subscriptions) { |
| 455 | await this.subscribe(subscriptions); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | // Subscribe to any events from listeners added with .on |
| 460 | if (this.listeners.length) { |
| 461 | const subscriptions = await this.getSubscriptionsFromListeners(); |
| 462 | await this.subscribe(subscriptions); |
| 463 | } |
| 464 | |
| 465 | this.logger?.verbose('Subscribed to requested events', this.subscriptions, this.listeners); |
| 466 | } catch (e) { |
| 467 | this.logger?.warn('Error subscribing to requested events', e); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | protected onClose(event: CloseEvent): void { |
| 472 | this._connectController.abort(); |
| 473 | |
| 474 | try { |
| 475 | if ((event.type === 'error' || !event.wasClean) && this.options.onError) |
| 476 | this?.options?.onError(new Error(getCloseEventReason(event))); |
| 477 | this?.options?.onDisconnect?.(); |
| 478 | } catch (e) { |
| 479 | this.logger?.warn('Error invoking user-provided onDisconnect handler', e); |
| 480 | } |
| 481 | |
| 482 | // No auto-reconnect, clean up |
| 483 | if (this._explicitlyClosed || !this.options.autoReconnect) { |
| 484 | this.logger?.debug('Cleaning up...'); |
| 485 | return this.cleanup(); |
| 486 | } |
| 487 | |
| 488 | // Handle auto-reconnect |
| 489 | this._retried += 1; |
| 490 | if ( |
| 491 | typeof this.options.retries === 'number' && |
| 492 | (this.options.retries < 0 || this._retried < this.options.retries) |
| 493 | ) { |
| 494 | if (this._reconnectTimeout) clearTimeout(this._reconnectTimeout); |
| 495 | this._reconnectTimeout = setTimeout( |
| 496 | async () => { |
| 497 | if (!!this.socket && this.socket.readyState !== this.socket.CLOSED) return; |
| 498 | this.logger?.debug(`Reconnecting... (attempt ${this._retried})`); |