@ipcom/asterisk-ari 0.0.34 → 0.0.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -1702,19 +1702,14 @@ var AriClient = class {
1702
1702
  "The 'app' parameter is required to connect to the WebSocket."
1703
1703
  );
1704
1704
  }
1705
+ if (this.isReconnecting) {
1706
+ console.warn("Already attempting to reconnect. Skipping this attempt.");
1707
+ return;
1708
+ }
1709
+ this.isReconnecting = true;
1705
1710
  const eventsParam = subscribedEvents && subscribedEvents.length > 0 ? `&event=${subscribedEvents.join(",")}` : "&subscribeAll=true";
1706
1711
  const protocol = this.config.secure ? "wss" : "ws";
1707
- const wsUrl = `${protocol}://${encodeURIComponent(
1708
- this.config.username
1709
- )}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
1710
- if (this.wsConnections.has(app)) {
1711
- const existingWsClient = this.wsConnections.get(app);
1712
- console.log(`WebSocket j\xE1 conectado para o app: ${app}. Reiniciando...`);
1713
- existingWsClient?.removeAllListeners();
1714
- existingWsClient?.close();
1715
- }
1716
- const wsClient = new WebSocketClient(wsUrl);
1717
- this.wsConnections.set(app, wsClient);
1712
+ const wsUrl = `${protocol}://${encodeURIComponent(this.config.username)}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
1718
1713
  const backoffOptions = {
1719
1714
  delayFirstAttempt: false,
1720
1715
  startingDelay: 1e3,
@@ -1724,27 +1719,39 @@ var AriClient = class {
1724
1719
  jitter: "full",
1725
1720
  retry: (error, attemptNumber) => {
1726
1721
  console.warn(`Tentativa ${attemptNumber} falhou: ${error.message}`);
1727
- return !wsClient.isConnected();
1722
+ return !this.wsClient?.isConnected();
1728
1723
  }
1729
1724
  };
1725
+ if (this.wsClient?.isConnected()) {
1726
+ console.log("WebSocket j\xE1 conectado. Removendo listeners antigos...");
1727
+ this.wsClient.removeAllListeners();
1728
+ this.wsClient.close();
1729
+ }
1730
+ if (!this.wsClient) {
1731
+ console.log("Criando nova inst\xE2ncia do WebSocketClient...");
1732
+ this.wsClient = new WebSocketClient(wsUrl);
1733
+ }
1730
1734
  try {
1731
1735
  await (0, import_exponential_backoff.backOff)(async () => {
1732
1736
  if (!this.wsClient) {
1733
1737
  throw new Error("WebSocketClient instance is null.");
1734
1738
  }
1735
- await this.wsClient.connect();
1736
- this.isWebSocketConnectedFlag = true;
1737
- this.integrateWebSocketEvents(app, this.wsClient);
1738
- console.log(`WebSocket conectado para o app: ${app}`);
1739
- await this.ensureAppRegistered(app);
1739
+ if (!this.wsClient.isConnected()) {
1740
+ console.log("Tentando conectar ao WebSocket...");
1741
+ await this.wsClient.connect();
1742
+ }
1740
1743
  }, backoffOptions);
1744
+ this.integrateWebSocketEvents(app, this.wsClient);
1745
+ console.log(`WebSocket conectado para o app: ${app}`);
1746
+ await this.ensureAppRegistered(app);
1741
1747
  } catch (err) {
1742
- this.isWebSocketConnectedFlag = false;
1743
1748
  console.error(
1744
1749
  "N\xE3o foi poss\xEDvel conectar ao WebSocket ap\xF3s m\xFAltiplas tentativas:",
1745
1750
  err
1746
1751
  );
1747
1752
  throw err;
1753
+ } finally {
1754
+ this.isReconnecting = false;
1748
1755
  }
1749
1756
  }
1750
1757
  /**