private async startConnectingInternal()

in src/messagingsession/DefaultMessagingSession.ts [121:188]


  private async startConnectingInternal(reconnecting: boolean): Promise<void> {
    let endpointUrl = this.configuration.endpointUrl;

    // Moving this reconnect logic can potentially result into an infinite reconnect loop on errors.
    // Check https://github.com/aws/amazon-chime-sdk-js/issues/2372 for details.
    if (!reconnecting) {
      this.reconnectController.reset();
    }
    if (this.reconnectController.hasStartedConnectionAttempt()) {
      this.reconnectController.startedConnectionAttempt(false);
    } else {
      this.reconnectController.startedConnectionAttempt(true);
    }
    // reconnect needs to re-resolve endpoint url, which will also refresh credentials on client if they are expired
    if (reconnecting || endpointUrl === undefined) {
      try {
        if (this.configuration.chimeClient.getMessagingSessionEndpoint instanceof Function) {
          const response = await this.configuration.chimeClient.getMessagingSessionEndpoint();
          // Check for aws sdk v3 with v2 style compatibility first
          if (response.Endpoint?.Url) {
            endpointUrl = response.Endpoint.Url;
          } else {
            // Make aws sdk v2 call
            const endpoint = await this.configuration.chimeClient
              .getMessagingSessionEndpoint()
              .promise();
            endpointUrl = endpoint.Endpoint.Url;
          }
        } else {
          endpointUrl = (
            await this.configuration.chimeClient.send(new GetMessagingSessionEndpointCommand({}))
          ).Endpoint.Url;
        }
        this.logger.debug(`Messaging endpoint resolved to: ${endpointUrl}`);
      } catch (e) {
        // send artificial close code event so the
        // re-connect logic of underlying websocket client is
        // triggered in the close handler
        this.logger.error(`Messaging Session failed to resolve endpoint: ${e}`);
        const closeEvent = new CloseEvent('close', {
          wasClean: false,
          code: 4999,
          reason: 'Failed to get messaging session endpoint URL',
          bubbles: false,
        });
        this.closeEventHandler(closeEvent);
        return;
      }
    }

    const signedUrl = await this.prepareWebSocketUrl(endpointUrl);
    this.logger.info(`opening connection to ${signedUrl}`);
    if (!reconnecting) {
      this.reconnectController.reset();
    }
    if (this.reconnectController.hasStartedConnectionAttempt()) {
      this.reconnectController.startedConnectionAttempt(false);
    } else {
      this.reconnectController.startedConnectionAttempt(true);
    }
    this.webSocket.create(signedUrl, [], true);
    this.forEachObserver(observer => {
      if (observer.messagingSessionDidStartConnecting) {
        observer.messagingSessionDidStartConnecting(reconnecting);
      }
    });
    this.setUpEventListeners();
  }