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