@ipcom/asterisk-ari 0.0.37 → 0.0.39
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
|
@@ -1678,6 +1678,8 @@ var AriClient = class {
|
|
|
1678
1678
|
eventEmitter = new EventEmitter4();
|
|
1679
1679
|
wsConnections = /* @__PURE__ */ new Map();
|
|
1680
1680
|
isWebSocketConnectedFlag = false;
|
|
1681
|
+
webSocketReady = null;
|
|
1682
|
+
// Promise para rastrear o estado do WebSocket
|
|
1681
1683
|
pendingListeners = [];
|
|
1682
1684
|
processPendingListeners() {
|
|
1683
1685
|
if (!this.wsClient || !this.isWebSocketConnectedFlag) {
|
|
@@ -1716,58 +1718,67 @@ var AriClient = class {
|
|
|
1716
1718
|
async connectWebSocket(app, subscribedEvents) {
|
|
1717
1719
|
if (!app) {
|
|
1718
1720
|
throw new Error(
|
|
1719
|
-
"
|
|
1721
|
+
"O par\xE2metro 'app' \xE9 obrigat\xF3rio para conectar ao WebSocket."
|
|
1720
1722
|
);
|
|
1721
1723
|
}
|
|
1722
|
-
if (this.
|
|
1723
|
-
console.
|
|
1724
|
-
return;
|
|
1725
|
-
}
|
|
1726
|
-
this.isReconnecting = true;
|
|
1727
|
-
const eventsParam = subscribedEvents && subscribedEvents.length > 0 ? `&event=${subscribedEvents.join(",")}` : "&subscribeAll=true";
|
|
1728
|
-
const protocol = this.config.secure ? "wss" : "ws";
|
|
1729
|
-
const wsUrl = `${protocol}://${encodeURIComponent(this.config.username)}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
|
|
1730
|
-
const backoffOptions = {
|
|
1731
|
-
delayFirstAttempt: false,
|
|
1732
|
-
startingDelay: 1e3,
|
|
1733
|
-
timeMultiple: 2,
|
|
1734
|
-
maxDelay: 3e4,
|
|
1735
|
-
numOfAttempts: 10,
|
|
1736
|
-
jitter: "full",
|
|
1737
|
-
retry: (error, attemptNumber) => {
|
|
1738
|
-
console.warn(`Tentativa ${attemptNumber} falhou: ${error.message}`);
|
|
1739
|
-
return !this.wsClient?.isConnected();
|
|
1740
|
-
}
|
|
1741
|
-
};
|
|
1742
|
-
if (this.wsClient?.isConnected()) {
|
|
1743
|
-
console.log("WebSocket j\xE1 conectado. Removendo listeners antigos...");
|
|
1744
|
-
this.wsClient.removeAllListeners();
|
|
1745
|
-
this.wsClient.close();
|
|
1746
|
-
}
|
|
1747
|
-
if (!this.wsClient) {
|
|
1748
|
-
console.log("Criando nova inst\xE2ncia do WebSocketClient...");
|
|
1749
|
-
this.wsClient = new WebSocketClient(wsUrl);
|
|
1724
|
+
if (this.webSocketReady) {
|
|
1725
|
+
console.log("J\xE1 existe uma tentativa de conex\xE3o ativa. Aguardando...");
|
|
1726
|
+
return this.webSocketReady;
|
|
1750
1727
|
}
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
if (
|
|
1754
|
-
|
|
1728
|
+
this.webSocketReady = new Promise(async (resolve, reject) => {
|
|
1729
|
+
try {
|
|
1730
|
+
if (this.isReconnecting) {
|
|
1731
|
+
console.warn(
|
|
1732
|
+
"J\xE1 est\xE1 tentando reconectar. Ignorando esta tentativa."
|
|
1733
|
+
);
|
|
1734
|
+
return;
|
|
1755
1735
|
}
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1736
|
+
this.isReconnecting = true;
|
|
1737
|
+
const eventsParam = subscribedEvents && subscribedEvents.length > 0 ? `&event=${subscribedEvents.join(",")}` : "&subscribeAll=true";
|
|
1738
|
+
const protocol = this.config.secure ? "wss" : "ws";
|
|
1739
|
+
const wsUrl = `${protocol}://${encodeURIComponent(this.config.username)}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
|
|
1740
|
+
const backoffOptions = {
|
|
1741
|
+
delayFirstAttempt: false,
|
|
1742
|
+
startingDelay: 1e3,
|
|
1743
|
+
timeMultiple: 2,
|
|
1744
|
+
maxDelay: 3e4,
|
|
1745
|
+
numOfAttempts: 10,
|
|
1746
|
+
jitter: "full",
|
|
1747
|
+
retry: (error, attemptNumber) => {
|
|
1748
|
+
console.warn(`Tentativa ${attemptNumber} falhou: ${error.message}`);
|
|
1749
|
+
return !this.wsClient?.isConnected();
|
|
1750
|
+
}
|
|
1751
|
+
};
|
|
1752
|
+
if (this.wsClient?.isConnected()) {
|
|
1753
|
+
console.log("WebSocket j\xE1 conectado. Removendo listeners antigos...");
|
|
1754
|
+
this.wsClient.removeAllListeners();
|
|
1755
|
+
this.wsClient.close();
|
|
1756
|
+
}
|
|
1757
|
+
this.wsClient = new WebSocketClient(wsUrl);
|
|
1758
|
+
await (0, import_exponential_backoff.backOff)(async () => {
|
|
1759
|
+
if (!this.wsClient) {
|
|
1760
|
+
throw new Error("WebSocketClient instance is null.");
|
|
1761
|
+
}
|
|
1762
|
+
await this.wsClient.connect();
|
|
1763
|
+
this.integrateWebSocketEvents(app, this.wsClient);
|
|
1764
|
+
console.log(`WebSocket conectado para o app: ${app}`);
|
|
1765
|
+
await this.ensureAppRegistered(app);
|
|
1766
|
+
this.processPendingListeners();
|
|
1767
|
+
this.isWebSocketConnectedFlag = true;
|
|
1768
|
+
}, backoffOptions);
|
|
1769
|
+
resolve();
|
|
1770
|
+
} catch (err) {
|
|
1771
|
+
console.error(
|
|
1772
|
+
"N\xE3o foi poss\xEDvel conectar ao WebSocket ap\xF3s m\xFAltiplas tentativas:",
|
|
1773
|
+
err
|
|
1774
|
+
);
|
|
1775
|
+
reject(err);
|
|
1776
|
+
} finally {
|
|
1777
|
+
this.isReconnecting = false;
|
|
1778
|
+
this.webSocketReady = null;
|
|
1779
|
+
}
|
|
1780
|
+
});
|
|
1781
|
+
return this.webSocketReady;
|
|
1771
1782
|
}
|
|
1772
1783
|
/**
|
|
1773
1784
|
* Integrates WebSocket events with playback listeners.
|
|
@@ -1778,6 +1789,7 @@ var AriClient = class {
|
|
|
1778
1789
|
`WebSocket client para o app '${app}' n\xE3o est\xE1 conectado.`
|
|
1779
1790
|
);
|
|
1780
1791
|
}
|
|
1792
|
+
console.log(`[${app}] Registrando eventos do WebSocket...`);
|
|
1781
1793
|
const eventHandlers = {
|
|
1782
1794
|
PlaybackFinished: (data) => {
|
|
1783
1795
|
if ("playbackId" in data) {
|
|
@@ -1805,20 +1817,6 @@ var AriClient = class {
|
|
|
1805
1817
|
this.channels.emitChannelEvent("ChannelDtmfReceived", data);
|
|
1806
1818
|
}
|
|
1807
1819
|
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);
|
|
1822
1820
|
}
|
|
1823
1821
|
};
|
|
1824
1822
|
for (const [eventType, handler] of Object.entries(eventHandlers)) {
|
|
@@ -1926,13 +1924,13 @@ var AriClient = class {
|
|
|
1926
1924
|
* @throws {Error} Throws an error if the WebSocket client is not connected when this method is called.
|
|
1927
1925
|
* @returns {void} This function doesn't return a value.
|
|
1928
1926
|
*/
|
|
1929
|
-
onWebSocketEvent(event, callback) {
|
|
1927
|
+
async onWebSocketEvent(event, callback) {
|
|
1928
|
+
if (this.webSocketReady) {
|
|
1929
|
+
console.log(`Aguardando WebSocket conectar antes de registrar: ${event}`);
|
|
1930
|
+
await this.webSocketReady;
|
|
1931
|
+
}
|
|
1930
1932
|
if (!this.wsClient || !this.isWebSocketConnectedFlag) {
|
|
1931
|
-
|
|
1932
|
-
`WebSocket ainda n\xE3o est\xE1 conectado. Colocando o listener na fila para: ${event}`
|
|
1933
|
-
);
|
|
1934
|
-
this.pendingListeners.push({ event, callback });
|
|
1935
|
-
return;
|
|
1933
|
+
throw new Error("WebSocket ainda n\xE3o est\xE1 conectado.");
|
|
1936
1934
|
}
|
|
1937
1935
|
console.log(`Registrando listener para evento: ${event}`);
|
|
1938
1936
|
this.wsClient.on(event, callback);
|