@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.
package/dist/esm/index.js CHANGED
@@ -843,13 +843,20 @@ function toQueryParams2(options) {
843
843
  ).toString();
844
844
  }
845
845
  var ChannelInstance = class extends EventEmitter {
846
- constructor(client, baseClient, channelId) {
846
+ constructor(client, baseClient, channelId = `channel-${Date.now()}`) {
847
847
  super();
848
848
  this.client = client;
849
849
  this.baseClient = baseClient;
850
850
  this.channelId = channelId;
851
851
  }
852
852
  channelData = null;
853
+ /**
854
+ * Getter para o ID do canal.
855
+ * Sempre retorna um ID, seja o definido manualmente ou o gerado automaticamente.
856
+ */
857
+ get id() {
858
+ return this.channelId;
859
+ }
853
860
  /**
854
861
  * Origina um canal físico no Asterisk.
855
862
  */
@@ -1669,6 +1676,7 @@ var AriClient = class {
1669
1676
  baseClient;
1670
1677
  isReconnecting = false;
1671
1678
  eventEmitter = new EventEmitter4();
1679
+ wsConnections = /* @__PURE__ */ new Map();
1672
1680
  channels;
1673
1681
  endpoints;
1674
1682
  applications;
@@ -1693,14 +1701,19 @@ var AriClient = class {
1693
1701
  "The 'app' parameter is required to connect to the WebSocket."
1694
1702
  );
1695
1703
  }
1696
- if (this.isReconnecting) {
1697
- console.warn("Already attempting to reconnect. Skipping this attempt.");
1698
- return;
1699
- }
1700
- this.isReconnecting = true;
1701
1704
  const eventsParam = subscribedEvents && subscribedEvents.length > 0 ? `&event=${subscribedEvents.join(",")}` : "&subscribeAll=true";
1702
1705
  const protocol = this.config.secure ? "wss" : "ws";
1703
- 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);
1704
1717
  const backoffOptions = {
1705
1718
  delayFirstAttempt: false,
1706
1719
  startingDelay: 1e3,
@@ -1710,77 +1723,83 @@ var AriClient = class {
1710
1723
  jitter: "full",
1711
1724
  retry: (error, attemptNumber) => {
1712
1725
  console.warn(`Tentativa ${attemptNumber} falhou: ${error.message}`);
1713
- return !this.wsClient?.isConnected();
1726
+ return !wsClient.isConnected();
1714
1727
  }
1715
1728
  };
1716
- if (this.wsClient?.isConnected()) {
1717
- console.log("WebSocket j\xE1 conectado. Removendo listeners antigos...");
1718
- this.wsClient.removeAllListeners();
1719
- this.wsClient.close();
1720
- }
1721
- this.wsClient = new WebSocketClient(wsUrl);
1722
1729
  try {
1723
1730
  await (0, import_exponential_backoff.backOff)(async () => {
1724
- if (!this.wsClient) {
1725
- throw new Error("WebSocketClient instance is null.");
1726
- }
1727
- await this.wsClient.connect();
1728
- this.integrateWebSocketEvents();
1731
+ await wsClient.connect();
1732
+ this.integrateWebSocketEvents(app, wsClient);
1729
1733
  console.log(`WebSocket conectado para o app: ${app}`);
1730
1734
  await this.ensureAppRegistered(app);
1731
1735
  }, backoffOptions);
1732
1736
  } catch (err) {
1733
1737
  console.error(
1734
- "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}:`,
1735
1739
  err
1736
1740
  );
1741
+ this.wsConnections.delete(app);
1737
1742
  throw err;
1738
- } finally {
1739
- this.isReconnecting = false;
1740
1743
  }
1741
1744
  }
1742
1745
  /**
1743
1746
  * Integrates WebSocket events with playback listeners.
1744
1747
  */
1745
- integrateWebSocketEvents() {
1746
- if (!this.wsClient) {
1747
- 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
+ );
1748
1753
  }
1749
1754
  const eventHandlers = {
1750
1755
  PlaybackFinished: (data) => {
1751
1756
  if ("playbackId" in data) {
1752
1757
  this.playbacks.emitPlaybackEvent("PlaybackFinished", data);
1753
1758
  }
1759
+ console.log(`[${app}] PlaybackFinished:`, data);
1754
1760
  this.emitGlobalEvent(data);
1755
1761
  },
1756
1762
  ChannelStateChange: (data) => {
1757
1763
  if ("channel" in data) {
1758
- console.log("Estado do canal alterado:", data.channel);
1764
+ console.log(`[${app}] Estado do canal alterado:`, data.channel);
1765
+ this.channels.emitChannelEvent("ChannelStateChange", data);
1759
1766
  }
1760
1767
  this.emitGlobalEvent(data);
1761
1768
  },
1762
1769
  BridgeDestroyed: (data) => {
1763
1770
  if ("bridge" in data) {
1764
- console.log("Bridge destru\xEDda:", data.bridge);
1771
+ console.log(`[${app}] Bridge destru\xEDda:`, data.bridge);
1765
1772
  }
1766
1773
  this.emitGlobalEvent(data);
1767
1774
  },
1768
- // Adicione mais eventos conforme necessário
1769
- // Exemplo:
1770
1775
  ChannelDtmfReceived: (data) => {
1771
1776
  if ("channel" in data) {
1772
- console.log("DTMF recebido no canal:", data.channel);
1777
+ console.log(`[${app}] DTMF recebido no canal:`, data.channel);
1773
1778
  this.channels.emitChannelEvent("ChannelDtmfReceived", data);
1774
1779
  }
1775
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);
1776
1795
  }
1777
1796
  };
1778
1797
  for (const [eventType, handler] of Object.entries(eventHandlers)) {
1779
1798
  if (handler) {
1780
- this.wsClient.on(eventType, handler);
1799
+ wsClient.on(eventType, handler);
1781
1800
  }
1782
1801
  }
1783
- console.log("Todos os eventos do WebSocket foram registrados.");
1802
+ console.log(`[${app}] Todos os eventos do WebSocket foram registrados.`);
1784
1803
  }
1785
1804
  /**
1786
1805
  * Registra um listener para eventos globais.