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