@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/esm/index.js
CHANGED
|
@@ -1678,6 +1678,25 @@ 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
|
|
1683
|
+
pendingListeners = [];
|
|
1684
|
+
processPendingListeners() {
|
|
1685
|
+
if (!this.wsClient || !this.isWebSocketConnectedFlag) {
|
|
1686
|
+
console.warn(
|
|
1687
|
+
"WebSocket ainda n\xE3o est\xE1 pronto. N\xE3o \xE9 poss\xEDvel processar a fila."
|
|
1688
|
+
);
|
|
1689
|
+
return;
|
|
1690
|
+
}
|
|
1691
|
+
console.log(
|
|
1692
|
+
`Processando ${this.pendingListeners.length} listeners pendentes...`
|
|
1693
|
+
);
|
|
1694
|
+
while (this.pendingListeners.length > 0) {
|
|
1695
|
+
const { event, callback } = this.pendingListeners.shift();
|
|
1696
|
+
console.log(`Registrando listener pendente para evento: ${event}`);
|
|
1697
|
+
this.wsClient.on(event, callback);
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1681
1700
|
channels;
|
|
1682
1701
|
endpoints;
|
|
1683
1702
|
applications;
|
|
@@ -1702,57 +1721,65 @@ var AriClient = class {
|
|
|
1702
1721
|
"The 'app' parameter is required to connect to the WebSocket."
|
|
1703
1722
|
);
|
|
1704
1723
|
}
|
|
1705
|
-
if (this.
|
|
1706
|
-
console.
|
|
1707
|
-
return;
|
|
1708
|
-
}
|
|
1709
|
-
this.isReconnecting = true;
|
|
1710
|
-
const eventsParam = subscribedEvents && subscribedEvents.length > 0 ? `&event=${subscribedEvents.join(",")}` : "&subscribeAll=true";
|
|
1711
|
-
const protocol = this.config.secure ? "wss" : "ws";
|
|
1712
|
-
const wsUrl = `${protocol}://${encodeURIComponent(this.config.username)}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
|
|
1713
|
-
const backoffOptions = {
|
|
1714
|
-
delayFirstAttempt: false,
|
|
1715
|
-
startingDelay: 1e3,
|
|
1716
|
-
timeMultiple: 2,
|
|
1717
|
-
maxDelay: 3e4,
|
|
1718
|
-
numOfAttempts: 10,
|
|
1719
|
-
jitter: "full",
|
|
1720
|
-
retry: (error, attemptNumber) => {
|
|
1721
|
-
console.warn(`Tentativa ${attemptNumber} falhou: ${error.message}`);
|
|
1722
|
-
return !this.wsClient?.isConnected();
|
|
1723
|
-
}
|
|
1724
|
-
};
|
|
1725
|
-
if (this.wsClient?.isConnected()) {
|
|
1726
|
-
console.log("WebSocket j\xE1 conectado. Removendo listeners antigos...");
|
|
1727
|
-
this.wsClient.removeAllListeners();
|
|
1728
|
-
this.wsClient.close();
|
|
1724
|
+
if (this.webSocketReady) {
|
|
1725
|
+
console.log("J\xE1 existe uma tentativa de conex\xE3o ativa. Aguardando...");
|
|
1726
|
+
return this.webSocketReady;
|
|
1729
1727
|
}
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
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;
|
|
1738
1735
|
}
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
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();
|
|
1742
1758
|
}
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
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;
|
|
1756
1783
|
}
|
|
1757
1784
|
/**
|
|
1758
1785
|
* Integrates WebSocket events with playback listeners.
|
|
@@ -1911,16 +1938,13 @@ var AriClient = class {
|
|
|
1911
1938
|
* @throws {Error} Throws an error if the WebSocket client is not connected when this method is called.
|
|
1912
1939
|
* @returns {void} This function doesn't return a value.
|
|
1913
1940
|
*/
|
|
1914
|
-
onWebSocketEvent(event, callback) {
|
|
1915
|
-
if (
|
|
1916
|
-
console.
|
|
1917
|
-
|
|
1941
|
+
async onWebSocketEvent(event, callback) {
|
|
1942
|
+
if (this.webSocketReady) {
|
|
1943
|
+
console.log(`Aguardando WebSocket conectar antes de registrar: ${event}`);
|
|
1944
|
+
await this.webSocketReady;
|
|
1918
1945
|
}
|
|
1919
|
-
if (!this.isWebSocketConnectedFlag) {
|
|
1920
|
-
|
|
1921
|
-
"WebSocket ainda n\xE3o est\xE1 conectado. Tentando reconectar..."
|
|
1922
|
-
);
|
|
1923
|
-
return;
|
|
1946
|
+
if (!this.wsClient || !this.isWebSocketConnectedFlag) {
|
|
1947
|
+
throw new Error("WebSocket ainda n\xE3o est\xE1 conectado.");
|
|
1924
1948
|
}
|
|
1925
1949
|
console.log(`Registrando listener para evento: ${event}`);
|
|
1926
1950
|
this.wsClient.on(event, callback);
|