| 407 | throw new Error('Authentication failed'); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | protected async onOpen(): Promise<void> { |
| 412 | this._retried = 0; |
| 413 | if (this._reconnectTimeout) clearTimeout(this._reconnectTimeout); |
| 414 | |
| 415 | try { |
| 416 | // Force a getInfo call for backwards compat with Streamer.bot v0.2.4 and older |
| 417 | if (!this._authEnabled) { |
| 418 | void this.getInfo().catch(() => |
| 419 | this.logger?.debug('Failed to fetch Streamer.bot instance info'), |
| 420 | ); |
| 421 | } |
| 422 | await this.handshake(); |
| 423 | |
| 424 | if (this.version && this.info) { |
| 425 | this.logger?.debug(`Connected to Streamer.bot: v${this.version} (${this.info.name})`); |
| 426 | await this.updateSupportedEvents(); |
| 427 | this?.options?.onConnect?.(this.info); |
| 428 | } |
| 429 | } catch (err) { |
| 430 | this.logger?.warn('Failed handshake with Streamer.bot', err); |
| 431 | this.options?.onError?.( |
| 432 | err instanceof Error ? err : new Error('Failed handshake with Streamer.bot'), |
| 433 | ); |
| 434 | return await this.disconnect(); |
| 435 | } |
| 436 | |
| 437 | try { |
| 438 | // Subscribe to initial subscriptions requested in client options |
| 439 | if ( |
| 440 | this.options.subscribe === '*' || |
| 441 | (typeof this.options.subscribe === 'object' && |
| 442 | !Array.isArray(this.options.subscribe) && |
| 443 | Object.keys(this.options.subscribe ?? {}).length) |
| 444 | ) { |
| 445 | this.logger?.debug('Subscribing to initial events from options:', this.options.subscribe); |
| 446 | await this.subscribe(this.options.subscribe); |
| 447 | } else if ( |
| 448 | typeof this.options.subscribe === 'string' || |
| 449 | Array.isArray(this.options.subscribe) |
| 450 | ) { |