@ipcom/asterisk-ari 0.0.31 → 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.
@@ -865,13 +865,20 @@ function toQueryParams2(options) {
865
865
  ).toString();
866
866
  }
867
867
  var ChannelInstance = class extends import_events.EventEmitter {
868
- constructor(client, baseClient, channelId) {
868
+ constructor(client, baseClient, channelId = `channel-${Date.now()}`) {
869
869
  super();
870
870
  this.client = client;
871
871
  this.baseClient = baseClient;
872
872
  this.channelId = channelId;
873
873
  }
874
874
  channelData = null;
875
+ /**
876
+ * Getter para o ID do canal.
877
+ * Sempre retorna um ID, seja o definido manualmente ou o gerado automaticamente.
878
+ */
879
+ get id() {
880
+ return this.channelId;
881
+ }
875
882
  /**
876
883
  * Origina um canal físico no Asterisk.
877
884
  */
@@ -1691,6 +1698,7 @@ var AriClient = class {
1691
1698
  baseClient;
1692
1699
  isReconnecting = false;
1693
1700
  eventEmitter = new import_events4.EventEmitter();
1701
+ wsConnections = /* @__PURE__ */ new Map();
1694
1702
  channels;
1695
1703
  endpoints;
1696
1704
  applications;
@@ -1715,14 +1723,19 @@ var AriClient = class {
1715
1723
  "The 'app' parameter is required to connect to the WebSocket."
1716
1724
  );
1717
1725
  }
1718
- if (this.isReconnecting) {
1719
- console.warn("Already attempting to reconnect. Skipping this attempt.");
1720
- return;
1721
- }
1722
- this.isReconnecting = true;
1723
1726
  const eventsParam = subscribedEvents && subscribedEvents.length > 0 ? `&event=${subscribedEvents.join(",")}` : "&subscribeAll=true";
1724
1727
  const protocol = this.config.secure ? "wss" : "ws";
1725
- 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);
1726
1739
  const backoffOptions = {
1727
1740
  delayFirstAttempt: false,
1728
1741
  startingDelay: 1e3,
@@ -1732,77 +1745,83 @@ var AriClient = class {
1732
1745
  jitter: "full",
1733
1746
  retry: (error, attemptNumber) => {
1734
1747
  console.warn(`Tentativa ${attemptNumber} falhou: ${error.message}`);
1735
- return !this.wsClient?.isConnected();
1748
+ return !wsClient.isConnected();
1736
1749
  }
1737
1750
  };
1738
- if (this.wsClient?.isConnected()) {
1739
- console.log("WebSocket j\xE1 conectado. Removendo listeners antigos...");
1740
- this.wsClient.removeAllListeners();
1741
- this.wsClient.close();
1742
- }
1743
- this.wsClient = new WebSocketClient(wsUrl);
1744
1751
  try {
1745
1752
  await (0, import_exponential_backoff.backOff)(async () => {
1746
- if (!this.wsClient) {
1747
- throw new Error("WebSocketClient instance is null.");
1748
- }
1749
- await this.wsClient.connect();
1750
- this.integrateWebSocketEvents();
1753
+ await wsClient.connect();
1754
+ this.integrateWebSocketEvents(app, wsClient);
1751
1755
  console.log(`WebSocket conectado para o app: ${app}`);
1752
1756
  await this.ensureAppRegistered(app);
1753
1757
  }, backoffOptions);
1754
1758
  } catch (err) {
1755
1759
  console.error(
1756
- "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}:`,
1757
1761
  err
1758
1762
  );
1763
+ this.wsConnections.delete(app);
1759
1764
  throw err;
1760
- } finally {
1761
- this.isReconnecting = false;
1762
1765
  }
1763
1766
  }
1764
1767
  /**
1765
1768
  * Integrates WebSocket events with playback listeners.
1766
1769
  */
1767
- integrateWebSocketEvents() {
1768
- if (!this.wsClient) {
1769
- 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
+ );
1770
1775
  }
1771
1776
  const eventHandlers = {
1772
1777
  PlaybackFinished: (data) => {
1773
1778
  if ("playbackId" in data) {
1774
1779
  this.playbacks.emitPlaybackEvent("PlaybackFinished", data);
1775
1780
  }
1781
+ console.log(`[${app}] PlaybackFinished:`, data);
1776
1782
  this.emitGlobalEvent(data);
1777
1783
  },
1778
1784
  ChannelStateChange: (data) => {
1779
1785
  if ("channel" in data) {
1780
- console.log("Estado do canal alterado:", data.channel);
1786
+ console.log(`[${app}] Estado do canal alterado:`, data.channel);
1787
+ this.channels.emitChannelEvent("ChannelStateChange", data);
1781
1788
  }
1782
1789
  this.emitGlobalEvent(data);
1783
1790
  },
1784
1791
  BridgeDestroyed: (data) => {
1785
1792
  if ("bridge" in data) {
1786
- console.log("Bridge destru\xEDda:", data.bridge);
1793
+ console.log(`[${app}] Bridge destru\xEDda:`, data.bridge);
1787
1794
  }
1788
1795
  this.emitGlobalEvent(data);
1789
1796
  },
1790
- // Adicione mais eventos conforme necessário
1791
- // Exemplo:
1792
1797
  ChannelDtmfReceived: (data) => {
1793
1798
  if ("channel" in data) {
1794
- console.log("DTMF recebido no canal:", data.channel);
1799
+ console.log(`[${app}] DTMF recebido no canal:`, data.channel);
1795
1800
  this.channels.emitChannelEvent("ChannelDtmfReceived", data);
1796
1801
  }
1797
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);
1798
1817
  }
1799
1818
  };
1800
1819
  for (const [eventType, handler] of Object.entries(eventHandlers)) {
1801
1820
  if (handler) {
1802
- this.wsClient.on(eventType, handler);
1821
+ wsClient.on(eventType, handler);
1803
1822
  }
1804
1823
  }
1805
- console.log("Todos os eventos do WebSocket foram registrados.");
1824
+ console.log(`[${app}] Todos os eventos do WebSocket foram registrados.`);
1806
1825
  }
1807
1826
  /**
1808
1827
  * Registra um listener para eventos globais.