@ipcom/asterisk-ari 0.0.37 → 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/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) {
|
|
@@ -1719,55 +1721,65 @@ var AriClient = class {
|
|
|
1719
1721
|
"The 'app' parameter is required to connect to the 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();
|
|
1724
|
+
if (this.webSocketReady) {
|
|
1725
|
+
console.log("J\xE1 existe uma tentativa de conex\xE3o ativa. Aguardando...");
|
|
1726
|
+
return this.webSocketReady;
|
|
1746
1727
|
}
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
throw new Error("WebSocketClient instance is null.");
|
|
1728
|
+
this.webSocketReady = new Promise(async (resolve, reject) => {
|
|
1729
|
+
try {
|
|
1730
|
+
if (this.isReconnecting) {
|
|
1731
|
+
console.warn(
|
|
1732
|
+
"Already attempting to reconnect. Skipping this attempt."
|
|
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(
|
|
1740
|
+
this.config.username
|
|
1741
|
+
)}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
|
|
1742
|
+
const backoffOptions = {
|
|
1743
|
+
delayFirstAttempt: false,
|
|
1744
|
+
startingDelay: 1e3,
|
|
1745
|
+
timeMultiple: 2,
|
|
1746
|
+
maxDelay: 3e4,
|
|
1747
|
+
numOfAttempts: 10,
|
|
1748
|
+
jitter: "full",
|
|
1749
|
+
retry: (error, attemptNumber) => {
|
|
1750
|
+
console.warn(`Tentativa ${attemptNumber} falhou: ${error.message}`);
|
|
1751
|
+
return !this.wsClient?.isConnected();
|
|
1752
|
+
}
|
|
1753
|
+
};
|
|
1754
|
+
if (this.wsClient?.isConnected()) {
|
|
1755
|
+
console.log("WebSocket j\xE1 conectado. Removendo listeners antigos...");
|
|
1756
|
+
this.wsClient.removeAllListeners();
|
|
1757
|
+
this.wsClient.close();
|
|
1758
|
+
}
|
|
1759
|
+
this.wsClient = new WebSocketClient(wsUrl);
|
|
1760
|
+
await (0, import_exponential_backoff.backOff)(async () => {
|
|
1761
|
+
if (!this.wsClient) {
|
|
1762
|
+
throw new Error("WebSocketClient instance is null.");
|
|
1763
|
+
}
|
|
1764
|
+
await this.wsClient.connect();
|
|
1765
|
+
this.integrateWebSocketEvents(app, this.wsClient);
|
|
1766
|
+
console.log(`WebSocket conectado para o app: ${app}`);
|
|
1767
|
+
await this.ensureAppRegistered(app);
|
|
1768
|
+
this.processPendingListeners();
|
|
1769
|
+
}, backoffOptions);
|
|
1770
|
+
resolve();
|
|
1771
|
+
} catch (err) {
|
|
1772
|
+
console.error(
|
|
1773
|
+
"N\xE3o foi poss\xEDvel conectar ao WebSocket ap\xF3s m\xFAltiplas tentativas:",
|
|
1774
|
+
err
|
|
1775
|
+
);
|
|
1776
|
+
reject(err);
|
|
1777
|
+
} finally {
|
|
1778
|
+
this.isReconnecting = false;
|
|
1779
|
+
this.webSocketReady = null;
|
|
1780
|
+
}
|
|
1781
|
+
});
|
|
1782
|
+
return this.webSocketReady;
|
|
1771
1783
|
}
|
|
1772
1784
|
/**
|
|
1773
1785
|
* Integrates WebSocket events with playback listeners.
|
|
@@ -1926,13 +1938,13 @@ var AriClient = class {
|
|
|
1926
1938
|
* @throws {Error} Throws an error if the WebSocket client is not connected when this method is called.
|
|
1927
1939
|
* @returns {void} This function doesn't return a value.
|
|
1928
1940
|
*/
|
|
1929
|
-
onWebSocketEvent(event, callback) {
|
|
1941
|
+
async onWebSocketEvent(event, callback) {
|
|
1942
|
+
if (this.webSocketReady) {
|
|
1943
|
+
console.log(`Aguardando WebSocket conectar antes de registrar: ${event}`);
|
|
1944
|
+
await this.webSocketReady;
|
|
1945
|
+
}
|
|
1930
1946
|
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;
|
|
1947
|
+
throw new Error("WebSocket ainda n\xE3o est\xE1 conectado.");
|
|
1936
1948
|
}
|
|
1937
1949
|
console.log(`Registrando listener para evento: ${event}`);
|
|
1938
1950
|
this.wsClient.on(event, callback);
|