@ipcom/asterisk-ari 0.0.34 → 0.0.36

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.
@@ -1724,19 +1724,14 @@ var AriClient = class {
1724
1724
  "The 'app' parameter is required to connect to the WebSocket."
1725
1725
  );
1726
1726
  }
1727
+ if (this.isReconnecting) {
1728
+ console.warn("Already attempting to reconnect. Skipping this attempt.");
1729
+ return;
1730
+ }
1731
+ this.isReconnecting = true;
1727
1732
  const eventsParam = subscribedEvents && subscribedEvents.length > 0 ? `&event=${subscribedEvents.join(",")}` : "&subscribeAll=true";
1728
1733
  const protocol = this.config.secure ? "wss" : "ws";
1729
- const wsUrl = `${protocol}://${encodeURIComponent(
1730
- this.config.username
1731
- )}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
1732
- if (this.wsConnections.has(app)) {
1733
- const existingWsClient = this.wsConnections.get(app);
1734
- console.log(`WebSocket j\xE1 conectado para o app: ${app}. Reiniciando...`);
1735
- existingWsClient?.removeAllListeners();
1736
- existingWsClient?.close();
1737
- }
1738
- const wsClient = new WebSocketClient(wsUrl);
1739
- this.wsConnections.set(app, wsClient);
1734
+ const wsUrl = `${protocol}://${encodeURIComponent(this.config.username)}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
1740
1735
  const backoffOptions = {
1741
1736
  delayFirstAttempt: false,
1742
1737
  startingDelay: 1e3,
@@ -1746,27 +1741,39 @@ var AriClient = class {
1746
1741
  jitter: "full",
1747
1742
  retry: (error, attemptNumber) => {
1748
1743
  console.warn(`Tentativa ${attemptNumber} falhou: ${error.message}`);
1749
- return !wsClient.isConnected();
1744
+ return !this.wsClient?.isConnected();
1750
1745
  }
1751
1746
  };
1747
+ if (this.wsClient?.isConnected()) {
1748
+ console.log("WebSocket j\xE1 conectado. Removendo listeners antigos...");
1749
+ this.wsClient.removeAllListeners();
1750
+ this.wsClient.close();
1751
+ }
1752
+ if (!this.wsClient) {
1753
+ console.log("Criando nova inst\xE2ncia do WebSocketClient...");
1754
+ this.wsClient = new WebSocketClient(wsUrl);
1755
+ }
1752
1756
  try {
1753
1757
  await (0, import_exponential_backoff.backOff)(async () => {
1754
1758
  if (!this.wsClient) {
1755
1759
  throw new Error("WebSocketClient instance is null.");
1756
1760
  }
1757
- await this.wsClient.connect();
1758
- this.isWebSocketConnectedFlag = true;
1759
- this.integrateWebSocketEvents(app, this.wsClient);
1760
- console.log(`WebSocket conectado para o app: ${app}`);
1761
- await this.ensureAppRegistered(app);
1761
+ if (!this.wsClient.isConnected()) {
1762
+ console.log("Tentando conectar ao WebSocket...");
1763
+ await this.wsClient.connect();
1764
+ }
1762
1765
  }, backoffOptions);
1766
+ this.integrateWebSocketEvents(app, this.wsClient);
1767
+ console.log(`WebSocket conectado para o app: ${app}`);
1768
+ await this.ensureAppRegistered(app);
1763
1769
  } catch (err) {
1764
- this.isWebSocketConnectedFlag = false;
1765
1770
  console.error(
1766
1771
  "N\xE3o foi poss\xEDvel conectar ao WebSocket ap\xF3s m\xFAltiplas tentativas:",
1767
1772
  err
1768
1773
  );
1769
1774
  throw err;
1775
+ } finally {
1776
+ this.isReconnecting = false;
1770
1777
  }
1771
1778
  }
1772
1779
  /**
@@ -1926,13 +1933,18 @@ var AriClient = class {
1926
1933
  * @throws {Error} Throws an error if the WebSocket client is not connected when this method is called.
1927
1934
  * @returns {void} This function doesn't return a value.
1928
1935
  */
1929
- async onWebSocketEvent(event, callback) {
1930
- if (!this.isWebSocketConnectedFlag) {
1931
- throw new Error("WebSocket ainda n\xE3o est\xE1 conectado.");
1932
- }
1936
+ onWebSocketEvent(event, callback) {
1933
1937
  if (!this.wsClient) {
1934
- throw new Error("WebSocket n\xE3o est\xE1 configurado.");
1938
+ console.error("WebSocket n\xE3o est\xE1 configurado.");
1939
+ return;
1940
+ }
1941
+ if (!this.isWebSocketConnectedFlag) {
1942
+ console.warn(
1943
+ "WebSocket ainda n\xE3o est\xE1 conectado. Tentando reconectar..."
1944
+ );
1945
+ return;
1935
1946
  }
1947
+ console.log(`Registrando listener para evento: ${event}`);
1936
1948
  this.wsClient.on(event, callback);
1937
1949
  }
1938
1950
  /**