@ipcom/asterisk-ari 0.0.32 → 0.0.33

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
@@ -1676,6 +1676,7 @@ var AriClient = class {
1676
1676
  baseClient;
1677
1677
  isReconnecting = false;
1678
1678
  eventEmitter = new EventEmitter4();
1679
+ wsConnections = /* @__PURE__ */ new Map();
1679
1680
  channels;
1680
1681
  endpoints;
1681
1682
  applications;
@@ -1700,14 +1701,19 @@ var AriClient = class {
1700
1701
  "The 'app' parameter is required to connect to the WebSocket."
1701
1702
  );
1702
1703
  }
1703
- if (this.isReconnecting) {
1704
- console.warn("Already attempting to reconnect. Skipping this attempt.");
1705
- return;
1706
- }
1707
- this.isReconnecting = true;
1708
1704
  const eventsParam = subscribedEvents && subscribedEvents.length > 0 ? `&event=${subscribedEvents.join(",")}` : "&subscribeAll=true";
1709
1705
  const protocol = this.config.secure ? "wss" : "ws";
1710
- const wsUrl = `${protocol}://${encodeURIComponent(this.config.username)}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
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);
1711
1717
  const backoffOptions = {
1712
1718
  delayFirstAttempt: false,
1713
1719
  startingDelay: 1e3,
@@ -1717,77 +1723,83 @@ var AriClient = class {
1717
1723
  jitter: "full",
1718
1724
  retry: (error, attemptNumber) => {
1719
1725
  console.warn(`Tentativa ${attemptNumber} falhou: ${error.message}`);
1720
- return !this.wsClient?.isConnected();
1726
+ return !wsClient.isConnected();
1721
1727
  }
1722
1728
  };
1723
- if (this.wsClient?.isConnected()) {
1724
- console.log("WebSocket j\xE1 conectado. Removendo listeners antigos...");
1725
- this.wsClient.removeAllListeners();
1726
- this.wsClient.close();
1727
- }
1728
- this.wsClient = new WebSocketClient(wsUrl);
1729
1729
  try {
1730
1730
  await (0, import_exponential_backoff.backOff)(async () => {
1731
- if (!this.wsClient) {
1732
- throw new Error("WebSocketClient instance is null.");
1733
- }
1734
- await this.wsClient.connect();
1735
- this.integrateWebSocketEvents();
1731
+ await wsClient.connect();
1732
+ this.integrateWebSocketEvents(app, wsClient);
1736
1733
  console.log(`WebSocket conectado para o app: ${app}`);
1737
1734
  await this.ensureAppRegistered(app);
1738
1735
  }, backoffOptions);
1739
1736
  } catch (err) {
1740
1737
  console.error(
1741
- "N\xE3o foi poss\xEDvel conectar ao WebSocket ap\xF3s m\xFAltiplas tentativas:",
1738
+ `N\xE3o foi poss\xEDvel conectar ao WebSocket para o app ${app}:`,
1742
1739
  err
1743
1740
  );
1741
+ this.wsConnections.delete(app);
1744
1742
  throw err;
1745
- } finally {
1746
- this.isReconnecting = false;
1747
1743
  }
1748
1744
  }
1749
1745
  /**
1750
1746
  * Integrates WebSocket events with playback listeners.
1751
1747
  */
1752
- integrateWebSocketEvents() {
1753
- if (!this.wsClient) {
1754
- throw new Error("WebSocket client n\xE3o est\xE1 conectado.");
1748
+ integrateWebSocketEvents(app, wsClient) {
1749
+ if (!wsClient) {
1750
+ throw new Error(
1751
+ `WebSocket client para o app '${app}' n\xE3o est\xE1 conectado.`
1752
+ );
1755
1753
  }
1756
1754
  const eventHandlers = {
1757
1755
  PlaybackFinished: (data) => {
1758
1756
  if ("playbackId" in data) {
1759
1757
  this.playbacks.emitPlaybackEvent("PlaybackFinished", data);
1760
1758
  }
1759
+ console.log(`[${app}] PlaybackFinished:`, data);
1761
1760
  this.emitGlobalEvent(data);
1762
1761
  },
1763
1762
  ChannelStateChange: (data) => {
1764
1763
  if ("channel" in data) {
1765
- console.log("Estado do canal alterado:", data.channel);
1764
+ console.log(`[${app}] Estado do canal alterado:`, data.channel);
1765
+ this.channels.emitChannelEvent("ChannelStateChange", data);
1766
1766
  }
1767
1767
  this.emitGlobalEvent(data);
1768
1768
  },
1769
1769
  BridgeDestroyed: (data) => {
1770
1770
  if ("bridge" in data) {
1771
- console.log("Bridge destru\xEDda:", data.bridge);
1771
+ console.log(`[${app}] Bridge destru\xEDda:`, data.bridge);
1772
1772
  }
1773
1773
  this.emitGlobalEvent(data);
1774
1774
  },
1775
- // Adicione mais eventos conforme necessário
1776
- // Exemplo:
1777
1775
  ChannelDtmfReceived: (data) => {
1778
1776
  if ("channel" in data) {
1779
- console.log("DTMF recebido no canal:", data.channel);
1777
+ console.log(`[${app}] DTMF recebido no canal:`, data.channel);
1780
1778
  this.channels.emitChannelEvent("ChannelDtmfReceived", data);
1781
1779
  }
1782
1780
  this.emitGlobalEvent(data);
1781
+ },
1782
+ // Adicione mais eventos conforme necessário
1783
+ EndpointStateChange: (data) => {
1784
+ if ("endpoint" in data) {
1785
+ console.log(`[${app}] Estado do endpoint alterado:`, data.endpoint);
1786
+ }
1787
+ this.emitGlobalEvent(data);
1788
+ },
1789
+ ChannelHangupRequest: (data) => {
1790
+ if ("channel" in data) {
1791
+ console.log(`[${app}] Requisi\xE7\xE3o de hangup no canal:`, data.channel);
1792
+ this.channels.emitChannelEvent("ChannelHangupRequest", data);
1793
+ }
1794
+ this.emitGlobalEvent(data);
1783
1795
  }
1784
1796
  };
1785
1797
  for (const [eventType, handler] of Object.entries(eventHandlers)) {
1786
1798
  if (handler) {
1787
- this.wsClient.on(eventType, handler);
1799
+ wsClient.on(eventType, handler);
1788
1800
  }
1789
1801
  }
1790
- console.log("Todos os eventos do WebSocket foram registrados.");
1802
+ console.log(`[${app}] Todos os eventos do WebSocket foram registrados.`);
1791
1803
  }
1792
1804
  /**
1793
1805
  * Registra um listener para eventos globais.