@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.
package/dist/esm/index.js CHANGED
@@ -1677,6 +1677,7 @@ var AriClient = class {
1677
1677
  isReconnecting = false;
1678
1678
  eventEmitter = new EventEmitter4();
1679
1679
  wsConnections = /* @__PURE__ */ new Map();
1680
+ isWebSocketConnectedFlag = false;
1680
1681
  channels;
1681
1682
  endpoints;
1682
1683
  applications;
@@ -1701,19 +1702,14 @@ var AriClient = class {
1701
1702
  "The 'app' parameter is required to connect to the WebSocket."
1702
1703
  );
1703
1704
  }
1705
+ if (this.isReconnecting) {
1706
+ console.warn("Already attempting to reconnect. Skipping this attempt.");
1707
+ return;
1708
+ }
1709
+ this.isReconnecting = true;
1704
1710
  const eventsParam = subscribedEvents && subscribedEvents.length > 0 ? `&event=${subscribedEvents.join(",")}` : "&subscribeAll=true";
1705
1711
  const protocol = this.config.secure ? "wss" : "ws";
1706
- const wsUrl = `${protocol}://${encodeURIComponent(
1707
- this.config.username
1708
- )}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
1709
- if (this.wsConnections.has(app)) {
1710
- const existingWsClient = this.wsConnections.get(app);
1711
- console.log(`WebSocket j\xE1 conectado para o app: ${app}. Reiniciando...`);
1712
- existingWsClient?.removeAllListeners();
1713
- existingWsClient?.close();
1714
- }
1715
- const wsClient = new WebSocketClient(wsUrl);
1716
- 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}`;
1717
1713
  const backoffOptions = {
1718
1714
  delayFirstAttempt: false,
1719
1715
  startingDelay: 1e3,
@@ -1723,23 +1719,39 @@ var AriClient = class {
1723
1719
  jitter: "full",
1724
1720
  retry: (error, attemptNumber) => {
1725
1721
  console.warn(`Tentativa ${attemptNumber} falhou: ${error.message}`);
1726
- return !wsClient.isConnected();
1722
+ return !this.wsClient?.isConnected();
1727
1723
  }
1728
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
+ }
1729
1734
  try {
1730
1735
  await (0, import_exponential_backoff.backOff)(async () => {
1731
- await wsClient.connect();
1732
- this.integrateWebSocketEvents(app, wsClient);
1733
- console.log(`WebSocket conectado para o app: ${app}`);
1734
- await this.ensureAppRegistered(app);
1736
+ if (!this.wsClient) {
1737
+ throw new Error("WebSocketClient instance is null.");
1738
+ }
1739
+ if (!this.wsClient.isConnected()) {
1740
+ console.log("Tentando conectar ao WebSocket...");
1741
+ await this.wsClient.connect();
1742
+ }
1735
1743
  }, backoffOptions);
1744
+ this.integrateWebSocketEvents(app, this.wsClient);
1745
+ console.log(`WebSocket conectado para o app: ${app}`);
1746
+ await this.ensureAppRegistered(app);
1736
1747
  } catch (err) {
1737
1748
  console.error(
1738
- `N\xE3o foi poss\xEDvel conectar ao WebSocket para o app ${app}:`,
1749
+ "N\xE3o foi poss\xEDvel conectar ao WebSocket ap\xF3s m\xFAltiplas tentativas:",
1739
1750
  err
1740
1751
  );
1741
- this.wsConnections.delete(app);
1742
1752
  throw err;
1753
+ } finally {
1754
+ this.isReconnecting = false;
1743
1755
  }
1744
1756
  }
1745
1757
  /**
@@ -1899,9 +1911,12 @@ var AriClient = class {
1899
1911
  * @throws {Error} Throws an error if the WebSocket client is not connected when this method is called.
1900
1912
  * @returns {void} This function doesn't return a value.
1901
1913
  */
1902
- onWebSocketEvent(event, callback) {
1914
+ async onWebSocketEvent(event, callback) {
1915
+ if (!this.isWebSocketConnectedFlag) {
1916
+ throw new Error("WebSocket ainda n\xE3o est\xE1 conectado.");
1917
+ }
1903
1918
  if (!this.wsClient) {
1904
- throw new Error("WebSocket n\xE3o est\xE1 conectado.");
1919
+ throw new Error("WebSocket n\xE3o est\xE1 configurado.");
1905
1920
  }
1906
1921
  this.wsClient.on(event, callback);
1907
1922
  }