@ipcom/asterisk-ari 0.0.36 → 0.0.38
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/cjs/index.cjs
CHANGED
|
@@ -1700,6 +1700,25 @@ var AriClient = class {
|
|
|
1700
1700
|
eventEmitter = new import_events4.EventEmitter();
|
|
1701
1701
|
wsConnections = /* @__PURE__ */ new Map();
|
|
1702
1702
|
isWebSocketConnectedFlag = false;
|
|
1703
|
+
webSocketReady = null;
|
|
1704
|
+
// Promise para rastrear o estado do WebSocket
|
|
1705
|
+
pendingListeners = [];
|
|
1706
|
+
processPendingListeners() {
|
|
1707
|
+
if (!this.wsClient || !this.isWebSocketConnectedFlag) {
|
|
1708
|
+
console.warn(
|
|
1709
|
+
"WebSocket ainda n\xE3o est\xE1 pronto. N\xE3o \xE9 poss\xEDvel processar a fila."
|
|
1710
|
+
);
|
|
1711
|
+
return;
|
|
1712
|
+
}
|
|
1713
|
+
console.log(
|
|
1714
|
+
`Processando ${this.pendingListeners.length} listeners pendentes...`
|
|
1715
|
+
);
|
|
1716
|
+
while (this.pendingListeners.length > 0) {
|
|
1717
|
+
const { event, callback } = this.pendingListeners.shift();
|
|
1718
|
+
console.log(`Registrando listener pendente para evento: ${event}`);
|
|
1719
|
+
this.wsClient.on(event, callback);
|
|
1720
|
+
}
|
|
1721
|
+
}
|
|
1703
1722
|
channels;
|
|
1704
1723
|
endpoints;
|
|
1705
1724
|
applications;
|
|
@@ -1724,57 +1743,65 @@ var AriClient = class {
|
|
|
1724
1743
|
"The 'app' parameter is required to connect to the WebSocket."
|
|
1725
1744
|
);
|
|
1726
1745
|
}
|
|
1727
|
-
if (this.
|
|
1728
|
-
console.
|
|
1729
|
-
return;
|
|
1730
|
-
}
|
|
1731
|
-
this.isReconnecting = true;
|
|
1732
|
-
const eventsParam = subscribedEvents && subscribedEvents.length > 0 ? `&event=${subscribedEvents.join(",")}` : "&subscribeAll=true";
|
|
1733
|
-
const protocol = this.config.secure ? "wss" : "ws";
|
|
1734
|
-
const wsUrl = `${protocol}://${encodeURIComponent(this.config.username)}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
|
|
1735
|
-
const backoffOptions = {
|
|
1736
|
-
delayFirstAttempt: false,
|
|
1737
|
-
startingDelay: 1e3,
|
|
1738
|
-
timeMultiple: 2,
|
|
1739
|
-
maxDelay: 3e4,
|
|
1740
|
-
numOfAttempts: 10,
|
|
1741
|
-
jitter: "full",
|
|
1742
|
-
retry: (error, attemptNumber) => {
|
|
1743
|
-
console.warn(`Tentativa ${attemptNumber} falhou: ${error.message}`);
|
|
1744
|
-
return !this.wsClient?.isConnected();
|
|
1745
|
-
}
|
|
1746
|
-
};
|
|
1747
|
-
if (this.wsClient?.isConnected()) {
|
|
1748
|
-
console.log("WebSocket j\xE1 conectado. Removendo listeners antigos...");
|
|
1749
|
-
this.wsClient.removeAllListeners();
|
|
1750
|
-
this.wsClient.close();
|
|
1746
|
+
if (this.webSocketReady) {
|
|
1747
|
+
console.log("J\xE1 existe uma tentativa de conex\xE3o ativa. Aguardando...");
|
|
1748
|
+
return this.webSocketReady;
|
|
1751
1749
|
}
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
throw new Error("WebSocketClient instance is null.");
|
|
1750
|
+
this.webSocketReady = new Promise(async (resolve, reject) => {
|
|
1751
|
+
try {
|
|
1752
|
+
if (this.isReconnecting) {
|
|
1753
|
+
console.warn(
|
|
1754
|
+
"Already attempting to reconnect. Skipping this attempt."
|
|
1755
|
+
);
|
|
1756
|
+
return;
|
|
1760
1757
|
}
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1758
|
+
this.isReconnecting = true;
|
|
1759
|
+
const eventsParam = subscribedEvents && subscribedEvents.length > 0 ? `&event=${subscribedEvents.join(",")}` : "&subscribeAll=true";
|
|
1760
|
+
const protocol = this.config.secure ? "wss" : "ws";
|
|
1761
|
+
const wsUrl = `${protocol}://${encodeURIComponent(
|
|
1762
|
+
this.config.username
|
|
1763
|
+
)}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
|
|
1764
|
+
const backoffOptions = {
|
|
1765
|
+
delayFirstAttempt: false,
|
|
1766
|
+
startingDelay: 1e3,
|
|
1767
|
+
timeMultiple: 2,
|
|
1768
|
+
maxDelay: 3e4,
|
|
1769
|
+
numOfAttempts: 10,
|
|
1770
|
+
jitter: "full",
|
|
1771
|
+
retry: (error, attemptNumber) => {
|
|
1772
|
+
console.warn(`Tentativa ${attemptNumber} falhou: ${error.message}`);
|
|
1773
|
+
return !this.wsClient?.isConnected();
|
|
1774
|
+
}
|
|
1775
|
+
};
|
|
1776
|
+
if (this.wsClient?.isConnected()) {
|
|
1777
|
+
console.log("WebSocket j\xE1 conectado. Removendo listeners antigos...");
|
|
1778
|
+
this.wsClient.removeAllListeners();
|
|
1779
|
+
this.wsClient.close();
|
|
1764
1780
|
}
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1781
|
+
this.wsClient = new WebSocketClient(wsUrl);
|
|
1782
|
+
await (0, import_exponential_backoff.backOff)(async () => {
|
|
1783
|
+
if (!this.wsClient) {
|
|
1784
|
+
throw new Error("WebSocketClient instance is null.");
|
|
1785
|
+
}
|
|
1786
|
+
await this.wsClient.connect();
|
|
1787
|
+
this.integrateWebSocketEvents(app, this.wsClient);
|
|
1788
|
+
console.log(`WebSocket conectado para o app: ${app}`);
|
|
1789
|
+
await this.ensureAppRegistered(app);
|
|
1790
|
+
this.processPendingListeners();
|
|
1791
|
+
}, backoffOptions);
|
|
1792
|
+
resolve();
|
|
1793
|
+
} catch (err) {
|
|
1794
|
+
console.error(
|
|
1795
|
+
"N\xE3o foi poss\xEDvel conectar ao WebSocket ap\xF3s m\xFAltiplas tentativas:",
|
|
1796
|
+
err
|
|
1797
|
+
);
|
|
1798
|
+
reject(err);
|
|
1799
|
+
} finally {
|
|
1800
|
+
this.isReconnecting = false;
|
|
1801
|
+
this.webSocketReady = null;
|
|
1802
|
+
}
|
|
1803
|
+
});
|
|
1804
|
+
return this.webSocketReady;
|
|
1778
1805
|
}
|
|
1779
1806
|
/**
|
|
1780
1807
|
* Integrates WebSocket events with playback listeners.
|
|
@@ -1933,16 +1960,13 @@ var AriClient = class {
|
|
|
1933
1960
|
* @throws {Error} Throws an error if the WebSocket client is not connected when this method is called.
|
|
1934
1961
|
* @returns {void} This function doesn't return a value.
|
|
1935
1962
|
*/
|
|
1936
|
-
onWebSocketEvent(event, callback) {
|
|
1937
|
-
if (
|
|
1938
|
-
console.
|
|
1939
|
-
|
|
1963
|
+
async onWebSocketEvent(event, callback) {
|
|
1964
|
+
if (this.webSocketReady) {
|
|
1965
|
+
console.log(`Aguardando WebSocket conectar antes de registrar: ${event}`);
|
|
1966
|
+
await this.webSocketReady;
|
|
1940
1967
|
}
|
|
1941
|
-
if (!this.isWebSocketConnectedFlag) {
|
|
1942
|
-
|
|
1943
|
-
"WebSocket ainda n\xE3o est\xE1 conectado. Tentando reconectar..."
|
|
1944
|
-
);
|
|
1945
|
-
return;
|
|
1968
|
+
if (!this.wsClient || !this.isWebSocketConnectedFlag) {
|
|
1969
|
+
throw new Error("WebSocket ainda n\xE3o est\xE1 conectado.");
|
|
1946
1970
|
}
|
|
1947
1971
|
console.log(`Registrando listener para evento: ${event}`);
|
|
1948
1972
|
this.wsClient.on(event, callback);
|