@ipcom/asterisk-ari 0.0.32 → 0.0.34

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,8 @@ var AriClient = class {
1698
1698
  baseClient;
1699
1699
  isReconnecting = false;
1700
1700
  eventEmitter = new import_events4.EventEmitter();
1701
+ wsConnections = /* @__PURE__ */ new Map();
1702
+ isWebSocketConnectedFlag = false;
1701
1703
  channels;
1702
1704
  endpoints;
1703
1705
  applications;
@@ -1722,14 +1724,19 @@ var AriClient = class {
1722
1724
  "The 'app' parameter is required to connect to the WebSocket."
1723
1725
  );
1724
1726
  }
1725
- if (this.isReconnecting) {
1726
- console.warn("Already attempting to reconnect. Skipping this attempt.");
1727
- return;
1728
- }
1729
- this.isReconnecting = true;
1730
1727
  const eventsParam = subscribedEvents && subscribedEvents.length > 0 ? `&event=${subscribedEvents.join(",")}` : "&subscribeAll=true";
1731
1728
  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}`;
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);
1733
1740
  const backoffOptions = {
1734
1741
  delayFirstAttempt: false,
1735
1742
  startingDelay: 1e3,
@@ -1739,77 +1746,87 @@ var AriClient = class {
1739
1746
  jitter: "full",
1740
1747
  retry: (error, attemptNumber) => {
1741
1748
  console.warn(`Tentativa ${attemptNumber} falhou: ${error.message}`);
1742
- return !this.wsClient?.isConnected();
1749
+ return !wsClient.isConnected();
1743
1750
  }
1744
1751
  };
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
1752
  try {
1752
1753
  await (0, import_exponential_backoff.backOff)(async () => {
1753
1754
  if (!this.wsClient) {
1754
1755
  throw new Error("WebSocketClient instance is null.");
1755
1756
  }
1756
1757
  await this.wsClient.connect();
1757
- this.integrateWebSocketEvents();
1758
+ this.isWebSocketConnectedFlag = true;
1759
+ this.integrateWebSocketEvents(app, this.wsClient);
1758
1760
  console.log(`WebSocket conectado para o app: ${app}`);
1759
1761
  await this.ensureAppRegistered(app);
1760
1762
  }, backoffOptions);
1761
1763
  } catch (err) {
1764
+ this.isWebSocketConnectedFlag = false;
1762
1765
  console.error(
1763
1766
  "N\xE3o foi poss\xEDvel conectar ao WebSocket ap\xF3s m\xFAltiplas tentativas:",
1764
1767
  err
1765
1768
  );
1766
1769
  throw err;
1767
- } finally {
1768
- this.isReconnecting = false;
1769
1770
  }
1770
1771
  }
1771
1772
  /**
1772
1773
  * Integrates WebSocket events with playback listeners.
1773
1774
  */
1774
- integrateWebSocketEvents() {
1775
- if (!this.wsClient) {
1776
- throw new Error("WebSocket client n\xE3o est\xE1 conectado.");
1775
+ integrateWebSocketEvents(app, wsClient) {
1776
+ if (!wsClient) {
1777
+ throw new Error(
1778
+ `WebSocket client para o app '${app}' n\xE3o est\xE1 conectado.`
1779
+ );
1777
1780
  }
1778
1781
  const eventHandlers = {
1779
1782
  PlaybackFinished: (data) => {
1780
1783
  if ("playbackId" in data) {
1781
1784
  this.playbacks.emitPlaybackEvent("PlaybackFinished", data);
1782
1785
  }
1786
+ console.log(`[${app}] PlaybackFinished:`, data);
1783
1787
  this.emitGlobalEvent(data);
1784
1788
  },
1785
1789
  ChannelStateChange: (data) => {
1786
1790
  if ("channel" in data) {
1787
- console.log("Estado do canal alterado:", data.channel);
1791
+ console.log(`[${app}] Estado do canal alterado:`, data.channel);
1792
+ this.channels.emitChannelEvent("ChannelStateChange", data);
1788
1793
  }
1789
1794
  this.emitGlobalEvent(data);
1790
1795
  },
1791
1796
  BridgeDestroyed: (data) => {
1792
1797
  if ("bridge" in data) {
1793
- console.log("Bridge destru\xEDda:", data.bridge);
1798
+ console.log(`[${app}] Bridge destru\xEDda:`, data.bridge);
1794
1799
  }
1795
1800
  this.emitGlobalEvent(data);
1796
1801
  },
1797
- // Adicione mais eventos conforme necessário
1798
- // Exemplo:
1799
1802
  ChannelDtmfReceived: (data) => {
1800
1803
  if ("channel" in data) {
1801
- console.log("DTMF recebido no canal:", data.channel);
1804
+ console.log(`[${app}] DTMF recebido no canal:`, data.channel);
1802
1805
  this.channels.emitChannelEvent("ChannelDtmfReceived", data);
1803
1806
  }
1804
1807
  this.emitGlobalEvent(data);
1808
+ },
1809
+ // Adicione mais eventos conforme necessário
1810
+ EndpointStateChange: (data) => {
1811
+ if ("endpoint" in data) {
1812
+ console.log(`[${app}] Estado do endpoint alterado:`, data.endpoint);
1813
+ }
1814
+ this.emitGlobalEvent(data);
1815
+ },
1816
+ ChannelHangupRequest: (data) => {
1817
+ if ("channel" in data) {
1818
+ console.log(`[${app}] Requisi\xE7\xE3o de hangup no canal:`, data.channel);
1819
+ this.channels.emitChannelEvent("ChannelHangupRequest", data);
1820
+ }
1821
+ this.emitGlobalEvent(data);
1805
1822
  }
1806
1823
  };
1807
1824
  for (const [eventType, handler] of Object.entries(eventHandlers)) {
1808
1825
  if (handler) {
1809
- this.wsClient.on(eventType, handler);
1826
+ wsClient.on(eventType, handler);
1810
1827
  }
1811
1828
  }
1812
- console.log("Todos os eventos do WebSocket foram registrados.");
1829
+ console.log(`[${app}] Todos os eventos do WebSocket foram registrados.`);
1813
1830
  }
1814
1831
  /**
1815
1832
  * Registra um listener para eventos globais.
@@ -1909,9 +1926,12 @@ var AriClient = class {
1909
1926
  * @throws {Error} Throws an error if the WebSocket client is not connected when this method is called.
1910
1927
  * @returns {void} This function doesn't return a value.
1911
1928
  */
1912
- onWebSocketEvent(event, callback) {
1929
+ async onWebSocketEvent(event, callback) {
1930
+ if (!this.isWebSocketConnectedFlag) {
1931
+ throw new Error("WebSocket ainda n\xE3o est\xE1 conectado.");
1932
+ }
1913
1933
  if (!this.wsClient) {
1914
- throw new Error("WebSocket n\xE3o est\xE1 conectado.");
1934
+ throw new Error("WebSocket n\xE3o est\xE1 configurado.");
1915
1935
  }
1916
1936
  this.wsClient.on(event, callback);
1917
1937
  }